From e05208e5335fe8c68ae238dbd7ab02cc86a4e294 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 09:58:07 +0530 Subject: [PATCH 01/13] Multiple file download changes --- .../java/integration/com/sap/cds/sdm/Api.java | 53 ++ .../com/sap/cds/sdm/ApiInterface.java | 4 + .../integration/com/sap/cds/sdm/ApiMT.java | 49 ++ ...ntegrationTest_Chapters_MultipleFacet.java | 615 ++++++++++++++++++ .../sdm/IntegrationTest_MultipleFacet.java | 503 ++++++++++++++ .../cds/sdm/IntegrationTest_SingleFacet.java | 566 ++++++++++++++++ 6 files changed, 1790 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 b8f92136c..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,59 @@ public List> fetchEntityMetadataDraft( } } + @Override + 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..bc05d3715 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 @@ -121,4 +121,8 @@ public String openAttachment( public Map fetchChangelog( String appUrl, String entityName, String facetName, String entityID, String ID) throws IOException; + + 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 d4c380bc0..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,55 @@ public List> fetchEntityMetadataDraft( } } + @Override + 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_Chapters_MultipleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_Chapters_MultipleFacet.java index abf2b7f32..edabcc985 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.*; @@ -6574,4 +6575,618 @@ void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOExcept api.deleteEntity(appUrl, bookEntityName, newBookID); } } + + @Test + @Order(78) + 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(79) + 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(80) + 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(81) + 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); + } } 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 08f04a515..73ffcc0c3 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.*; @@ -7060,6 +7061,508 @@ void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { } } + @Test + @Order(78) + 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: 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++) { + List extraResp = + api.createAttachment( + appUrl, entityName, facet[0], downloadTestEntityID, srvpath, postData, pdfFile); + if (!extraResp.get(0).equals("Attachment created")) { + 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); + 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(79) + 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(80) + 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(81) + 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(77) // void testUploadAttachmentExceedingMaximumFileSize() 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 ab0dbee55..3afbcee4f 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.*; @@ -6535,6 +6536,571 @@ void testRenameAttachmentWithExtensionChange_WhileUpload() throws IOException { api.deleteEntity(appUrl, entityName, newEntityID); } + @Test + @Order(78) + 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(79) + 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(80) + 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(81) + 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 ca081944d9b0a4b72d5480272d325ec631e1e1df Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 10:39:34 +0530 Subject: [PATCH 02/13] Fix for multifacet --- .../com/sap/cds/sdm/IntegrationTest_MultipleFacet.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 73ffcc0c3..0bbd75ec8 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 @@ -7133,10 +7133,14 @@ void testDownloadAttachmentsAcrossMultipleFacets() throws IOException { List multiIDs = new ArrayList<>(); multiIDs.add(facetAttachmentIDs[0]); + File[] extraFiles = { + new File(classLoader.getResource("sample1.pdf").getFile()), + new File(classLoader.getResource("sample2.pdf").getFile()) + }; for (int i = 0; i < 2; i++) { List extraResp = api.createAttachment( - appUrl, entityName, facet[0], downloadTestEntityID, srvpath, postData, pdfFile); + appUrl, entityName, facet[0], downloadTestEntityID, srvpath, postData, extraFiles[i]); if (!extraResp.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); fail("Could not upload extra attachment to facet: " + facet[0]); From 1f75c51227a0bfe5d4982d4d59ce88f3edd3bf78 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 10:41:27 +0530 Subject: [PATCH 03/13] Commenting other tests --- .../sdm/IntegrationTest_MultipleFacet.java | 12430 ++++++++-------- 1 file changed, 6319 insertions(+), 6111 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 0bbd75ec8..2a23e4f2b 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 @@ -7,13 +7,8 @@ import java.io.File; import java.io.IOException; 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.*; @@ -388,6678 +383,6891 @@ void testUploadSingleTXT() throws IOException { } } - @Test - @Order(5) - void testUploadSingleEXE() throws IOException { - System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); - Boolean testStatus = false; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.exe").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID); - postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables - 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, entityID); - if (response.equals("Entity in draft mode")) { - // Creation of attachment, reference and footnote - for (int i = 0; i < facet.length; i++) { - ID3[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID, postData, file); - } - testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); - } - if (!testStatus) { - fail("Could not upload sample.exe " + response); - } - } - - @Test - @Order(6) - void testUploadPDFDuplicate() throws IOException { - System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID); - 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, entityID); - if ("Entity in draft mode".equals(response)) { - Boolean allFacetsFailedCorrectly = true; - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, file); - allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); - } - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (!allFacetsFailedCorrectly) { - fail("One or more facets were incorrectly accepted as new."); - } - } else { - fail("Entity could not be edited to draft mode."); - } - } - - @Test - @Order(7) - void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { - System.out.println( - "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and footnote"); - Boolean testStatus = false; - // Create a new entity draft - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID2 = response; - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - - if ("Saved".equals(response)) { - response = api.checkEntity(appUrl, entityName, entityID2); - if ("Entity exists".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Could not create entity"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID2); - 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 draft mode - response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - if ("Entity in draft mode".equals(response)) { - // Create attachment, reference, and footnote - for (int i = 0; i < facet.length; i++) { - ID4[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID2, postData, file); - } - // Verify and save - testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); - } - if (!testStatus) { - fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); - } - } - - @Test - @Order(8) - void testRenameEntities() { - System.out.println("Test (8) : Rename single attachment, reference, and footnote"); - Boolean testStatus = true; - - try { - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - - if ("Entity in draft mode".equals(response)) { - String[] name = {"sample123", "reference123", "footnote123"}; - for (int i = 0; i < facet.length; i++) { - // Read the facet to ensure it exists - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); - if (!"Renamed".equals(response)) { - testStatus = false; - System.out.println(facet[i] + " was not renamed: " + response); - } - } - // Save entity draft if everything is renamed - if (testStatus) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (!"Saved".equals(response)) { - testStatus = false; - System.out.println("Entity draft was not saved: " + response); - } - } else { - // Attempt save despite potential rename failures - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } else { - testStatus = false; - System.out.println("Entity was not put into draft mode: " + response); - } - } catch (Exception e) { - testStatus = false; - System.out.println("Exception during renaming entities: " + e.getMessage()); - } - - if (!testStatus) { - fail("There was an error during the rename test process."); - } - } - - @Test - @Order(9) - void testCreateEntitiesWithUnsupportedCharacter() throws IOException { - System.out.println("Test (9): Create attachments with unsupported characters"); - 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("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, entityID); - if (!"Entity in draft mode".equals(response)) { - fail("Entity not in draft mode: " + response); - return; - } - - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", entityID); - List createResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, tempFile); - - String check = createResponse.get(0); - if (!"Attachment created".equals(check)) { - System.out.println("Failed to create attachment for facet: " + facet[i]); - continue; - } - - String restrictedName = "a/\\bc.pdf"; - response = - api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); - } - - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - if (response.equals(expected)) { - api.deleteEntityDraft(appUrl, entityName, entityID); - testStatus = true; - } - - if (!testStatus) { - fail("Facets renamed with restricted characters were not correctly rejected."); - } - } - - @Test - @Order(10) - void testRenameEntitiesWithUnsupportedCharacter() { - System.out.println("Test (10) : Rename attachments with unsupported characters"); - Boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - if (response.equals("Renamed")) counter++; - } - if (counter >= 2) { - counter = -1; // Reset counter for the next check - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - if (response.equals(expected)) { - for (int i = 0; i < facet.length; i++) { - response = - api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], "sample.pdf"); - } - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment was renamed with unsupported characters"); - } - } - - @Test - @Order(11) - void testRenameMultipleEntityComponents() { - System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); - boolean testStatus = true; - - String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (!"Entity in draft mode".equals(draftResponse)) { - fail("Entity is not in draft mode."); - return; - } - String[] name = {"sample1234", "reference1234", "footnote1234"}; - String[] name2 = {"sample12345", "reference12345", "footnote12345"}; - for (int i = 0; i < facet.length; i++) { - // Read the facet to ensure it exists - testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); - testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); - } - // Save the draft if all renames succeeded - if (testStatus) { - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (!"Saved".equals(saveResponse)) { - fail("Entity draft was not saved after renaming."); - } - } else { - // Save draft even if renaming failed to preserve state - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - fail("One or more components were not renamed."); - } - } - - @Test - @Order(12) - void testRenameSingleDuplicate() { - System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); - Boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String[] name = {"sample1234", "reference1234", "footnote1234"}; - String[] name2 = {"sample123456", "reference123456", "footnote123456"}; - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - if (response.equals("Renamed")) counter++; - } - if (counter >= 2) { - counter = -1; // Reset counter for the next check - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - String.format( - "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", - name[1], name[0], name[2]); - if (response.equals(expected)) { - for (int i = 0; i < facet.length; i++) { - // Attempt to rename again with a different name - response = - api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); - if (response.equals("Renamed")) counter++; - } - } - if (counter >= 2) { - // If all renames were successful, save the draft - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - testStatus = true; - } - } else { - testStatus = false; - fail("Attachment was renamed"); - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - } - - @Test - @Order(13) - void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { - System.out.println( - "Test (13) : Rename multiple files out of which one file name contains unsupported characters"); - boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String[] names = {"summary_1234", "reference_4567", "note/invalid"}; - - if (response.equals("Entity in draft mode")) { - int successCount = 0; - for (int i = 0; i < facet.length; i++) { - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], names[i]); - if (response.equals("Renamed")) successCount++; - } - - if (successCount >= 2) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - response = - api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); - 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 with unsupported characters"); - } - } - - @Test - @Order(14) - void testRenameToValidateNames() throws IOException { - System.out.println("Test (14) : Rename attachments to validate names"); - String[] generatedIDs = new String[3]; - String[] duplicateIDs = new String[1]; - boolean testStatus = false, allRenamedSuccessfully = true; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID3 = response; - - String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; - String duplicateName = "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"); - - // Creation of attachment, reference and footnote - for (int i = 0; i < facet.length; i++) { - File file = new File(classLoader.getResource("sample2.pdf").getFile()); - generatedIDs[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - response = - api.renameAttachment( - appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); - allRenamedSuccessfully &= "Renamed".equals(response); - } - File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Creating duplicate name for last facet - duplicateIDs[0] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[2], entityID3, postData, file); - String response2 = - api.renameAttachment( - appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); - - if (allRenamedSuccessfully && "Renamed".equals(response2)) { - 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: references\\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: footnotes\\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(15) - void testRenameEntitiesWithoutSDMRole() throws IOException { - System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); - boolean testStatus = true; - try { - String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); - if ("Entity in draft mode".equals(apiResponse)) { - String[] name = {"sample456", "reference456", "footnote456"}; - for (int i = 0; i < facet.length; i++) { - apiResponse = - apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); - if (!"Renamed".equals(apiResponse)) { - testStatus = false; - } - } - if (testStatus) { - apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (!apiResponse.equals(expected)) { - testStatus = false; - } - } else { - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - } catch (Exception e) { - testStatus = false; - } - if (!testStatus) { - fail("Attachment got renamed without SDM roles."); - } - } + // @Test + // @Order(5) + // void testUploadSingleEXE() throws IOException { + // System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); + // Boolean testStatus = false; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.exe").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables + // 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, entityID); + // if (response.equals("Entity in draft mode")) { + // // Creation of attachment, reference and footnote + // for (int i = 0; i < facet.length; i++) { + // ID3[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID, postData, file); + // } + // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); + // } + // if (!testStatus) { + // fail("Could not upload sample.exe " + response); + // } + // } - @Test - @Order(16) - void testDeleteSingleAttachment() throws IOException { - System.out.println("Test (16) : Delete single attachment, reference, and footnote"); - Boolean testStatus = false; - counter = -1; + // @Test + // @Order(6) + // void testUploadPDFDuplicate() throws IOException { + // System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // 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, entityID); + // if ("Entity in draft mode".equals(response)) { + // Boolean allFacetsFailedCorrectly = true; + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, + // file); + // allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); + // } + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!allFacetsFailedCorrectly) { + // fail("One or more facets were incorrectly accepted as new."); + // } + // } else { + // fail("Entity could not be edited to draft mode."); + // } + // } - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - if (response.equals("Deleted")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - counter = -1; // Reset counter for the next check - if (response.equals("Saved")) { - for (int i = 0; i < facet.length; i++) { - response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - if (response.equals("Could not read Attachment")) counter++; - } - if (counter >= 2) testStatus = true; - else fail("Could not read deleted facets"); - } else { - fail("Could not save entity after deletion"); - } - } - } + // @Test + // @Order(7) + // void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { + // System.out.println( + // "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and + // footnote"); + // Boolean testStatus = false; + // // Create a new entity draft + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID2 = response; + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + + // if ("Saved".equals(response)) { + // response = api.checkEntity(appUrl, entityName, entityID2); + // if ("Entity exists".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Could not create entity"); + // } - @Test - @Order(17) - void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { - System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); - Boolean testStatus = false; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID2); + // 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 draft mode + // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + // if ("Entity in draft mode".equals(response)) { + // // Create attachment, reference, and footnote + // for (int i = 0; i < facet.length; i++) { + // ID4[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID2, postData, file); + // } + // // Verify and save + // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); + // } + // if (!testStatus) { + // fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); + // } + // } - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; - } - } - if (counter >= 2) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - if (response.equals("Saved")) { - for (int i = 0; i < facet.length; i++) { - String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - if (response1.equals("Could not read " + facet[i]) - && response2.equals("Could not read " + facet[i])) { - counter++; - } - } - if (counter >= 2) testStatus = true; - else fail("Could not read deleted facets"); - } else fail("Could not save entity after deletion"); - } + // @Test + // @Order(8) + // void testRenameEntities() { + // System.out.println("Test (8) : Rename single attachment, reference, and footnote"); + // Boolean testStatus = true; + + // try { + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + + // if ("Entity in draft mode".equals(response)) { + // String[] name = {"sample123", "reference123", "footnote123"}; + // for (int i = 0; i < facet.length; i++) { + // // Read the facet to ensure it exists + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], + // name[i]); + // if (!"Renamed".equals(response)) { + // testStatus = false; + // System.out.println(facet[i] + " was not renamed: " + response); + // } + // } + // // Save entity draft if everything is renamed + // if (testStatus) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!"Saved".equals(response)) { + // testStatus = false; + // System.out.println("Entity draft was not saved: " + response); + // } + // } else { + // // Attempt save despite potential rename failures + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } else { + // testStatus = false; + // System.out.println("Entity was not put into draft mode: " + response); + // } + // } catch (Exception e) { + // testStatus = false; + // System.out.println("Exception during renaming entities: " + e.getMessage()); + // } - @Test - @Order(18) - void testUploadBlockedMimeType() throws IOException { - System.out.println("Test (18) : Upload blocked mimeType .rtf"); - Boolean testStatus = false; + // if (!testStatus) { + // fail("There was an error during the rename test process."); + // } + // } - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID2 = response; + // @Test + // @Order(9) + // void testCreateEntitiesWithUnsupportedCharacter() throws IOException { + // System.out.println("Test (9): Create attachments with unsupported characters"); + // boolean testStatus = false; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + // 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("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, entityID); + // if (!"Entity in draft mode".equals(response)) { + // fail("Entity not in draft mode: " + response); + // return; + // } - 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"); + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", entityID); + // List createResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, + // tempFile); - boolean allBlocked = true; - for (int i = 0; i < facet.length; i++) { - List createResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, file); + // String check = createResponse.get(0); + // if (!"Attachment created".equals(check)) { + // System.out.println("Failed to create attachment for facet: " + facet[i]); + // continue; + // } - 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.\"}}"; + // String restrictedName = "a/\\bc.pdf"; + // response = + // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); + // } - if (!expectedJson.equals(actualResponse)) { - allBlocked = false; - System.out.println( - "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); - } - } + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" + // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // if (response.equals(expected)) { + // api.deleteEntityDraft(appUrl, entityName, entityID); + // testStatus = true; + // } - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if ("Saved".equals(response) && allBlocked) { - testStatus = true; - } - } + // if (!testStatus) { + // fail("Facets renamed with restricted characters were not correctly rejected."); + // } + // } - if (!testStatus) { - fail("Attachment got uploaded with blocked .rtf MIME type"); - } - } + // @Test + // @Order(10) + // void testRenameEntitiesWithUnsupportedCharacter() { + // System.out.println("Test (10) : Rename attachments with unsupported characters"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + // if (response.equals("Renamed")) counter++; + // } + // if (counter >= 2) { + // counter = -1; // Reset counter for the next check + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains + // unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" + // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // if (response.equals(expected)) { + // for (int i = 0; i < facet.length; i++) { + // response = + // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], + // "sample.pdf"); + // } + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment was renamed with unsupported characters"); + // } + // } - @Test - @Order(19) - void testDeleteEntity() { - System.out.println("Test (19) : Delete entity"); - Boolean testStatus = false; - String response = api.deleteEntity(appUrl, entityName, entityID); - String response2 = api.deleteEntity(appUrl, entityName, entityID2); - if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = true; - if (!testStatus) fail("Could not delete entity"); - } + // @Test + // @Order(11) + // void testRenameMultipleEntityComponents() { + // System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); + // boolean testStatus = true; + + // String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!"Entity in draft mode".equals(draftResponse)) { + // fail("Entity is not in draft mode."); + // return; + // } + // String[] name = {"sample1234", "reference1234", "footnote1234"}; + // String[] name2 = {"sample12345", "reference12345", "footnote12345"}; + // for (int i = 0; i < facet.length; i++) { + // // Read the facet to ensure it exists + // testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); + // testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); + // } + // // Save the draft if all renames succeeded + // if (testStatus) { + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!"Saved".equals(saveResponse)) { + // fail("Entity draft was not saved after renaming."); + // } + // } else { + // // Save draft even if renaming failed to preserve state + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // fail("One or more components were not renamed."); + // } + // } - @Test - @Order(20) - void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - System.out.println("Test (20) : Rename & Update secondary property before entity is saved"); - System.out.println("Creating entity"); + // @Test + // @Order(12) + // void testRenameSingleDuplicate() { + // System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String[] name = {"sample1234", "reference1234", "footnote1234"}; + // String[] name2 = {"sample123456", "reference123456", "footnote123456"}; + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + // if (response.equals("Renamed")) counter++; + // } + // if (counter >= 2) { + // counter = -1; // Reset counter for the next check + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // String.format( + // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already + // exists. Rename the object and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named + // \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An + // object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: + // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", + // name[1], name[0], name[2]); + // if (response.equals(expected)) { + // for (int i = 0; i < facet.length; i++) { + // // Attempt to rename again with a different name + // response = + // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); + // if (response.equals("Renamed")) counter++; + // } + // } + // if (counter >= 2) { + // // If all renames were successful, save the draft + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } else { + // testStatus = false; + // fail("Attachment was renamed"); + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // } - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // @Test + // @Order(13) + // void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { + // System.out.println( + // "Test (13) : Rename multiple files out of which one file name contains unsupported + // characters"); + // boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String[] names = {"summary_1234", "reference_4567", "note/invalid"}; + + // if (response.equals("Entity in draft mode")) { + // int successCount = 0; + // for (int i = 0; i < facet.length; i++) { + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], + // names[i]); + // if (response.equals("Renamed")) successCount++; + // } - if (!response.equals("Could not create entity")) { - entityID3 = response; + // if (successCount >= 2) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // response = + // api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) testStatus = true; + // } + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } - System.out.println("Creating attachment, reference, and footnote"); + // if (!testStatus) { + // fail("Attachment was renamed with unsupported characters"); + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // @Test + // @Order(14) + // void testRenameToValidateNames() throws IOException { + // System.out.println("Test (14) : Rename attachments to validate names"); + // String[] generatedIDs = new String[3]; + // String[] duplicateIDs = new String[1]; + // boolean testStatus = false, allRenamedSuccessfully = true; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID3 = response; - 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"); + // String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; + // String duplicateName = "duplicateName.pdf"; - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // 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"); - System.out.println("Attachments, References, and Footnotes created"); + // // Creation of attachment, reference and footnote + // for (int i = 0; i < facet.length; i++) { + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // generatedIDs[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // response = + // api.renameAttachment( + // appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); + // allRenamedSuccessfully &= "Renamed".equals(response); + // } + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // // Creating duplicate name for last facet + // duplicateIDs[0] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[2], entityID3, postData, file); + // String response2 = + // api.renameAttachment( + // appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); + + // if (allRenamedSuccessfully && "Renamed".equals(response2)) { + // 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: references\\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: footnotes\\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; + // } + // } - // Use valid dropdown value for customProperty1 - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // @Test + // @Order(15) + // void testRenameEntitiesWithoutSDMRole() throws IOException { + // System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); + // boolean testStatus = true; + // try { + // String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if ("Entity in draft mode".equals(apiResponse)) { + // String[] name = {"sample456", "reference456", "footnote456"}; + // for (int i = 0; i < facet.length; i++) { + // apiResponse = + // apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], + // name[i]); + // if (!"Renamed".equals(apiResponse)) { + // testStatus = false; + // } + // } + // if (testStatus) { + // apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "[{\"code\":\"\",\"message\":\"Could not update the following files. + // \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update + // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (!apiResponse.equals(expected)) { + // testStatus = false; + // } + // } else { + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // } catch (Exception e) { + // testStatus = false; + // } + // if (!testStatus) { + // fail("Attachment got renamed without SDM roles."); + // } + // } - String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + // @Test + // @Order(16) + // void testDeleteSingleAttachment() throws IOException { + // System.out.println("Test (16) : Delete single attachment, reference, and footnote"); + // Boolean testStatus = false; + // counter = -1; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + // if (response.equals("Deleted")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // counter = -1; // Reset counter for the next check + // if (response.equals("Saved")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + // if (response.equals("Could not read Attachment")) counter++; + // } + // if (counter >= 2) testStatus = true; + // else fail("Could not read deleted facets"); + // } else { + // fail("Could not save entity after deletion"); + // } + // } + // } - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - - // Update customProperty1 (String - dropdown value) - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - - // Update customProperty2 (Integer) - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - - // Update customProperty5 (DateTime) - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - - // Update customProperty6 (Boolean) - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - counter++; - } - } + // @Test + // @Order(17) + // void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { + // System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + // String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + // if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; + // } + // } + // if (counter >= 2) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // if (response.equals("Saved")) { + // for (int i = 0; i < facet.length; i++) { + // String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + // String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + // if (response1.equals("Could not read " + facet[i]) + // && response2.equals("Could not read " + facet[i])) { + // counter++; + // } + // } + // if (counter >= 2) testStatus = true; + // else fail("Could not read deleted facets"); + // } else fail("Could not save entity after deletion"); + // } - if (counter >= 2) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - } - if (response.equals("Saved")) { - testStatus = true; - } - } + // @Test + // @Order(18) + // void testUploadBlockedMimeType() throws IOException { + // System.out.println("Test (18) : Upload blocked mimeType .rtf"); + // Boolean testStatus = false; - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID2 = response; - @Test - @Order(21) - void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { - System.out.println("Test (21): Rename & Update secondary property after entity is saved"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - System.out.println("Editing entity"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - if (response.equals("Entity in draft mode")) { - // Sample secondary properties - String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; - Integer secondaryPropertyInt = 42; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // 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("Renaming and updating secondary properties for attachment"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Saved")) { - testStatus = true; - System.out.println("Renamed & updated Secondary properties for attachment"); - } - // Clean up - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); - } - if (!testStatus) fail("Could not update secondary properties after entity is saved"); - } + // boolean allBlocked = true; + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, + // file); - @Test - @Order(22) - void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - System.out.println( - "Test (22): 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 (response != "Could not create entity") { - entityID3 = response; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // 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.\"}}"; - 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"); + // if (!expectedJson.equals(actualResponse)) { + // allBlocked = false; + // System.out.println( + // "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); + // } + // } - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } - // Prepare test data - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testid"; + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if ("Saved".equals(response) && allBlocked) { + // testStatus = true; + // } + // } - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, or footnote"); - } + // if (!testStatus) { + // fail("Attachment got uploaded with blocked .rtf MIME type"); + // } + // } - @Test - @Order(23) - void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { - System.out.println( - "Test (23): Rename & Update invalid secondary property after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; + // @Test + // @Order(19) + // void testDeleteEntity() { + // System.out.println("Test (19) : Delete entity"); + // Boolean testStatus = false; + // String response = api.deleteEntity(appUrl, entityName, entityID); + // String response2 = api.deleteEntity(appUrl, entityName, entityID2); + // if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = + // true; + // if (!testStatus) fail("Could not delete entity"); + // } - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Entity in draft mode")) { - String name1 = "sample.pdf"; - Integer secondaryPropertyInt = 12; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testidinvalid"; + // @Test + // @Order(20) + // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + // System.out.println("Test (20) : Rename & Update secondary property before entity is saved"); + // System.out.println("Creating entity"); - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for attachment, reference, footnote is unsuccessfull"); - } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (!deleteEntityResponse.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } - if (!testStatus) - fail( - "Could not update secondary property after entity is saved for attachment, reference, or footnote"); - } + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - @Test - @Order(24) - void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (24): Rename & Update valid secondary properties for multiple facets 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; + // if (!response.equals("Could not create entity")) { + // entityID3 = response; - System.out.println("Entity created"); - ClassLoader classLoader = getClass().getClassLoader(); + // System.out.println("Creating attachment, reference, and footnote"); - 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"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - System.out.println("Creating attachment, reference, and footnote PDF"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // 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"); - System.out.println("Creating attachment, reference, and footnote TXT"); - file = new File(classLoader.getResource("sample.txt").getFile()); - postData.put("mimeType", "application/txt"); - for (int i = 0; i < facet.length; i++) { - ID2[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - System.out.println("Creating attachment, reference, and footnote EXE"); - file = new File(classLoader.getResource("sample.exe").getFile()); - postData.put("mimeType", "application/exe"); - for (int i = 0; i < facet.length; i++) { - ID3[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } - Boolean Updated1[] = new Boolean[3]; - Boolean Updated2[] = new Boolean[3]; - Boolean Updated3[] = new Boolean[3]; - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // PDF - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } + // System.out.println("Attachments, References, and Footnotes created"); + + // // Use valid dropdown value for customProperty1 + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + + // // Update customProperty1 (String - dropdown value) + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + + // // Update customProperty2 (Integer) + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + + // // Update customProperty5 (DateTime) + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + + // // Update customProperty6 (Boolean) + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // counter++; + // } + // } - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - // 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, facet[i], entityID3, ID2[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // if (counter >= 2) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // } + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - for (int i = 0; i < facet.length; i++) { - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponseEXE3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); - - if (updateSecondaryPropertyResponseEXE1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated") - && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - 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"); - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - @Test - @Order(25) - void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { - System.out.println( - "Test (25): Rename & Update valid secondary properties for multiple facets after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Entity in draft mode")) { - Boolean Updated1[] = new Boolean[3]; - Boolean Updated2[] = new Boolean[3]; - Boolean Updated3[] = new Boolean[3]; - - String name1 = "sample1.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } + // @Test + // @Order(21) + // void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { + // System.out.println("Test (21): Rename & Update secondary property after entity is saved"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // System.out.println("Editing entity"); + + // if (response.equals("Entity in draft mode")) { + // // Sample secondary properties + // String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; + // Integer secondaryPropertyInt = 42; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // System.out.println("Renaming and updating secondary properties for attachment"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Saved")) { + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for attachment"); + // } + // // Clean up + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); + // } + // if (!testStatus) fail("Could not update secondary properties after entity is saved"); + // } - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - // 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, facet[i], entityID3, ID2[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // @Test + // @Order(22) + // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + // System.out.println( + // "Test (22): 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 (response != "Could not create entity") { + // entityID3 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - for (int i = 0; i < facet.length; i++) { - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponseEXE3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); - - if (updateSecondaryPropertyResponseEXE1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated") - && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } + // 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 < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } + // // Prepare test data + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, + // or footnote"); + // } - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - 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(23) + // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { + // System.out.println( + // "Test (23): 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.equals("Entity in draft mode")) { + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt = 12; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testidinvalid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for attachment, reference, footnote is + // unsuccessfull"); + // } + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (!deleteEntityResponse.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } + // if (!testStatus) + // fail( + // "Could not update secondary property after entity is saved for attachment, reference, + // or footnote"); + // } - @Test - @Order(26) - void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (26): Rename & Update invalid and valid secondary properties for multiple facets before entity is saved"); - System.out.println("Creating entity"); + // @Test + // @Order(24) + // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (24): Rename & Update valid secondary properties for multiple facets 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; - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // System.out.println("Entity created"); + // ClassLoader classLoader = getClass().getClassLoader(); - if (!"Could not create entity".equals(response)) { - entityID3 = response; - System.out.println("Entity created"); + // 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"); - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID3); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // System.out.println("Creating attachment, reference, and footnote PDF"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - // Create PDF attachments - postData.put("mimeType", "application/pdf"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // System.out.println("Creating attachment, reference, and footnote TXT"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // postData.put("mimeType", "application/txt"); + // for (int i = 0; i < facet.length; i++) { + // ID2[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - // Create TXT attachments - postData.put("mimeType", "application/txt"); - file = new File(classLoader.getResource("sample.txt").getFile()); - for (int i = 0; i < facet.length; i++) { - ID2[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // System.out.println("Creating attachment, reference, and footnote EXE"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // postData.put("mimeType", "application/exe"); + // for (int i = 0; i < facet.length; i++) { + // ID3[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } + // Boolean Updated1[] = new Boolean[3]; + // Boolean Updated2[] = new Boolean[3]; + // Boolean Updated3[] = new Boolean[3]; + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // // PDF + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } - // Create EXE attachments - postData.put("mimeType", "application/exe"); - file = new File(classLoader.getResource("sample.exe").getFile()); - for (int i = 0; i < facet.length; i++) { - ID3[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // // 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, facet[i], entityID3, ID2[i], + // bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // for (int i = 0; i < facet.length; i++) { + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponseEXE3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyDate); + + // if (updateSecondaryPropertyResponseEXE1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated") + // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // 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"); + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - String name1 = "sample1234.pdf"; - String dropdownValue = - integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; + // @Test + // @Order(25) + // void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { + // System.out.println( + // "Test (25): Rename & Update valid secondary properties for multiple facets after entity + // is saved"); + // System.out.println("Editing entity"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Entity in draft mode")) { + // Boolean Updated1[] = new Boolean[3]; + // Boolean Updated2[] = new Boolean[3]; + // Boolean Updated3[] = new Boolean[3]; + + // String name1 = "sample1.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } - // Update PDF properties - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String renameResp = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - String upd2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - String upd3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - String upd4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - String updInvalid = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - if ("Renamed".equals(renameResp) - && "Updated".equals(upd1) - && "Updated".equals(upd2) - && "Updated".equals(upd3) - && "Updated".equals(upd4) - && "Updated".equals(updInvalid)) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // // 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, facet[i], entityID3, ID2[i], + // bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - // Update TXT properties - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - String upd = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], bodyBool); - if ("Updated".equals(upd)) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // for (int i = 0; i < facet.length; i++) { + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponseEXE3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyDate); + + // if (updateSecondaryPropertyResponseEXE1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated") + // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } - // Update EXE properties - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValueExe = integrationTestUtils.getDropDownValue(); - String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // 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"); + // } + // } - for (int i = 0; i < facet.length; i++) { - RequestBody bodyDropdownExe = - RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - RequestBody bodyIntExe = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); - String upd2 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); - - if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } + // @Test + // @Order(26) + // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (26): Rename & Update invalid and valid secondary properties for multiple facets + // before entity is saved"); + // System.out.println("Creating entity"); - if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + // if (!"Could not create entity".equals(response)) { + // entityID3 = response; + // System.out.println("Entity created"); - // Verify PDF metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals(expectedNames[0], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertNull(metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID3); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Verify TXT metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - assertEquals(expectedNames[1], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertTrue((Boolean) metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } + // // Create PDF attachments + // postData.put("mimeType", "application/pdf"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - // Verify EXE metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - assertEquals(expectedNames[2], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertEquals( - dropdownValueExe, - metadata.get("customProperty1_code")); // Adjust expected value if needed - assertEquals(1234, metadata.get("customProperty2")); - } + // // Create TXT attachments + // postData.put("mimeType", "application/txt"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID2[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - 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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update unsuccessful for invalid properties and successful for valid attachments"); - } - } - } + // // Create EXE attachments + // postData.put("mimeType", "application/exe"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID3[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample1234.pdf"; + // String dropdownValue = + // integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + + // // Update PDF properties + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String renameResp = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // String upd2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // String upd3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // String upd4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + // String updInvalid = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + // if ("Renamed".equals(renameResp) + // && "Updated".equals(upd1) + // && "Updated".equals(upd2) + // && "Updated".equals(upd3) + // && "Updated".equals(upd4) + // && "Updated".equals(updInvalid)) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } - @Test - @Order(27) - void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (27): 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.equals("Entity in draft mode")) { - Boolean Updated1[] = new Boolean[3]; - Boolean Updated2[] = new Boolean[3]; - Boolean Updated3[] = new Boolean[3]; - String name1 = "sample.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - String dropdownValue = integrationTestUtils.getDropDownValue(); - System.out.println("drop down value is: " + dropdownValue); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // PDF - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // Update secondary properties for String - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - // Update invalid secondary property - String updateSecondaryPropertyResponse5 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated") - && updateSecondaryPropertyResponse5.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - // 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, facet[i], entityID3, ID2[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // // Update TXT properties + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + // String upd = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], + // bodyBool); + // if ("Updated".equals(upd)) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - Integer secondaryPropertyInt3 = 12; - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - for (int i = 0; i < facet.length; i++) { - // Update secondary properties for String - System.out.println("drop down value is: " + dropdownValue1); - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); - - if (updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } + // // Update EXE properties + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValueExe = integrationTestUtils.getDropDownValue(); + // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyDropdownExe = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + // RequestBody bodyIntExe = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); + + // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; - // for PDF - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals(name[0], FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - // for TXT - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - assertEquals(name[1], FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertFalse((Boolean) FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - // for EXE - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - assertEquals(name[2], FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); - assertEquals(12, FacetMetadata.get("customProperty2")); - } + // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + + // // Verify PDF metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals(expectedNames[0], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertNull(metadata.get("customProperty6")); + // assertNull(metadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); - } - 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"); - } - } + // // Verify TXT metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + // assertEquals(expectedNames[1], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertTrue((Boolean) metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } - @Test - @Order(28) - void testNAttachments_NewEntity() throws IOException { - System.out.println( - "Test (28): 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, facet[0], entityID4, srvpath, postData1, file); - if (createResponse1.get(0).equals("Attachment created")) { - ID[0] = createResponse1.get(1); - System.out.println("Attachment created"); - } + // // Verify EXE metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + // assertEquals(expectedNames[2], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertEquals( + // dropdownValueExe, + // metadata.get("customProperty1_code")); // Adjust expected value if needed + // assertEquals(1234, metadata.get("customProperty2")); + // } - 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, facet[0], entityID4, srvpath, postData2, file); - if (createResponse2.get(0).equals("Attachment created")) { - ID2[0] = createResponse2.get(1); - System.out.println("Attachment created"); - } + // 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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessful for invalid properties and successful for valid + // attachments"); + // } + // } + // } - 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, facet[0], entityID4, srvpath, postData3, file); - if (createResponse3.get(0).equals("Attachment created")) { - ID[0] = createResponse3.get(1); - System.out.println("Attachment created"); - } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - 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, facet[0], entityID4, srvpath, postData3, file); - if (createResponse4.get(0).equals("Attachment created")) { - ID4[0] = createResponse4.get(1); - System.out.println("Attachment created"); - } + // @Test + // @Order(27) + // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (27): 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.equals("Entity in draft mode")) { + // Boolean Updated1[] = new Boolean[3]; + // Boolean Updated2[] = new Boolean[3]; + // Boolean Updated3[] = new Boolean[3]; + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // System.out.println("drop down value is: " + dropdownValue); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // // PDF + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // // Update secondary properties for String + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + // // Update invalid secondary property + // String updateSecondaryPropertyResponse5 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated") + // && updateSecondaryPropertyResponse5.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // // 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, facet[i], entityID3, ID2[i], + // bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - 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, facet[0], entityID4, srvpath, postData3, file); - if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { - testStatus = true; - ID5[0] = 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"); - } - } + // Integer secondaryPropertyInt3 = 12; + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // for (int i = 0; i < facet.length; i++) { + // // Update secondary properties for String + // System.out.println("drop down value is: " + dropdownValue1); + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyInt); + + // if (updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } - @Test - @Order(29) - void testUploadNAttachments() throws IOException { - System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; + // // for PDF + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals(name[0], FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + // // for TXT + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + // assertEquals(name[1], FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertFalse((Boolean) FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + // // for EXE + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + // assertEquals(name[2], FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); + // assertEquals(12, FacetMetadata.get("customProperty2")); + // } - 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, facet[0], 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 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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); + // } + // 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(30) - void testDiscardDraftWithoutAttachments() { - System.out.println("Test (30) : Discard draft without adding attachments"); - Boolean testStatus = false; + // @Test + // @Order(28) + // void testNAttachments_NewEntity() throws IOException { + // System.out.println( + // "Test (28): 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 response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID6 = response; - response = api.deleteEntityDraft(appUrl, entityName, entityID6); - if (response.equals("Entity Draft Deleted")) { - testStatus = true; - } - } - if (!testStatus) { - fail("Draft was not discarded properly"); - } - } + // System.out.println("Entity created"); - @Test - @Order(31) - void testDiscardDraftWithAttachments() throws IOException { - System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); - boolean testStatus = false; + // System.out.println("Creating attachment PDF"); + // ClassLoader classLoader = getClass().getClassLoader(); - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID6 = response; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID6); - 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 < facet.length; i++) { - List createResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, file); - if ("Attachment created".equals(createResponse.get(0))) { - System.out.println("Attachment created in facet: " + facet[i]); - } else { - System.out.println("Attachment creation failed in facet: " + facet[i]); - } - } - response = api.deleteEntityDraft(appUrl, entityName, entityID6); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - if (!testStatus) { - fail("Draft with attachments was not discarded properly"); - } - } + // 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"); - @Test - @Order(32) - void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { - System.out.println("Test (32): Upload to all facets, delete one, and create entity"); + // List createResponse1 = + // api.createAttachment(appUrl, entityName, facet[0], entityID4, srvpath, postData1, + // file); + // if (createResponse1.get(0).equals("Attachment created")) { + // ID[0] = createResponse1.get(1); + // System.out.println("Attachment created"); + // } - boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // 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, facet[0], entityID4, srvpath, postData2, + // file); + // if (createResponse2.get(0).equals("Attachment created")) { + // ID2[0] = createResponse2.get(1); + // System.out.println("Attachment created"); + // } - if (!"Could not create entity".equals(response)) { - entityID5 = response; - ClassLoader classLoader = getClass().getClassLoader(); - - File file1 = - new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - File file2 = - new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); - - Map postData2 = new HashMap<>(postData1); - postData2.put("up__ID", entityID5); - postData2.put("mimeType", "text/plain"); - postData2.put("createdAt", new Date().toString()); - postData2.put("createdBy", "test@test.com"); - postData2.put("modifiedBy", "test@test.com"); - - boolean allCreated = true; - for (int i = 0; i < facet.length; i++) { - List response1 = - api.createAttachment( - appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); - List response2 = - api.createAttachment( - appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); - - if (response1.get(0).equals("Attachment created") - && response2.get(0).equals("Attachment created")) { - ID4[i] = response1.get(1); // to keep one - ID5[i] = response2.get(1); // will delete this one - } else { - allCreated = false; - break; - } + // 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, facet[0], entityID4, srvpath, postData3, + // file); + // if (createResponse3.get(0).equals("Attachment created")) { + // ID[0] = createResponse3.get(1); + // System.out.println("Attachment created"); + // } - String deleteResponse = - api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); - if (!"Deleted".equals(deleteResponse)) { - allCreated = false; - break; - } - } + // 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, facet[0], entityID4, srvpath, postData3, + // file); + // if (createResponse4.get(0).equals("Attachment created")) { + // ID4[0] = createResponse4.get(1); + // System.out.println("Attachment created"); + // } - if (allCreated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } + // 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, facet[0], entityID4, srvpath, postData3, + // file); + // if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { + // testStatus = true; + // ID5[0] = 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"); + // } + // } - if (!testStatus) { - fail("Failed to upload multiple facet entries, delete one per facet and create entity"); - } - } + // @Test + // @Order(29) + // void testUploadNAttachments() throws IOException { + // System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); - @Test - @Order(33) - void testUpdateEntityDraft() throws IOException { - System.out.println("Test (33): Update entity draft with new facet content"); - boolean testStatus = false; + // 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, facet[0], 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"); + // } + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + // @Test + // @Order(30) + // void testDiscardDraftWithoutAttachments() { + // System.out.println("Test (30) : Discard draft without adding attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID6 = response; + // response = api.deleteEntityDraft(appUrl, entityName, entityID6); + // if (response.equals("Entity Draft Deleted")) { + // testStatus = true; + // } + // } + // if (!testStatus) { + // fail("Draft was not discarded properly"); + // } + // } - File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + // @Test + // @Order(31) + // void testDiscardDraftWithAttachments() throws IOException { + // System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); + // boolean testStatus = false; - 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.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID6 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); - if ("Entity in draft mode".equals(response)) { - boolean allCreated = true; + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID6); + // 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 < facet.length; i++) { + // List createResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, + // file); + // if ("Attachment created".equals(createResponse.get(0))) { + // System.out.println("Attachment created in facet: " + facet[i]); + // } else { + // System.out.println("Attachment creation failed in facet: " + facet[i]); + // } + // } + // response = api.deleteEntityDraft(appUrl, entityName, entityID6); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // if (!testStatus) { + // fail("Draft with attachments was not discarded properly"); + // } + // } - for (int i = 0; i < facet.length; i++) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); - if (!"Attachment created".equals(createResponse.get(0))) { - allCreated = false; - } - } + // @Test + // @Order(32) + // void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { + // System.out.println("Test (32): Upload to all facets, delete one, and create entity"); - if (allCreated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } - api.deleteEntity(appUrl, entityName, entityID5); - if (!testStatus) { - fail("Failed to update draft with new attachments for all facets"); - } - } + // boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - @Test - @Order(34) - void testUploadAttachmentWithoutSDMRole() throws IOException { - System.out.println("Test (34): Upload attachment across facets without SDM role"); - boolean testStatus = true; - - String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID7 = response; - 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", 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 (!"Could not create entity".equals(response)) { + // entityID5 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + + // File file1 = + // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + // File file2 = + // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); + + // Map postData2 = new HashMap<>(postData1); + // postData2.put("up__ID", entityID5); + // postData2.put("mimeType", "text/plain"); + // postData2.put("createdAt", new Date().toString()); + // postData2.put("createdBy", "test@test.com"); + // postData2.put("modifiedBy", "test@test.com"); + + // boolean allCreated = true; + // for (int i = 0; i < facet.length; i++) { + // List response1 = + // api.createAttachment( + // appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); + // List response2 = + // api.createAttachment( + // appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); + + // if (response1.get(0).equals("Attachment created") + // && response2.get(0).equals("Attachment created")) { + // ID4[i] = response1.get(1); // to keep one + // ID5[i] = response2.get(1); // will delete this one + // } else { + // allCreated = false; + // break; + // } - for (int i = 0; i < facet.length; i++) { - List createResponse = - apiNoRoles.createAttachment( - appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); - String check = createResponse.get(0); - String expectedError = - "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions to upload attachments. Please contact your administrator for access.\"}}"; - if (!expectedError.equals(check)) { - testStatus = false; - } - } - } - api.deleteEntityDraft(appUrl, entityName, entityID7); - if (!testStatus) { - fail("Attachment uploaded without SDM role for one or more facets"); - } - } + // String deleteResponse = + // api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); + // if (!"Deleted".equals(deleteResponse)) { + // allCreated = false; + // break; + // } + // } - @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<>(); - for (int i = 0; i < 3; i++) { - attachments.add(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 (allCreated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } - for (int i = 0; i < facet.length; i++) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - copyAttachmentSourceEntity, - srvpath, - postData, - file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - } - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facet[i], 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"); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - if (sourceObjectIds.size() == 6) { - String copyResponse; - int i = 0; - for (String facetName : facet) { - if (i != 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } - copyResponse = - api.copyAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - i += 2; - if (copyResponse.equals("Attachments copied successfully")) { - // Fetch copied attachment IDs from target draft - List> copiedMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - List copiedAttachmentIds = - copiedMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - 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"); - } - } + // if (!testStatus) { + // fail("Failed to upload multiple facet entries, delete one per facet and create entity"); + // } + // } - @Test - @Order(36) - void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { - System.out.println( - "Test (36): Copy incorrect 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")) { - if (sourceObjectIds.size() == 6) { - int i = 0; - for (String facet : facet) { - try { - List currentFacetObjectIds = - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - currentFacetObjectIds.add("incorrectObjectId"); - if (currentFacetObjectIds.size() != 3) { - fail("Not enough object IDs to copy attachments for facet: " + facet); - } - api.copyAttachment( - appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, currentFacetObjectIds); - fail("Copy attachments did not throw an error"); - } catch (IOException e) { - i += 2; - } - } - 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(33) + // void testUpdateEntityDraft() throws IOException { + // System.out.println("Test (33): Update entity draft with new facet content"); + // boolean testStatus = false; - @Test - @Order(37) - void testCopyAttachmentWithNotesField() throws IOException { - System.out.println( - "Test (37): Create entity with attachments containing notes in multiple facets, copy to new entity and verify notes field"); - 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 ("Entity in draft mode".equals(response)) { + // boolean allCreated = true; + + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); + // if (!"Attachment created".equals(createResponse.get(0))) { + // allCreated = false; + // } + // } - copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } + // api.deleteEntity(appUrl, entityName, entityID5); + // if (!testStatus) { + // fail("Failed to update draft with new attachments for all facets"); + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - String notesValue = "This is a test note for copy attachment verification"; - MediaType mediaType = MediaType.parse("application/json"); - - for (String facetName : facet) { - 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); + // @Test + // @Order(34) + // void testUploadAttachmentWithoutSDMRole() throws IOException { + // System.out.println("Test (34): Upload attachment across facets without SDM role"); + // boolean testStatus = true; - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } + // String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID7 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - String sourceAttachmentId = createResponse.get(1); + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); + // 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 updateResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateBody); + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // apiNoRoles.createAttachment( + // appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); + // String check = createResponse.get(0); + // String expectedError = + // "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions + // to upload attachments. Please contact your administrator for access.\"}}"; + // if (!expectedError.equals(check)) { + // testStatus = false; + // } + // } + // } + // api.deleteEntityDraft(appUrl, entityName, entityID7); + // if (!testStatus) { + // fail("Attachment uploaded without SDM role for one or more facets"); + // } + // } - if (!updateResponse.equals("Updated")) { - fail("Could not update attachment notes field in facet: " + facetName); - } - } + // @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<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(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"); - List objectIdsToStore = new ArrayList<>(); - for (String facetName : facet) { - List> sourceAttachmentsMetadata = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); + // for (int i = 0; i < facet.length; i++) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // copyAttachmentSourceEntity, + // srvpath, + // postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // } + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facet[i], 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"); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + // if (sourceObjectIds.size() == 6) { + // String copyResponse; + // int i = 0; + // for (String facetName : facet) { + // if (i != 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } + // copyResponse = + // api.copyAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + // i += 2; + // if (copyResponse.equals("Attachments copied successfully")) { + // // Fetch copied attachment IDs from target draft + // List> copiedMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // List copiedAttachmentIds = + // copiedMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // 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"); + // } + // } - if (sourceAttachmentsMetadata.isEmpty()) { - fail("No attachments found in source entity for facet: " + facetName); - } + // @Test + // @Order(36) + // void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (36): Copy incorrect 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")) { + // if (sourceObjectIds.size() == 6) { + // int i = 0; + // for (String facet : facet) { + // try { + // List currentFacetObjectIds = + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + // currentFacetObjectIds.add("incorrectObjectId"); + // if (currentFacetObjectIds.size() != 3) { + // fail("Not enough object IDs to copy attachments for facet: " + facet); + // } + // api.copyAttachment( + // appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, + // currentFacetObjectIds); + // fail("Copy attachments did not throw an error"); + // } catch (IOException e) { + // i += 2; + // } + // } + // 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"); + // } + // } - Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); + // @Test + // @Order(37) + // void testCopyAttachmentWithNotesField() throws IOException { + // System.out.println( + // "Test (37): Create entity with attachments containing notes in multiple facets, copy to + // new entity and verify notes field"); + // Boolean testStatus = false; - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId for facet: " + facetName); - } + // 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(); - objectIdsToStore.add(sourceObjectId); - - 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 for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + sourceNoteValue); - } - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String notesValue = "This is a test note for copy attachment verification"; + // MediaType mediaType = MediaType.parse("application/json"); - int startIndex = sourceObjectIds.size(); - sourceObjectIds.addAll(objectIdsToStore); + // for (String facetName : facet) { + // 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 saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } - int facetIndex = 0; - for (String facetName : facet) { - if (facetIndex > 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } + // String sourceAttachmentId = createResponse.get(1); - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - String copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // String updateResponse = + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facetName, + // copyCustomSourceEntity, + // sourceAttachmentId, + // updateBody); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity for facet: " + facetName); - } + // if (!updateResponse.equals("Updated")) { + // fail("Could not update attachment notes field in facet: " + facetName); + // } + // } - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity for facet: " + facetName); - } + // List objectIdsToStore = new ArrayList<>(); + // for (String facetName : facet) { + // List> sourceAttachmentsMetadata = + // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); - facetIndex++; - } + // if (sourceAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in source entity for facet: " + facetName); + // } - for (String facetName : facet) { - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity for facet: " + facetName); - } + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId for facet: " + facetName); + // } - 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 for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + copiedNoteValue); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // objectIdsToStore.add(sourceObjectId); + + // 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 for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + sourceNoteValue); + // } + // } - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // int startIndex = sourceObjectIds.size(); + // sourceObjectIds.addAll(objectIdsToStore); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment from target entity for facet: " + facetName); - } else { - testStatus = true; - } - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - if (!testStatus) { - fail( - "Could not verify that notes field was copied from source to target attachment for all facets"); - } - } + // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - @Test - @Order(38) - void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { - System.out.println( - "Test (38): Verify that secondary properties are preserved when copying attachments between entities across multiple facets"); - Boolean testStatus = false; + // int facetIndex = 0; + // for (String facetName : facet) { + // if (facetIndex > 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample1.pdf").getFile()); + // String copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - List objectIdsToStore = new ArrayList<>(); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity for facet: " + facetName); + // } - for (String facetName : facet) { - 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 saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity for facet: " + facetName); + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // facetIndex++; + // } - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } + // for (String facetName : facet) { + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - String sourceAttachmentId = createResponse.get(1); - - 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 for facet: " + facetName); - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity for facet: " + facetName); + // } - 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 for facet: " + facetName); - } - } + // 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 for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + copiedNoteValue); + // } - // Save source entity to persist attachments before fetching metadata - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after creating attachments"); - } + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - Integer customProperty2Value = 12345; - for (String facetName : facet) { - List> sourceAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment from target entity for facet: " + facetName); + // } else { + // testStatus = true; + // } + // } - Map sourceAttachmentMetadata = - sourceAttachmentsMetadata.stream() - .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // if (!testStatus) { + // fail( + // "Could not verify that notes field was copied from source to target attachment for all + // facets"); + // } + // } - if (sourceAttachmentMetadata == null) { - fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); - } + // @Test + // @Order(38) + // void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { + // System.out.println( + // "Test (38): Verify that secondary properties are preserved when copying attachments + // between entities across multiple facets"); + // 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 (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId for facet: " + facetName); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample1.pdf").getFile()); - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - objectIdsToStore.add(sourceObjectId); - - 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 for facet " - + facetName - + ". Expected: true, Got: " - + sourceCustomProperty6); - } + // List objectIdsToStore = new ArrayList<>(); - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } - } + // for (String facetName : facet) { + // 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"); - int startIndex = sourceObjectIds.size(); - sourceObjectIds.addAll(objectIdsToStore); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - int facetIndex = 0; - for (String facetName : facet) { - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity"); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + // String sourceAttachmentId = createResponse.get(1); + + // 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 for facet: " + + // facetName); + // } - String copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // 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 for facet: " + facetName); + // } + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity for facet: " + facetName); - } + // // Save source entity to persist attachments before fetching metadata + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after creating attachments"); + // } - // Fetch copied attachment IDs from target draft - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity for facet: " + facetName); - } + // Integer customProperty2Value = 12345; + // for (String facetName : facet) { + // List> sourceAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - facetIndex++; - } + // Map sourceAttachmentMetadata = + // sourceAttachmentsMetadata.stream() + // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - for (String facetName : facet) { - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // if (sourceAttachmentMetadata == null) { + // fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); + // } - Map copiedAttachmentMetadata = - targetAttachmentsMetadata.stream() - .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId for facet: " + facetName); + // } - if (copiedAttachmentMetadata == null) { - fail( - "Could not find the copied attachment with file in target entity for facet: " - + facetName); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // objectIdsToStore.add(sourceObjectId); + + // 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 for facet " + // + facetName + // + ". Expected: true, Got: " + // + sourceCustomProperty6); + // } - Boolean copiedCustomProperty6 = - copiedAttachmentMetadata.get("customProperty6") != null - ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - : null; - Integer copiedCustomProperty2 = - copiedAttachmentMetadata.get("customProperty2") != null - ? (Integer) copiedAttachmentMetadata.get("customProperty2") - : null; - - if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - fail( - "DocumentInfoRecordBoolean was not properly copied for facet " - + facetName - + ". Expected: true, Got: " - + copiedCustomProperty6); - } + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } + // } - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // int startIndex = sourceObjectIds.size(); + // sourceObjectIds.addAll(objectIdsToStore); - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // int facetIndex = 0; + // for (String facetName : facet) { + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity"); + // } - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment from target entity for facet: " + facetName); - } else { - testStatus = true; - } - } + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - if (!testStatus) { - fail( - "Could not verify that all secondary properties were copied from source to target attachment for all facets"); - } - } + // String copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - @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 across multiple facets"); - Boolean testStatus = false; + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity for facet: " + facetName); + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // // Fetch copied attachment IDs from target draft + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity for facet: " + facetName); + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample2.pdf").getFile()); - - String notesValue = "This attachment has both notes and secondary properties for testing"; - MediaType mediaType = MediaType.parse("application/json"); - Integer customProperty2Value = 99999; - List objectIdsToStore = new ArrayList<>(); - - for (String facetName : facet) { - 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); + // facetIndex++; + // } - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } + // for (String facetName : facet) { + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - String sourceAttachmentId = createResponse.get(1); + // Map copiedAttachmentMetadata = + // targetAttachmentsMetadata.stream() + // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + // if (copiedAttachmentMetadata == null) { + // fail( + // "Could not find the copied attachment with file in target entity for facet: " + // + facetName); + // } - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateNotesBody); + // Boolean copiedCustomProperty6 = + // copiedAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + // : null; + // Integer copiedCustomProperty2 = + // copiedAttachmentMetadata.get("customProperty2") != null + // ? (Integer) copiedAttachmentMetadata.get("customProperty2") + // : null; + + // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean was not properly copied for facet " + // + facetName + // + ". Expected: true, Got: " + // + copiedCustomProperty6); + // } - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update attachment notes field for facet: " + facetName); - } + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - 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 for facet: " + facetName); - } + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - 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 (!readResponse.equals("OK")) { + // fail("Could not read copied attachment from target entity for facet: " + facetName); + // } else { + // testStatus = true; + // } + // } - if (!updateSecondaryPropertyResponse2.equals("Updated")) { - fail("Could not update attachment customProperty2 field for facet: " + facetName); - } - } + // if (!testStatus) { + // fail( + // "Could not verify that all secondary properties were copied from source to target + // attachment for all facets"); + // } + // } - // Save source entity to persist attachments before fetching metadata and copying - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after creating attachments"); - } + // @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 across multiple facets"); + // Boolean testStatus = false; + + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyCustomSourceEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity"); + // } - for (String facetName : facet) { - List> sourceAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - Map sourceAttachmentMetadata = - sourceAttachmentsMetadata.stream() - .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // String notesValue = "This attachment has both notes and secondary properties for testing"; + // MediaType mediaType = MediaType.parse("application/json"); + // Integer customProperty2Value = 99999; + // List objectIdsToStore = new ArrayList<>(); - if (sourceAttachmentMetadata == null) { - fail("Could not find attachment with file in facet: " + facetName); - } + // for (String facetName : facet) { + // 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 (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId for facet: " + facetName); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - objectIdsToStore.add(sourceObjectId); - - 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 for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + sourceNoteValue); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } - 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 for facet " - + facetName - + ". Expected: true, Got: " - + sourceCustomProperty6); - } + // String sourceAttachmentId = createResponse.get(1); - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } - } + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - int startIndex = sourceObjectIds.size(); - sourceObjectIds.addAll(objectIdsToStore); + // String updateNotesResponse = + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facetName, + // copyCustomSourceEntity, + // sourceAttachmentId, + // updateNotesBody); - int facetIndex = 0; - for (String facetName : facet) { - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity"); - } + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update attachment notes field for facet: " + facetName); + // } - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + // 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 for facet: " + + // facetName); + // } - String copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // 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 for facet: " + facetName); + // } + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity for facet: " + facetName); - } + // // Save source entity to persist attachments before fetching metadata and copying + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after creating attachments"); + // } - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity for facet: " + facetName); - } + // for (String facetName : facet) { + // List> sourceAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - facetIndex++; - } + // Map sourceAttachmentMetadata = + // sourceAttachmentsMetadata.stream() + // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - for (String facetName : facet) { - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // if (sourceAttachmentMetadata == null) { + // fail("Could not find attachment with file in facet: " + facetName); + // } - Map copiedAttachmentMetadata = - targetAttachmentsMetadata.stream() - .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId for facet: " + facetName); + // } - if (copiedAttachmentMetadata == null) { - fail( - "Could not find the copied attachment with file in target entity for facet: " - + facetName); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // objectIdsToStore.add(sourceObjectId); + + // 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 for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + sourceNoteValue); + // } - String copiedNoteValue = - copiedAttachmentMetadata.get("note") != null - ? copiedAttachmentMetadata.get("note").toString() - : null; - - if (!notesValue.equals(copiedNoteValue)) { - fail( - "Notes field was not properly copied for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + copiedNoteValue); - } + // 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 for facet " + // + facetName + // + ". Expected: true, Got: " + // + sourceCustomProperty6); + // } - Boolean copiedCustomProperty6 = - copiedAttachmentMetadata.get("customProperty6") != null - ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - : null; - Integer copiedCustomProperty2 = - copiedAttachmentMetadata.get("customProperty2") != null - ? (Integer) copiedAttachmentMetadata.get("customProperty2") - : null; - - if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - fail( - "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " - + facetName - + ". Expected: true, Got: " - + copiedCustomProperty6); - } - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } + // } - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment from target entity for facet: " + facetName); - } else { - testStatus = true; - } - } - api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); - api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); - if (!testStatus) { - fail( - "Could not verify that notes field and all secondary properties were copied from source to target attachment for all facets"); - } - } + // int startIndex = sourceObjectIds.size(); + // sourceObjectIds.addAll(objectIdsToStore); - @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<>(); - for (int i = 0; i < 3; i++) { - attachments.add(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"), "sample3.pdf"); - Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); - File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - copyAttachmentSourceEntity, - srvpath, - postData, - file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - } - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); - attachmentsMetadata.add(fetchAttachmentMetadataResponse); - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } - } + // int facetIndex = 0; + // for (String facetName : facet) { + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit 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"); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - if (sourceObjectIds.size() == 6) { - String copyResponse; - int i = 0; - for (String facetName : facet) { - if (i != 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } - List currentFacetObjectIds = - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - if (currentFacetObjectIds.size() != 2) { - fail("Not enough object IDs to copy attachments for facet: " + facet); - } - copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, currentFacetObjectIds); - i += 2; - if (copyResponse.equals("Attachments copied successfully")) { - // Fetch copied attachment IDs from target draft - List> copiedMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - List copiedAttachmentIds = - copiedMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - 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"); - } - } - } - } 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"); - } - } + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - @Test - @Order(41) - void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { - System.out.println("Test (41): Copy attachments from one entity to another new entity"); - 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")) { - if (sourceObjectIds.size() == 6) { - int i = 0; - for (String facetName : facet) { - List currentFacetObjectIds = - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - currentFacetObjectIds.add("incorrectObjectId"); - if (currentFacetObjectIds.size() != 3) { - fail("Not enough object IDs to copy attachments for facet: " + facet); - } - try { - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error"); - } catch (IOException e) { - i += 2; - } - } - 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 copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - @Test - @Order(42) - void testCreateLinkSuccess() throws IOException { - System.out.println("Test (42): Create link in entity"); - List attachments = new ArrayList<>(); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity for facet: " + facetName); + // } - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity for facet: " + facetName); + // } - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - for (String facetName : facet) { - 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")) { - fail("Could not create links for facet : " + facetName + createLinkResponse1); - } - } + // facetIndex++; + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // for (String facetName : facet) { + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - for (String facetName : facet) { - 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); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open created link in facet : " + facetName); - } - } - } - } + // Map copiedAttachmentMetadata = + // targetAttachmentsMetadata.stream() + // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - @Test - @Order(43) - void testCreateLinkDifferentEntity() throws IOException { - System.out.println("Test (43): Create link with same name in different entity"); + // if (copiedAttachmentMetadata == null) { + // fail( + // "Could not find the copied attachment with file in target entity for facet: " + // + facetName); + // } - String createLinkDifferentEntity = - api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkDifferentEntity.equals("Could not edit entity")) { - fail("Could not create entity"); - } + // String copiedNoteValue = + // copiedAttachmentMetadata.get("note") != null + // ? copiedAttachmentMetadata.get("note").toString() + // : null; + + // if (!notesValue.equals(copiedNoteValue)) { + // fail( + // "Notes field was not properly copied for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + copiedNoteValue); + // } - String linkName = "sample"; - String linkUrl = "https://example.com"; - for (String facetName : facet) { - 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"); - } - } + // Boolean copiedCustomProperty6 = + // copiedAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + // : null; + // Integer copiedCustomProperty2 = + // copiedAttachmentMetadata.get("customProperty2") != null + // ? (Integer) copiedAttachmentMetadata.get("customProperty2") + // : null; + + // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " + // + facetName + // + ". Expected: true, Got: " + // + copiedCustomProperty6); + // } + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkDifferentEntity); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment from target entity for facet: " + facetName); + // } else { + // testStatus = true; + // } + // } + // api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); + // api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); + // if (!testStatus) { + // fail( + // "Could not verify that notes field and all secondary properties were copied from source + // to target attachment for all facets"); + // } + // } - response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete 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<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(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"), "sample3.pdf"); + // Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); + // File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // copyAttachmentSourceEntity, + // srvpath, + // postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // } + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); + // attachmentsMetadata.add(fetchAttachmentMetadataResponse); + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } + // } - @Test - @Order(44) - void testCreateLinkFailure() throws IOException { - System.out.println("Test (41): Create link fails due to invalid URL and name"); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (editEntityResponse.equals("Could not edit entity")) { - fail("Could not edit entity"); - } - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "example.com"; - try { - String response = - 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 < 6; i++) { - api.createLink( - appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + linkUrl); - } - System.out.println("Created 5 links in facet: " + facetName); - if (!facetName.equals("footnotes")) { - 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); - if (facetName.equals("references")) { - assertEquals("Cannot upload more than 5 attachments.", errorMessage); - } else if (facetName.equals("attachments")) { - assertEquals("Cannot upload more than 4 attachments.", errorMessage); - } - } - } + // sourceObjectIds.clear(); + // for (Map metadata : attachmentsMetadata) { + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + // if (sourceObjectIds.size() == 6) { + // String copyResponse; + // int i = 0; + // for (String facetName : facet) { + // if (i != 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } + // List currentFacetObjectIds = + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + // if (currentFacetObjectIds.size() != 2) { + // fail("Not enough object IDs to copy attachments for facet: " + facet); + // } + // copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, + // currentFacetObjectIds); + // i += 2; + // if (copyResponse.equals("Attachments copied successfully")) { + // // Fetch copied attachment IDs from target draft + // List> copiedMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // List copiedAttachmentIds = + // copiedMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // 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"); + // } + // } + // } + // } 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 response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } + // @Test + // @Order(41) + // void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { + // System.out.println("Test (41): Copy attachments from one entity to another new entity"); + // 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")) { + // if (sourceObjectIds.size() == 6) { + // int i = 0; + // for (String facetName : facet) { + // List currentFacetObjectIds = + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + // currentFacetObjectIds.add("incorrectObjectId"); + // if (currentFacetObjectIds.size() != 3) { + // fail("Not enough object IDs to copy attachments for facet: " + facet); + // } + // try { + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error"); + // } catch (IOException e) { + // i += 2; + // } + // } + // 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"); + // } + // } - response = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } + // @Test + // @Order(42) + // void testCreateLinkSuccess() throws IOException { + // System.out.println("Test (42): Create link in entity"); + // List attachments = new ArrayList<>(); - @Test - @Order(45) - void testCreateLinkNoSDMRoles() throws IOException { - System.out.println("Test (42): Create link fails due to no SDM roles assigned"); + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - String createLinkEntityNoSDMRoles = - apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { - fail("Could not create entity"); - } + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // for (String facetName : facet) { + // 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")) { + // fail("Could not create links for facet : " + facetName + createLinkResponse1); + // } + // } - for (String facetName : facet) { - 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - String response = - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } + // for (String facetName : facet) { + // 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); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open created link in facet : " + facetName); + // } + // } + // } + // } - response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } + // @Test + // @Order(43) + // void testCreateLinkDifferentEntity() throws IOException { + // System.out.println("Test (43): Create link with same name in different entity"); - @Test - @Order(46) - void testDeleteLink() throws IOException { - System.out.println("Test (43): Delete link in entity"); - List> attachments = new ArrayList<>(); + // String createLinkDifferentEntity = + // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkDifferentEntity.equals("Could not edit entity")) { + // fail("Could not create entity"); + // } - String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // String linkName = "sample"; + // String linkUrl = "https://example.com"; + // for (String facetName : facet) { + // 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"); + // } + // } - for (String facetName : facet) { - 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 for facet : " + facetName); - } - } + // String response = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkDifferentEntity); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } + // @Test + // @Order(44) + // void testCreateLinkFailure() throws IOException { + // System.out.println("Test (41): Create link fails due to invalid URL and name"); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (editEntityResponse.equals("Could not edit entity")) { + // fail("Could not edit entity"); + // } + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "example.com"; + // try { + // String response = + // 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 < 6; i++) { + // api.createLink( + // appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + + // linkUrl); + // } + // System.out.println("Created 5 links in facet: " + facetName); + // if (!facetName.equals("footnotes")) { + // 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); + // if (facetName.equals("references")) { + // assertEquals("Cannot upload more than 5 attachments.", errorMessage); + // } else if (facetName.equals("attachments")) { + // assertEquals("Cannot upload more than 4 attachments.", errorMessage); + // } + // } + // } - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } - int index = 0; - for (String facetName : facet) { - String deleteLinkResponse = - api.deleteAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); - System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - if (!deleteLinkResponse.equals("Deleted")) { - fail("Could not delete created link"); - } - index += 1; - } + // response = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // @Test + // @Order(45) + // void testCreateLinkNoSDMRoles() throws IOException { + // System.out.println("Test (42): Create link fails due to no SDM roles assigned"); - index = 0; - attachments.clear(); - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - System.out.println( - "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - if (attachments.get(index).size() != 0) { - fail("Link wasn't deleted"); - } - index += 1; - } + // String createLinkEntityNoSDMRoles = + // apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { + // fail("Could not create entity"); + // } - String response = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } + // for (String facetName : facet) { + // 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); + // } + // } - @Test - @Order(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (44): Rename link in entity"); - List> attachments = new ArrayList<>(); + // String response = + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); + // if (!response.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"); - } + // response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - for (String facetName : facet) { - 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(46) + // void testDeleteLink() throws IOException { + // System.out.println("Test (43): Delete link in entity"); + // List> attachments = new ArrayList<>(); - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } + // for (String facetName : facet) { + // 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 for facet : " + facetName); + // } + // } - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - int index = 0; - for (String facetName : facet) { - successfullyRenamedAttachments.add(attachments.get(index).get(0)); - String renameLinkResponse = - api.renameAttachment( - appUrl, - entityName, - facetName, - createLinkEntity, - attachments.get(index).get(0), - "sampleRenamed"); - if (!renameLinkResponse.equals("Renamed")) { - fail("Could not Renamed created link"); - } - index += 1; - } + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } - 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 (45): 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"); - } + // int index = 0; + // for (String facetName : facet) { + // String deleteLinkResponse = + // api.deleteAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); + // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + // if (!deleteLinkResponse.equals("Deleted")) { + // fail("Could not delete created link"); + // } + // index += 1; + // } - int index = 0; - for (String facetName : facet) { - 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"); - } - } + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (saveResponse.equals("Could not save entity")) { - fail("Could not save entity"); - } + // index = 0; + // attachments.clear(); + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // System.out.println( + // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + // if (attachments.get(index).size() != 0) { + // fail("Link wasn't deleted"); + // } + // index += 1; + // } - index = 0; - List facetAttachments; - for (String facetName : facet) { - int lambdaIndex = index; - facetAttachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .filter( - item -> - !successfullyRenamedAttachments - .get(lambdaIndex) - .equals(item.get("ID"))) // skip unwanted filename - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - index += 1; - attachments.add(facetAttachments.get(0)); - } + // String response = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - System.out.println("Attachments to be renamed: " + attachments); - String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!response.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (44): Rename link in entity"); + // List> attachments = new ArrayList<>(); - index = 0; - for (String facetName : facet) { - api.renameAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(index), "sampleRenamed"); - index += 1; - } + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - String saveError = 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: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - ObjectMapper mapper = new ObjectMapper(); - assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); + // for (String facetName : facet) { + // 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 deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); - if (!deleteEntityResponse.equals("Entity Draft Deleted")) { - fail("Entity draft not deleted"); - } - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - @Test - @Order(49) - void testRenameLinkUnsupportedCharacters() throws IOException { - System.out.println( - "Test (46): Rename link in entity fails due to unsupported characters in name"); - List> attachments = new ArrayList<>(); + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // 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"; + // int index = 0; + // for (String facetName : facet) { + // successfullyRenamedAttachments.add(attachments.get(index).get(0)); + // String renameLinkResponse = + // api.renameAttachment( + // appUrl, + // entityName, + // facetName, + // createLinkEntity, + // attachments.get(index).get(0), + // "sampleRenamed"); + // if (!renameLinkResponse.equals("Renamed")) { + // fail("Could not Renamed created link"); + // } + // index += 1; + // } - for (String facetName : facet) { - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link"); - } - } + // 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"); - } + // @Test + // @Order(48) + // void testRenameLinkDuplicate() throws IOException { + // System.out.println("Test (45): 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"); + // } - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } + // int index = 0; + // for (String facetName : facet) { + // 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 editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (saveResponse.equals("Could not save entity")) { + // fail("Could not save entity"); + // } - int index = 0; - for (String facetName : facet) { - api.renameAttachment( - appUrl, - entityName, - facetName, - createLinkEntity, - attachments.get(index).get(0), - "sampleRenamed//"); - index += 1; - } + // index = 0; + // List facetAttachments; + // for (String facetName : facet) { + // int lambdaIndex = index; + // facetAttachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .filter( + // item -> + // !successfullyRenamedAttachments + // .get(lambdaIndex) + // .equals(item.get("ID"))) // skip unwanted filename + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // index += 1; + // attachments.add(facetAttachments.get(0)); + // } - String error = - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - String expectedError = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - ObjectMapper mapper = new ObjectMapper(); - assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); + // System.out.println("Attachments to be renamed: " + attachments); + // String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!response.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!deleteEntityResponse.equals("Entity Deleted")) { - fail("Entity draft not deleted"); - } - } + // index = 0; + // for (String facetName : facet) { + // api.renameAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(index), + // "sampleRenamed"); + // index += 1; + // } - @Test - @Order(50) - void testEditLinkSuccess() throws IOException { - System.out.println("Test (47): Edit existing link in entity"); - List> attachmentsPerFacet = new ArrayList<>(); + // String saveError = 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: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named + // \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An + // object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: + // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // 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"); + // } + // } - editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (editLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // @Test + // @Order(49) + // void testRenameLinkUnsupportedCharacters() throws IOException { + // System.out.println( + // "Test (46): Rename link in entity fails due to unsupported characters in name"); + // List> attachments = new ArrayList<>(); - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facetName); - } - } + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // String linkName = "sample2"; + // String linkUrl = "https://www.example.com"; - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } + // } - for (String facetName : facet) { - List attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - if (attachments.isEmpty()) { - fail("Could not find link in facet: " + facetName); - } - attachmentsPerFacet.add(attachments); - } + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } - int index = 0; - for (String facetName : facet) { - String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); - } - index++; - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - - int verificationIndex = 0; - for (String facetName : facet) { - List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); - for (String attachmentId : attachmentsInFacet) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open edited link " + attachmentId + " in facet: " + facetName); - } - } - verificationIndex++; - } - } + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - @Test - @Order(51) - void testEditLinkFailureInvalidURL() throws IOException { - System.out.println("Test (48): Edit existing link with invalid url"); - List> attachmentsPerFacet = new ArrayList<>(); - - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // int index = 0; + // for (String facetName : facet) { + // api.renameAttachment( + // appUrl, + // entityName, + // facetName, + // createLinkEntity, + // attachments.get(index).get(0), + // "sampleRenamed//"); + // index += 1; + // } - for (String facetName : facet) { - List attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String error = + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // String expectedError = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" + // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // ObjectMapper mapper = new ObjectMapper(); + // assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); + + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!deleteEntityResponse.equals("Entity Deleted")) { + // fail("Entity draft not deleted"); + // } + // } - if (attachments.isEmpty()) { - fail("Could not edit link in facet: " + facetName); - } - attachmentsPerFacet.add(attachments); - } + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (47): Edit existing link in entity"); + // List> attachmentsPerFacet = new ArrayList<>(); - int index = 0; - for (String facetName : facet) { - try { - String linkId = attachmentsPerFacet.get(index).get(0); - String updatedUrl = "https://editedexample"; - index++; - String editLinkResponse = - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - System.out.println("response " + editLinkResponse); - fail("Edit link did not throw an error for invalid url 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("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); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - } + // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (editLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - @Test - @Order(52) - void testEditLinkFailureEmptyURL() throws IOException { - System.out.println("Test (49): Edit existing link with an empty url"); - List> attachmentsPerFacet = new ArrayList<>(); - - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facetName); + // } + // } - for (String facetName : facet) { - List attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - if (attachments.isEmpty()) { - fail("Could not edit link in facet: " + facetName); - } - attachmentsPerFacet.add(attachments); - } + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - int index = 0; - for (String facetName : facet) { - try { - String linkId = attachmentsPerFacet.get(index).get(0); - String updatedUrl = ""; - index++; - - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - fail("Edit link did not throw an error for empty url 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"); - String expected = "Provide the missing value."; - assertEquals("409008", errorCode); - assertEquals(expected, errorMessage); - } - } - api.deleteEntity(appUrl, entityName, editLinkEntity); - } + // for (String facetName : facet) { + // List attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - @Test - @Order(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); + // if (attachments.isEmpty()) { + // fail("Could not find link in facet: " + facetName); + // } + // attachmentsPerFacet.add(attachments); + // } - Boolean testStatus = false; + // int index = 0; + // for (String facetName : facet) { + // String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); + // } + // index++; + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + + // int verificationIndex = 0; + // for (String facetName : facet) { + // List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); + // for (String attachmentId : attachmentsInFacet) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open edited link " + attachmentId + " in facet: " + facetName); + // } + // } + // verificationIndex++; + // } + // } - editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (editLinkEntity.equals("Could not create entity")) { - fail("Could not edit entity"); - } + // @Test + // @Order(51) + // void testEditLinkFailureInvalidURL() throws IOException { + // System.out.println("Test (48): Edit existing link with invalid url"); + // List> attachmentsPerFacet = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - for (String facetName : facet) { - String linkName = "sampleNoRole_" + facetName; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } + // for (String facetName : facet) { + // List attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // if (attachments.isEmpty()) { + // fail("Could not edit link in facet: " + facetName); + // } + // attachmentsPerFacet.add(attachments); + // } - String editEntityResponse = - apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // int index = 0; + // for (String facetName : facet) { + // try { + // String linkId = attachmentsPerFacet.get(index).get(0); + // String updatedUrl = "https://editedexample"; + // index++; + // String editLinkResponse = + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // System.out.println("response " + editLinkResponse); + // fail("Edit link did not throw an error for invalid url 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("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); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // } - for (String facetName : facet) { - List attachments = - apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // @Test + // @Order(52) + // void testEditLinkFailureEmptyURL() throws IOException { + // System.out.println("Test (49): Edit existing link with an empty url"); + // List> attachmentsPerFacet = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - if (attachments.isEmpty()) { - fail("Could not find link in facet: " + facetName); - } + // for (String facetName : facet) { + // List attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - String linkId = attachments.get(0); - String updatedUrl = "https://www.editedexample.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; - } - } - api.deleteEntity(appUrl, entityName, editLinkEntity); - if (!testStatus) { - fail("Link got edited without SDM roles"); - } - } + // if (attachments.isEmpty()) { + // fail("Could not edit link in facet: " + facetName); + // } + // attachmentsPerFacet.add(attachments); + // } - @Test - @Order(54) - void testCopyLinkSuccessNewEntity() throws IOException { - System.out.println("Test (51): Copy link from one entity to another new entity"); - List> attachmentsByFacet = new ArrayList<>(); - String linkUrl = "https://www.example.com"; - for (int i = 0; i < facet.length; i++) { - attachmentsByFacet.add(new ArrayList<>()); - } + // int index = 0; + // for (String facetName : facet) { + // try { + // String linkId = attachmentsPerFacet.get(index).get(0); + // String updatedUrl = ""; + // index++; + + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // fail("Edit link did not throw an error for empty url 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"); + // String expected = "Provide the missing value."; + // assertEquals("409008", errorCode); + // assertEquals(expected, errorMessage); + // } + // } + // api.deleteEntity(appUrl, entityName, editLinkEntity); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entities"); - } + // Boolean testStatus = false; - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } + // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (editLinkEntity.equals("Could not create entity")) { + // fail("Could not edit entity"); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // for (String facetName : facet) { + // String linkName = "sampleNoRole_" + facetName; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } - sourceObjectIds.clear(); - for (int i = 0; i < facet.length; i++) { - List objectIds = - api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - sourceObjectIds.addAll(objectIds); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - if (sourceObjectIds.size() != facet.length) { - fail( - "Could not fetch object Ids for all attachments. Expected: " - + facet.length - + ", Found: " - + sourceObjectIds.size()); - } + // String editEntityResponse = + // apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft for facet: " + facetName); - } + // for (String facetName : facet) { + // List attachments = + // apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + // if (attachments.isEmpty()) { + // fail("Could not find link in facet: " + facetName); + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - } + // String linkId = attachments.get(0); + // String updatedUrl = "https://www.editedexample.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; + // } + // } + // api.deleteEntity(appUrl, entityName, editLinkEntity); + // if (!testStatus) { + // fail("Link got edited without SDM roles"); + // } + // } - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity after copying attachments for facet " + facetName); - } + // @Test + // @Order(54) + // void testCopyLinkSuccessNewEntity() throws IOException { + // System.out.println("Test (51): Copy link from one entity to another new entity"); + // List> attachmentsByFacet = new ArrayList<>(); + // String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // attachmentsByFacet.add(new ArrayList<>()); + // } - List> attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entities"); + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch in facet " + facetName); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - System.out.println("Attachment type and URL validated for facet " + facetName); - - List attachments = - attachmentsMetadata.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (String attachment : attachments) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open copied link in facet: " + facetName); - } - } + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } - objectIdIndex++; - } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - if (!deleteTargetResponse.equals("Entity Deleted")) { - fail("Could not delete target entity"); - } - } + // sourceObjectIds.clear(); + // for (int i = 0; i < facet.length; i++) { + // List objectIds = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // sourceObjectIds.addAll(objectIds); + // } - @Test - @Order(55) - void testCopyLinkUnsuccessfulNewEntity() throws IOException { - System.out.println( - "Test (52): Copy invalid type of link from one entity to another new entity"); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (sourceObjectIds.size() != facet.length) { + // fail( + // "Could not fetch object Ids for all attachments. Expected: " + // + facet.length + // + ", Found: " + // + sourceObjectIds.size()); + // } - if (!editResponse.equals("Entity in draft mode") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not edit source entity or create target entity"); - } + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft for facet: " + facetName); + // } - sourceObjectIds.add("incorrectObjectId"); + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - for (String facetName : facet) { - try { - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error for facet: " + facetName); - } catch (IOException e) { - System.out.println("Successfully caught expected error for facet: " + facetName); - } - } - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + // } - @Test - @Order(56) - void testCopyLinkFromNewEntityToExistingEntity() throws IOException { - System.out.println("Test (53): Copy link from a new entity to an existing target entity"); + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity after copying attachments for facet " + facetName); + // } - List> attachmentsByFacet = new ArrayList<>(); - String linkUrl = "https://www.example.com"; - for (int i = 0; i < facet.length; i++) { - attachmentsByFacet.add(new ArrayList<>()); - } + // List> attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - for (int i = 0; i < facet.length; i++) { - String linkName = "newsample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch in facet " + facetName); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + // System.out.println("Attachment type and URL validated for facet " + facetName); - sourceObjectIds.clear(); - for (String facetName : facet) { - List objectIds = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - sourceObjectIds.addAll(objectIds); - } + // List attachments = + // attachmentsMetadata.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - if (sourceObjectIds.isEmpty()) { - fail("Could not fetch object Ids for any attachments"); - } + // for (String attachment : attachments) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open copied link in facet: " + facetName); + // } + // } - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft for facet: " + facetName); - } + // objectIdIndex++; + // } - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // if (!deleteTargetResponse.equals("Entity Deleted")) { + // fail("Could not delete target entity"); + // } + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - } + // @Test + // @Order(55) + // void testCopyLinkUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (52): Copy invalid type of link from one entity to another new entity"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity after copying attachments for facet " + facetName); - } + // if (!editResponse.equals("Entity in draft mode") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not edit source entity or create target entity"); + // } - List> attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // sourceObjectIds.add("incorrectObjectId"); - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // for (String facetName : facet) { + // try { + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error for facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Successfully caught expected error for facet: " + facetName); + // } + // } + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch in facet " + facetName); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - System.out.println("Attachment type and URL validated for facet " + facetName); - - List attachments = - attachmentsMetadata.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (String attachment : attachments) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open copied link in facet: " + facetName); - } - } + // @Test + // @Order(56) + // void testCopyLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println("Test (53): Copy link from a new entity to an existing target entity"); - objectIdIndex++; - } + // List> attachmentsByFacet = new ArrayList<>(); + // String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // attachmentsByFacet.add(new ArrayList<>()); + // } - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - @Test - @Order(57) - void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - System.out.println( - "Test (54): Copy invalid type of link from new entity to existing target entity"); - String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // String linkName = "newsample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - for (int i = 0; i < facet.length; i++) { - String linkName = "newsample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit entities"); - } - for (String facetName : facet) { - List sourceObjectIds = new ArrayList<>(); - sourceObjectIds.add("incorrectObjectId"); - try { - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error for facet: " + facetName); - } catch (IOException e) { - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - } + // sourceObjectIds.clear(); + // for (String facetName : facet) { + // List objectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // sourceObjectIds.addAll(objectIds); + // } - @Test - @Order(58) - void testCopyLinkSuccessNewEntityDraft() throws IOException { - System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); - List> attachmentsByFacet = new ArrayList<>(); - String linkUrl = "https://www.example.com"; - for (int i = 0; i < facet.length; i++) { - attachmentsByFacet.add(new ArrayList<>()); - } + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Ids for any attachments"); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft for facet: " + facetName); + // } - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entities"); - } + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity after copying attachments for facet " + facetName); + // } - sourceObjectIds.clear(); - for (int i = 0; i < facet.length; i++) { - List objectIds = - api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - sourceObjectIds.addAll(objectIds); - } + // List> attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - if (sourceObjectIds.size() != facet.length) { - fail( - "Could not fetch object Ids for all attachments. Expected: " - + facet.length - + ", Found: " - + sourceObjectIds.size()); - } + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft for facet: " + facetName); - } + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch in facet " + facetName); - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + // System.out.println("Attachment type and URL validated for facet " + facetName); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - } + // List attachments = + // attachmentsMetadata.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity after copying attachments for facet " + facetName); - } + // for (String attachment : attachments) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open copied link in facet: " + facetName); + // } + // } - List> attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // objectIdIndex++; + // } - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch in facet " + facetName); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - System.out.println("Attachment type and URL validated for facet " + facetName); - - List attachments = - attachmentsMetadata.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (String attachment : attachments) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open copied link in facet: " + facetName); - } - } + // @Test + // @Order(57) + // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println( + // "Test (54): Copy invalid type of link from new entity to existing target entity"); + // String linkUrl = "https://www.example.com"; - objectIdIndex++; - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - } + // for (int i = 0; i < facet.length; i++) { + // String linkName = "newsample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit entities"); + // } + // for (String facetName : facet) { + // List sourceObjectIds = new ArrayList<>(); + // sourceObjectIds.add("incorrectObjectId"); + // try { + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error for facet: " + facetName); + // } catch (IOException e) { + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // } - @Test - @Order(59) - void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { - System.out.println( - "Test (56): Copy attachments from one entity to another new entity draft mode"); - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(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(); + // @Test + // @Order(58) + // void testCopyLinkSuccessNewEntityDraft() throws IOException { + // System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); + // List> attachmentsByFacet = new ArrayList<>(); + // String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // attachmentsByFacet.add(new ArrayList<>()); + // } - for (int i = 0; i < facet.length; i++) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - copyAttachmentSourceEntity, - srvpath, - postData, - file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - } - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facet[i], 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() == 6) { - String copyResponse; - int i = 0; - for (String facetName : facet) { - if (i != 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } - copyResponse = - api.copyAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - i += 2; - if (copyResponse.equals("Attachments copied successfully")) { - // Fetch copied attachment IDs from target draft - List> copiedMetadataResponse = - api.fetchEntityMetadataDraft( - appUrl, entityName, facetName, copyAttachmentTargetEntity); - List copiedAttachmentIds = - copiedMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (saveEntityResponse.equals("Saved")) { - List> fetchEntityMetadataResponse; - fetchEntityMetadataResponse = - api.fetchEntityMetadataDraft( - 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 entities"); + // } - @Test - @Order(60) - void testViewChangelogForNewlyCreatedAttachment() throws IOException { - System.out.println( - "Test (60): View changelog for newly created attachment in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // Create a new entity for changelog test - changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); - assertNotEquals("Could not create entity", changelogEntityID[i]); - - // 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"); - - // Create attachment - Map postData = new HashMap<>(); - postData.put("up__ID", changelogEntityID[i]); - postData.put("mimeType", "text/plain"); - 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[i], srvpath, postData, file); + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - changelogAttachmentID[i] = createResponse.get(1); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - assertEquals("Attachment created", status, "Attachment should be created successfully"); - assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); - assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); + // sourceObjectIds.clear(); + // for (int i = 0; i < facet.length; i++) { + // List objectIds = + // api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], + // copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // sourceObjectIds.addAll(objectIds); + // } - // Fetch changelog for the newly created attachment - Map changelogResponse = - api.fetchChangelog( - appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + // if (sourceObjectIds.size() != facet.length) { + // fail( + // "Could not fetch object Ids for all attachments. Expected: " + // + facet.length + // + ", Found: " + // + sourceObjectIds.size()); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft for facet: " + facetName); + // } - // 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 subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - @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 in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // 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[i], - changelogAttachmentID[i], - 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[i], - changelogAttachmentID[i], - bodyInt); - assertEquals( - "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + // } - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity after copying attachments for facet " + facetName); + // } - // Edit entity again to fetch changelog - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // List> attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // Fetch changelog after modifications - Map changelogResponse = - api.fetchChangelog( - appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - assertNotNull(changelogResponse, "Changelog response should not be null"); + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch in facet " + facetName); - // 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)"); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + // System.out.println("Attachment type and URL validated for facet " + facetName); - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); + // List attachments = + // attachmentsMetadata.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // 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)"); - - // Save the entity so test 62 can edit it - String saveResponseFinal = - api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); - } - } + // for (String attachment : attachments) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open copied link in facet: " + facetName); + // } + // } - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println( - "Test (62): Rename attachment and verify changelog increases with rename entry in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // Edit entity to put it in draft mode (entity was saved at end of test 61) - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - 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[i], - changelogAttachmentID[i], - newFileName); - assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); - - // Save entity after rename - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - - // Edit entity again and fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // Fetch changelog after rename - Map changelogAfterRename = - api.fetchChangelog( - appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - 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"); + // objectIdIndex++; + // } - @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[i]); - } - } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // } - @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 in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // 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.pdf").getFile()); - assertTrue(file.exists(), "Sample file should exist"); - - // 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, 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"); + // @Test + // @Order(59) + // void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { + // System.out.println( + // "Test (56): Copy attachments from one entity to another new entity draft mode"); + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(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"); - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // sourceObjectIds.clear(); + + // for (int i = 0; i < facet.length; i++) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // copyAttachmentSourceEntity, + // srvpath, + // postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // } + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facet[i], 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"); + // } + // } - // 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"); + // if (sourceObjectIds.size() == 6) { + // String copyResponse; + // int i = 0; + // for (String facetName : facet) { + // if (i != 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } + // copyResponse = + // api.copyAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + // i += 2; + // if (copyResponse.equals("Attachments copied successfully")) { + // // Fetch copied attachment IDs from target draft + // List> copiedMetadataResponse = + // api.fetchEntityMetadataDraft( + // appUrl, entityName, facetName, copyAttachmentTargetEntity); + // List copiedAttachmentIds = + // copiedMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (saveEntityResponse.equals("Saved")) { + // List> fetchEntityMetadataResponse; + // fetchEntityMetadataResponse = + // api.fetchEntityMetadataDraft( + // 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); + // } - // Fetch changelog after initial save - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println( + // "Test (60): View changelog for newly created attachment in all three facets"); - assertNotNull(changelogResponse, "Changelog response should not be null"); + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; - // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + - // customProperty2) - assertEquals( - 3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); + // // Create a new entity for changelog test + // changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); + // assertNotEquals("Could not create entity", changelogEntityID[i]); - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); + // // 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"); - // Save entity again without any modifications - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // // Create attachment + // Map postData = new HashMap<>(); + // postData.put("up__ID", changelogEntityID[i]); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // 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"); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, changelogEntityID[i], srvpath, postData, file); + + // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + // String status = createResponse.get(0); + // changelogAttachmentID[i] = createResponse.get(1); + + // assertEquals("Attachment created", status, "Attachment should be created successfully"); + // assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); + // assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); + + // // Fetch changelog for the newly created attachment + // Map changelogResponse = + // api.fetchChangelog( + // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + // 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"); + // } + // } - // Fetch changelog after second save - Map changelogAfterSecondSave = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // @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 in all three facets"); + + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; + + // // 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[i], + // changelogAttachmentID[i], + // 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[i], + // changelogAttachmentID[i], + // bodyInt); + // assertEquals( + // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + + // // Save the entity + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + + // // Edit entity again to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after modifications + // Map changelogResponse = + // api.fetchChangelog( + // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + // 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)"); + + // // Save the entity so test 62 can edit it + // String saveResponseFinal = + // api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + // assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); + // } + // } - assertNotNull( - changelogAfterSecondSave, "Changelog response should not be null after second save"); + // @Test + // @Order(62) + // void testChangelogAfterRenamingAttachment() throws IOException { + // System.out.println( + // "Test (62): Rename attachment and verify changelog increases with rename entry in all + // three facets"); + + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; + + // // Edit entity to put it in draft mode (entity was saved at end of test 61) + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // 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[i], + // changelogAttachmentID[i], + // newFileName); + // assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); + + // // Save entity after rename + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + + // // Edit entity again and fetch changelog + // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after rename + // Map changelogAfterRename = + // api.fetchChangelog( + // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + // 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[i]); + // } + // } - // 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"); + // @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 in all three facets"); - @SuppressWarnings("unchecked") - List> changeLogsAfterSecondSave = - (List>) changelogAfterSecondSave.get("changeLogs"); - assertEquals( - 3, - changeLogsAfterSecondSave.size(), - "Should still have exactly 3 changelog entries after second save"); + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; - // Clean up the 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(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 in all three facets"); + // // 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"); - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; + // // 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 a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - assertNotNull(newEntityID, "Failed to create new entity"); - assertNotEquals("Could not create entity", newEntityID); + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); - // 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"); + // 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); + // } + // } - // 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"); + // @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 in all three facets"); - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - String newAttachmentID = createResponse.get(1); + // // 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); - 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"); + // // 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"); - // Save the entity immediately without any modifications - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // // 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"); - // 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"); + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); - // Save entity again without modifying the attachment - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // 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); + // } + // } - // 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(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println( + // "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // Fetch changelog for the attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // 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 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); - } - } + // 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(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println( - "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } + // 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 saveTargetBeforeMoveTest65 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveTest65.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - // Save target before move - String saveTargetBeforeMoveTest65 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveTest65.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all attachments after move"); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after + // move"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // @Test + // @Order(66) + // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + // System.out.println( + // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - sourceAttachmentIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all attachments after move"); + // for (int i = 0; i < facet.length; i++) { + // 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, facet[i], moveSourceEntity); - assertEquals( - 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after move"); + // 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())); - 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(66) - public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - System.out.println( - "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } + // 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"); - } + // 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"); + + // File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + // List targetCreateResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // srvpath, + // targetPostData, + // duplicateFile); + + // if (!targetCreateResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment on target entity"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - 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"); + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - List targetCreateResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - srvpath, - targetPostData, - duplicateFile); - - if (!targetCreateResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment on target entity"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + + // int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target should have duplicate skipped, other attachments moved"); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // int expectedSourceCount = + // sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); + // assertEquals( + // expectedSourceCount, + // sourceMetadataAfterMove.size(), + // "Source should have duplicate attachment remaining"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // @Test + // @Order(67) + // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + // System.out.println( + // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // 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())); - int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target should have duplicate skipped, other attachments moved"); - - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - int expectedSourceCount = - sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); - assertEquals( - expectedSourceCount, - sourceMetadataAfterMove.size(), - "Source should have duplicate attachment remaining"); + // 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"); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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(67) - public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - System.out.println( - "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update notes for attachment: " + attachmentId); + // } + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // Integer customProperty2Value = 54321; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + // if (!updateCustomPropertyResponse.equals("Updated")) { + // fail("Could not update custom property for attachment: " + attachmentId); + // } + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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 saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update notes for attachment: " + attachmentId); - } - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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); + // } + // } - Integer customProperty2Value = 54321; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - - for (String attachmentId : sourceAttachmentIds) { - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], 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"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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 target before move + // String saveTargetBeforeMoveTest67 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveTest67.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all attachments after move"); + + // 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, facet[i], moveTargetEntity, + // targetAttachmentId); + + // 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 target before move - String saveTargetBeforeMoveTest67 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveTest67.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); - } + // 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); + // } + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - sourceAttachmentIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all attachments after move"); - - 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, facet[i], moveTargetEntity, targetAttachmentId); - - 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); - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - 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(68) + // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (68): Move valid attachments from Source Entity to Target Entity without + // sourceFacet"); - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // 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(68) - public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - System.out.println( - "Test (68): Move valid attachments from Source Entity to Target Entity 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"); - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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 saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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()); + // } + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - 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()); - } - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not 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"); + // } - // 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 targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // moveObjectIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all moved attachments"); + + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // String readResponse = + // api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read moved attachment from target entity"); + // } + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - moveObjectIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all moved attachments"); - - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - String readResponse = - api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read moved attachment from target entity"); - } - } + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // moveObjectIds.size(), + // sourceMetadataAfterMove.size(), + // "Source entity should still have attachments in UI when sourceFacet is not specified"); + + // for (Map metadata : sourceMetadataAfterMove) { + // String objectId = (String) metadata.get("objectId"); + // assertTrue( + // moveObjectIds.contains(objectId), + // "Source entity should still show attachment with objectId: " + objectId); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - moveObjectIds.size(), - sourceMetadataAfterMove.size(), - "Source entity should still have attachments in UI when sourceFacet is not specified"); - - for (Map metadata : sourceMetadataAfterMove) { - String objectId = (String) metadata.get("objectId"); - assertTrue( - moveObjectIds.contains(objectId), - "Source entity should still show attachment with objectId: " + objectId); - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // @Test + // @Order(69) + // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (69): Move attachments into existing Target Entity when duplicate exists without + // sourceFacet"); - @Test - @Order(69) - public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - System.out.println( - "Test (69): Move attachments into existing Target Entity when duplicate exists without sourceFacet"); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // 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())); - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - 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 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"); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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()); + // } + // } - 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"); + // } - 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"); + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - List createTargetResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - srvpath, - targetPostData, - files.get(0)); - if (!createTargetResponse.get(0).equals("Attachment created")) { - fail("Could not create duplicate attachment in target 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"); + + // List createTargetResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // srvpath, + // targetPostData, + // files.get(0)); + // if (!createTargetResponse.get(0).equals("Attachment created")) { + // fail("Could not create duplicate attachment in target entity"); + // } - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int initialTargetCount = targetMetadataBeforeMove.size(); - - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int initialTargetCount = targetMetadataBeforeMove.size(); + + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int nonDuplicateCount = moveObjectIds.size() - 1; - int expectedTargetCount = initialTargetCount + nonDuplicateCount; + // int nonDuplicateCount = moveObjectIds.size() - 1; + // int expectedTargetCount = initialTargetCount + nonDuplicateCount; - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have initial attachments plus non-duplicate moved attachments"); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target entity should have initial attachments plus non-duplicate moved attachments"); - assertTrue( - targetMetadataAfterMove.size() > initialTargetCount, - "Target should have more attachments after move (non-duplicates added)"); + // assertTrue( + // targetMetadataAfterMove.size() > initialTargetCount, + // "Target should have more attachments after move (non-duplicates added)"); - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - moveObjectIds.size(), - sourceMetadataAfterMove.size(), - "Source entity should still have all attachments in UI when sourceFacet is not specified"); + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // moveObjectIds.size(), + // sourceMetadataAfterMove.size(), + // "Source entity should still have all attachments in UI when sourceFacet is not + // specified"); - 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); - } + // 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); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - @Test - @Order(70) - public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - throws Exception { - System.out.println( - "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + // @Test + // @Order(70) + // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + // throws Exception { + // System.out.println( + // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // 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())); - 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, facet[i], 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"); - Integer customProperty2Value = 54321; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - - for (String attachmentId : sourceAttachmentIds) { - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); - if (!updateCustomPropertyResponse.equals("Updated")) { - fail("Could not update custom property for attachment: " + attachmentId); - } - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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 saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update notes for attachment: " + attachmentId); + // } + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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); - } - } + // Integer customProperty2Value = 54321; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], 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"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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); + // } + // } - 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"); - } + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have " + expectedTargetCount + " attachments after move"); - - 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, facet[i], moveTargetEntity, targetAttachmentId); - - 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 target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - 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); - } - } + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - sourceCountBeforeMove, - sourceMetadataAfterMove.size(), - "Source entity should still have " - + sourceCountBeforeMove - + " attachments (without sourceFacet)"); - - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target entity should have " + expectedTargetCount + " attachments after move"); + + // 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, facet[i], moveTargetEntity, + // targetAttachmentId); + + // 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); + // } - @Test - @Order(71) - public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - System.out.println( - "Test (71): Move attachments with invalid or undefined secondary properties"); + // 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); + // } + // } - for (int i = 0; i < facet.length; i++) { - 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, facet[i], moveSourceEntity); + // assertEquals( + // sourceCountBeforeMove, + // sourceMetadataAfterMove.size(), + // "Source entity should still have " + // + sourceCountBeforeMove + // + " attachments (without sourceFacet)"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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(71) + // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + // System.out.println( + // "Test (71): Move attachments with invalid or undefined secondary properties"); - String validAttachmentId = sourceAttachmentIds.get(0); - Integer validCustomProperty2Value = 12345; - RequestBody validPropertyBody = - RequestBody.create( - "{\"customProperty2\": " + validCustomProperty2Value + "}", - MediaType.parse("application/json")); - - String validPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, validPropertyBody); - if (!validPropertyResponse.equals("Updated")) { - fail("Could not update valid property for attachment: " + validAttachmentId); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - String invalidAttachmentId = sourceAttachmentIds.get(1); - RequestBody invalidPropertyBody = - RequestBody.create( - "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); - - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, invalidPropertyBody); - - String undefinedAttachmentId = sourceAttachmentIds.get(2); - RequestBody undefinedPropertyBody = - RequestBody.create( - "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", - MediaType.parse("application/json")); - - api.updateSecondaryProperty( - appUrl, - entityName, - facet[i], - moveSourceEntity, - undefinedAttachmentId, - undefinedPropertyBody); - - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // 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())); - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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); - } - } + // 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"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // String validAttachmentId = sourceAttachmentIds.get(0); + // Integer validCustomProperty2Value = 12345; + // RequestBody validPropertyBody = + // RequestBody.create( + // "{\"customProperty2\": " + validCustomProperty2Value + "}", + // MediaType.parse("application/json")); + + // String validPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, + // validPropertyBody); + // if (!validPropertyResponse.equals("Updated")) { + // fail("Could not update valid property for attachment: " + validAttachmentId); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String invalidAttachmentId = sourceAttachmentIds.get(1); + // RequestBody invalidPropertyBody = + // RequestBody.create( + // "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); + + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, + // invalidPropertyBody); + + // String undefinedAttachmentId = sourceAttachmentIds.get(2); + // RequestBody undefinedPropertyBody = + // RequestBody.create( + // "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", + // MediaType.parse("application/json")); + + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facet[i], + // moveSourceEntity, + // undefinedAttachmentId, + // undefinedPropertyBody); + + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Save target before move - String saveTargetBeforeMoveResponseTest72 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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); + // } + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - assertTrue( - targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - assertEquals( - sourceCountBeforeMove, - targetMetadataAfterMove.size(), - "All attachments should move (invalid properties are ignored)"); - - 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, facet[i], moveTargetEntity, targetAttachmentId); - - if (detailedMetadata.containsKey("customProperty2") - && detailedMetadata.get("customProperty2") != null) { - assertEquals( - validCustomProperty2Value, - detailedMetadata.get("customProperty2"), - "Valid customProperty2 should be preserved"); - } - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterMove.size(), - "Source entity should have no attachments after move with sourceFacet"); + // // Save target before move + // String saveTargetBeforeMoveResponseTest72 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - @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"); + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + + // assertTrue( + // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after + // move"); + // assertEquals( + // sourceCountBeforeMove, + // targetMetadataAfterMove.size(), + // "All attachments should move (invalid properties are ignored)"); + + // 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, facet[i], moveTargetEntity, + // targetAttachmentId); + + // if (detailedMetadata.containsKey("customProperty2") + // && detailedMetadata.get("customProperty2") != null) { + // assertEquals( + // validCustomProperty2Value, + // detailedMetadata.get("customProperty2"), + // "Valid customProperty2 should be preserved"); + // } + // } - for (int i = 0; i < facet.length; i++) { - 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, facet[i], moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterMove.size(), + // "Source entity should have no attachments after move with sourceFacet"); - 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - 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); - } + // @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"); - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // for (int i = 0; i < facet.length; i++) { + // 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"); - } + // 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())); - assertNotNull(moveSourceFolderId, "Source folder ID 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"); - 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"); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target 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); + // } - // Save target before move - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertTrue( - targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - assertEquals( - sourceCountBeforeMove, - targetMetadataAfterMove.size(), - "Target should have " + sourceCountBeforeMove + " attachments after move"); - - 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()); - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - String saveSourceAfterMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceAfterMoveResponse.equals("Saved")) { - fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); - } + // 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"); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], 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()); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // // Save target before move + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - @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"); + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertTrue( + // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after + // move"); + // assertEquals( + // sourceCountBeforeMove, + // targetMetadataAfterMove.size(), + // "Target should have " + sourceCountBeforeMove + " attachments after move"); + + // 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()); + // } - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // String saveSourceAfterMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceAfterMoveResponse.equals("Saved")) { + // fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); + // } - 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> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], 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()); + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in source entity"); - } + // 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"); - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - List> metadataBeforeRename = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); - assertEquals( - "sample.txt", - metadataBeforeRename.get(0).get("fileName"), - "Original filename should be sample.txt"); - - String editSourceResponse = - api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!editSourceResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity to draft mode"); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - String newFileName = "testEdited.txt"; - String renameResponse = - api.renameAttachment( - appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); - assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); + // 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"); - saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after rename: " + saveSourceResponse); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in source entity"); + // } - List> metadataAfterRename = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); - assertEquals( - newFileName, - metadataAfterRename.get(0).get("fileName"), - "Filename should be updated to " + newFileName); - - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); - moveObjectIds.add(objectId); - - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String attachmentId = createResponse.get(1); + // assertNotNull(attachmentId, "Attachment ID should not be null"); - // Save target before move - String saveTargetBeforeMoveResponseTest73 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> metadataBeforeRename = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); + // assertEquals( + // "sample.txt", + // metadataBeforeRename.get(0).get("fileName"), + // "Original filename should be sample.txt"); + + // String editSourceResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!editSourceResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity to draft mode"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); + // String newFileName = "testEdited.txt"; + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); + // assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterMove.size(), - "Source entity should have no attachments after move with sourceFacet"); + // saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after rename: " + saveSourceResponse); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // List> metadataAfterRename = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); + // assertEquals( + // newFileName, + // metadataAfterRename.get(0).get("fileName"), + // "Filename should be updated to " + newFileName); + + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target 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"); + // // Save target before move + // String saveTargetBeforeMoveResponseTest73 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterMove.size(), + // "Source entity should have no attachments after move with sourceFacet"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // @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"); - int sourceCountInitial = sourceAttachmentIds.size(); - assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // for (int i = 0; i < facet.length; i++) { + // 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"); - } + // 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(moveSourceFolderId, "Source folder ID 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"); - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity 1"); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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 target1 before move - String saveTarget1BeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTarget1BeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity 1 before move"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult1 = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult1 == null) { - fail("Move operation from source to target 1 returned null result"); - } + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - List> target1MetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertTrue( - target1MetadataAfterMove.size() > 0, - "Target entity 1 should have attachments after move"); - assertEquals( - sourceCountInitial, - target1MetadataAfterMove.size(), - "Target 1 should have " + sourceCountInitial + " attachments"); - - 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()); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - List> sourceMetadataAfterFirstMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterFirstMove.size(), - "Source entity should have no attachments after move to target 1"); + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity2.equals("Could not create entity")) { - fail("Could not create target entity 2"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity 1"); + // } - // 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"); - } + // // 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"); + // } - List target1AttachmentIds = new ArrayList<>(); - for (Map metadata : target1MetadataAfterMove) { - String attachmentId = metadata.get("ID").toString(); - target1AttachmentIds.add(attachmentId); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult1 = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult1 == null) { + // fail("Move operation from source to target 1 returned null result"); + // } - moveObjectIds = new ArrayList<>(); - String target1FolderId = null; - for (String attachmentId : target1AttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - 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()); - } - } + // List> target1MetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertTrue( + // target1MetadataAfterMove.size() > 0, + // "Target entity 1 should have attachments after move"); + // assertEquals( + // sourceCountInitial, + // target1MetadataAfterMove.size(), + // "Target 1 should have " + sourceCountInitial + " attachments"); + + // 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()); + // } - assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); - - Map moveResult2 = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity2, - target1FolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult2 == null) { - fail("Move operation from target 1 to target 2 returned null result"); - } + // List> sourceMetadataAfterFirstMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterFirstMove.size(), + // "Source entity should have no attachments after move to target 1"); - List> target2MetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); - assertTrue( - target2MetadataAfterMove.size() > 0, - "Target entity 2 should have attachments after move"); - assertEquals( - sourceCountInitial, - target2MetadataAfterMove.size(), - "Target 2 should have " + sourceCountInitial + " attachments"); - - 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()); - } + // String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity2.equals("Could not create entity")) { + // fail("Could not create target entity 2"); + // } - List> target1MetadataAfterSecondMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - 0, - target1MetadataAfterSecondMove.size(), - "Target entity 1 should have no attachments after move to target 2"); + // // 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"); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity2); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // List target1AttachmentIds = new ArrayList<>(); + // for (Map metadata : target1MetadataAfterMove) { + // String attachmentId = metadata.get("ID").toString(); + // target1AttachmentIds.add(attachmentId); + // } - @Test - @Order(75) - public void testMoveAttachmentsWithoutSDMRole() throws Exception { - System.out.println("Test (75): Move attachments when user does not have SDM Role"); + // moveObjectIds = new ArrayList<>(); + // String target1FolderId = null; + // for (String attachmentId : target1AttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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()); + // } + // } - for (int i = 0; i < facet.length; i++) { - 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"); + + // Map moveResult2 = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity2, + // target1FolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult2 == null) { + // fail("Move operation from target 1 to target 2 returned null result"); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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> target2MetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); + // assertTrue( + // target2MetadataAfterMove.size() > 0, + // "Target entity 2 should have attachments after move"); + // assertEquals( + // sourceCountInitial, + // target2MetadataAfterMove.size(), + // "Target 2 should have " + sourceCountInitial + " attachments"); + + // 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()); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // List> target1MetadataAfterSecondMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // 0, + // target1MetadataAfterSecondMove.size(), + // "Target entity 1 should have no attachments after move to target 2"); - int sourceCountInitial = sourceAttachmentIds.size(); - assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity2); + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // @Test + // @Order(75) + // public void testMoveAttachmentsWithoutSDMRole() throws Exception { + // System.out.println("Test (75): Move attachments when user does not have SDM Role"); - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity with no SDM role"); - } + // 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 = - apiNoRoles.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"); - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = null; - boolean moveOperationFailed = false; - String errorMessage = null; - - try { - moveResult = - apiNoRoles.moveAttachment( - appUrl, - entityName, - facet[i], - 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(); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - 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); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - sourceCountInitial, - sourceMetadataAfterMove.size(), - "Source should still have all attachments after failed move"); + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed move"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - @Test - @Order(76) - void testRenameAttachmentWithExtensionChange() throws IOException { - System.out.println( - "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); + // 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 1: Create a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (newEntityID.equals("Could not create entity")) { - fail("Could not create entity"); - } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!saveResponse.equals("Saved")) { - fail("Could not save new entity: " + saveResponse); - } + // // Save target before move + // String saveTargetBeforeMoveResponse = + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - // Step 2: Upload a PDF attachment to each facet - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = null; + // boolean moveOperationFailed = false; + // String errorMessage = null; + + // try { + // moveResult = + // apiNoRoles.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // 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(); + // } - 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"); + // 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); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // sourceCountInitial, + // sourceMetadataAfterMove.size(), + // "Source should still have all attachments after failed move"); + + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed + // move"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!"Entity in draft mode".equals(editResponse)) { - fail("Could not put entity in draft mode for PDF upload"); - } + // @Test + // @Order(76) + // void testRenameAttachmentWithExtensionChange() throws IOException { + // System.out.println( + // "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - + // should return extension change warning"); - String[] facetAttachmentIDs = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - facetAttachmentIDs[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - if (facetAttachmentIDs[i] == null) { - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not upload sample.pdf to facet: " + facet[i]); - } - } + // // Step 1: Create a new entity + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (newEntityID.equals("Could not create entity")) { + // fail("Could not create entity"); + // } + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save new entity: " + saveResponse); + // } - // Step 3: Save the entity - String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!savedAfterUpload.equals("Saved")) { - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not save entity after PDF upload: " + savedAfterUpload); - } + // // Step 2: Upload a PDF attachment to each facet + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // 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"); + + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!"Entity in draft mode".equals(editResponse)) { + // fail("Could not put entity in draft mode for PDF upload"); + // } - // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> .txt - for (int i = 0; i < facet.length; i++) { - String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!"Entity in draft mode".equals(editDraftResponse)) { - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not put entity in draft mode for rename on facet: " + facet[i]); - } + // String[] facetAttachmentIDs = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // facetAttachmentIDs[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + // if (facetAttachmentIDs[i] == null) { + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not upload sample.pdf to facet: " + facet[i]); + // } + // } - String renameResponse = - api.renameAttachment( - appUrl, - entityName, - facet[i], - newEntityID, - facetAttachmentIDs[i], - "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - } + // // Step 3: Save the entity + // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!savedAfterUpload.equals("Saved")) { + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not save entity after PDF upload: " + savedAfterUpload); + // } - // Step 6: Save and validate the extension change warning message - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + // // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> + // .txt + // for (int i = 0; i < facet.length; i++) { + // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!"Entity in draft mode".equals(editDraftResponse)) { + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not put entity in draft mode for rename on facet: " + facet[i]); + // } - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + // String renameResponse = + // api.renameAttachment( + // appUrl, + // entityName, + // facet[i], + // newEntityID, + // facetAttachmentIDs[i], + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + // } - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } + // // Step 6: Save and validate the extension change warning message + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - } + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + // } - // Clean up - api.deleteEntity(appUrl, entityName, newEntityID); - } + // // Clean up + // api.deleteEntity(appUrl, entityName, newEntityID); + // } - @Test - @Order(77) - void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { - System.out.println( - "Test (77) : Upload attachment in draft, rename changing extension before save across all facets - should return extension change warning"); + // @Test + // @Order(77) + // void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { + // System.out.println( + // "Test (77) : Upload attachment in draft, rename changing extension before save across all + // facets - should return extension change warning"); - for (int i = 0; i < facet.length; i++) { - // Step 1: Create a new entity draft (do NOT save it yet) - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (newEntityID.equals("Could not create entity")) { - fail("Could not create entity for facet: " + facet[i]); - } + // for (int i = 0; i < facet.length; i++) { + // // Step 1: Create a new entity draft (do NOT save it yet) + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (newEntityID.equals("Could not create entity")) { + // fail("Could not create entity for facet: " + facet[i]); + // } - // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - 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"); - - String facetAttachmentID = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - if (facetAttachmentID == null) { - api.deleteEntityDraft(appUrl, entityName, newEntityID); - fail("Could not upload sample.pdf to facet: " + facet[i]); - } + // // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not saved - String renameResponse = - api.renameAttachment( - appUrl, entityName, facet[i], newEntityID, facetAttachmentID, "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.deleteEntityDraft(appUrl, entityName, newEntityID); - fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - } + // 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"); - // Step 4: Save — should receive extension change warning, not "Saved" - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + // String facetAttachmentID = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + // if (facetAttachmentID == null) { + // api.deleteEntityDraft(appUrl, entityName, newEntityID); + // fail("Could not upload sample.pdf to facet: " + facet[i]); + // } - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + // // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not + // saved + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facet[i], newEntityID, facetAttachmentID, + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.deleteEntityDraft(appUrl, entityName, newEntityID); + // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + // } - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } + // // Step 4: Save — should receive extension change warning, not "Saved" + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - - // Clean up - api.deleteEntity(appUrl, entityName, newEntityID); - } - } + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + + // // Clean up + // api.deleteEntity(appUrl, entityName, newEntityID); + // } + // } @Test @Order(78) From 51974ee1b217ca4ea914db4c1f9827d3abb139dc Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 11:51:29 +0530 Subject: [PATCH 04/13] Commenting other tests --- ...ntegrationTest_Chapters_MultipleFacet.java | 12337 ++++++++-------- .../cds/sdm/IntegrationTest_SingleFacet.java | 11440 +++++++------- 2 files changed, 12029 insertions(+), 11748 deletions(-) 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 edabcc985..f0554780d 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 @@ -7,13 +7,8 @@ import java.io.File; import java.io.IOException; 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.*; @@ -472,6109 +467,6235 @@ void testCreateNewBookWithChapterAndAttachments() throws IOException { } } - @Test - @Order(7) - void testRenameChapterAttachments() { - System.out.println("Test (7) : Rename single attachment, reference, and footnote in chapter"); - Boolean testStatus = true; - - try { - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - if ("Entity in draft mode".equals(response)) { - String[] name = {"sample123", "reference123", "footnote123"}; - for (int i = 0; i < facet.length; i++) { - // Read the facet to ensure it exists - response = - api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); - if (!"Renamed".equals(response)) { - testStatus = false; - System.out.println(facet[i] + " was not renamed: " + response); - } - } - // Save book draft if everything is renamed - if (testStatus) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if (!"Saved".equals(response)) { - testStatus = false; - System.out.println("Book draft was not saved: " + response); - } - } else { - // Attempt save despite potential rename failures - api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - } - } else { - testStatus = false; - System.out.println("Book was not put into draft mode: " + response); - } - } catch (Exception e) { - testStatus = false; - System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); - } - - if (!testStatus) { - fail("There was an error during the rename test process for chapter."); - } - } - - @Test - @Order(8) - void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { - System.out.println("Test (8): Create chapter attachments with unsupported characters"); - 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("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, bookEntityName, srvpath, bookID); - if (!"Entity in draft mode".equals(response)) { - fail("Book not in draft mode: " + response); - return; - } - - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", chapterID); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); - - if (!"Attachment created".equals(createResponse.get(0))) { - fail("Could not create attachment in chapter facet: " + facet[i]); - return; - } - - String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE - response = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); - - System.out.println("Rename response for chapter " + facet[i] + ": " + response); - } - - // Save should fail - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - // ---------------- PARSE JSON ---------------- - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - String message = root.path("error").path("message").asText(); - - // ---------------- NORMALIZE MESSAGE ---------------- - // 1. Normalize smart quotes - // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared - message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); - - // ---------------- EXPECTED MESSAGE (EXACT) ---------------- - String expectedMessage = - "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" - + "Table: attachments\n" - + "Page: IntegrationTestEntity"; - - if (message.equals(expectedMessage)) { - - for (int i = 0; i < facet.length; i++) { - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if ("Saved".equals(response)) { - testStatus = true; - } - } - - if (!testStatus) { - fail("Test for unsupported characters in chapter attachments failed"); - } - } - - @Test - @Order(9) - void testRenameSingleDuplicateInChapter() throws IOException { - System.out.println( - "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); - Boolean testStatus = false; - int counter = 0; - - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Edit entity response: " + response); - - if ("Entity in draft mode".equals(response)) { - // To create a duplicate within the same facet, we need to rename ID2[i] to - // the same name as an existing file in that facet. After test 7, the existing files are: - // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) - // We rename ID2[i] (sample123.txt from test 8) to these names which already exist - String[] duplicateNames = {"sample123", "reference123", "footnote123"}; - String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; - - // Try to rename to duplicate file names (names that already exist in each facet) - for (int i = 0; i < facet.length; i++) { - response = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); - System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); - if ("Renamed".equals(response)) { - counter++; - } - } - System.out.println("Renamed count: " + counter); - - if (counter == facet.length) { - // Try to save - should fail with duplicate error - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Save response (expecting error): " + response); - - // Parse JSON response to check for duplicate error - ObjectMapper mapper = new ObjectMapper(); - try { - JsonNode root = mapper.readTree(response); - String message = root.path("error").path("message").asText(); - - if (message.contains("already exists")) { - System.out.println("Duplicate error detected as expected: " + message); - counter = 0; - // Rename with valid different names - for (int i = 0; i < facet.length; i++) { - response = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); - System.out.println("Rename " + facet[i] + " to valid name: " + response); - if ("Renamed".equals(response)) { - counter++; - } - } - - if (counter == facet.length) { - // Save should now succeed - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Final save response: " + response); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } else { - System.out.println("Unexpected error message: " + message); - } - } catch (Exception e) { - // Response might not be JSON if save succeeded (shouldn't happen with duplicates) - System.out.println("Response was not JSON error: " + response); - // If save succeeded unexpectedly, we still need to ensure book is saved - if ("Saved".equals(response)) { - System.out.println( - "Save succeeded unexpectedly - duplicates might be in different facets"); - } - } - } - } else { - System.out.println("Book was not put into draft mode: " + response); - } - - if (!testStatus) { - fail("Duplicate rename test failed for chapter"); - } - } - - @Test - @Order(10) - void testRenameToValidateNamesInChapter() throws IOException { - System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); - Boolean testStatus = false; - - // Create a new book and chapter for this test - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - bookID3 = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); - if (!"Could not create entity".equals(chapterResponse)) { - chapterID3 = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID3); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); - } - - String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; - - boolean allRenamed = true; - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); - if (!"Renamed".equals(response1)) { - allRenamed = false; - System.out.println( - "Failed to rename " - + facet[i] - + " to valid name " - + validNames[i] - + ": " - + response1); - } - } - - if (allRenamed) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } - } - - if (!testStatus) { - fail("Could not rename chapter attachments to valid names"); - } - } - - @Test - @Order(11) - void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { - System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); - boolean testStatus = true; - - try { - String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Edit entity response: " + response); - - if (response.equals("Entity in draft mode")) { - String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; - for (int i = 0; i < facet.length; i++) { - response = - apiNoRoles.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); - System.out.println("Rename response for " + facet[i] + ": " + response); - if (!"Renamed".equals(response)) { - testStatus = false; - } - } - - if (testStatus) { - // Save should fail with permission error - response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Save response (expecting permission error): " + response); - - // The expected error should indicate no permissions to update - String expected = - "[{\"code\":\"\",\"message\":\"Could not update the following files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - - // Check if response contains permission error - if (!response.equals(expected) - && !response.contains("do not have the required permissions")) { - System.out.println("Expected permission error but got: " + response); - testStatus = false; - } else { - System.out.println("Got expected permission error"); - } - } else { - // Some renames failed - save to release draft - apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - } - } else { - System.out.println("Could not edit entity: " + response); - testStatus = false; - } - } catch (Exception e) { - System.out.println("Exception: " + e.getMessage()); - testStatus = false; - } - - if (!testStatus) { - fail("Chapter attachment got renamed without SDM roles."); - } - } - - @Test - @Order(12) - void testDeleteSingleChapterAttachment() throws IOException { - System.out.println( - "Test (12) : Delete single attachment, reference, and footnote from chapter"); - Boolean testStatus = false; - int deleteCounter = 0; - - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - if (response.equals("Deleted")) deleteCounter++; - } - if (deleteCounter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if (response.equals("Saved")) { - int verifyCounter = 0; - for (int i = 0; i < facet.length; i++) { - response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - if (response.equals("Could not read Attachment")) verifyCounter++; - } - if (verifyCounter == facet.length) { - testStatus = true; - } else { - fail( - "Could not verify all deleted chapter facets. Verified: " - + verifyCounter - + "/" - + facet.length); - } - } else { - fail("Could not save book after deleting chapter attachments"); - } - } else { - fail( - "Could not delete all chapter attachments. Deleted: " - + deleteCounter - + "/" - + facet.length); - } - } else { - fail("Could not edit book to draft mode"); - } - - if (!testStatus) { - fail("Test failed to delete chapter attachments"); - } - } - - @Test - @Order(13) - void testUploadBlockedMimeTypeToChapter() throws IOException { - System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); - Boolean testStatus = false; - - // Create new book and chapter - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - bookID4 = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); - if (!"Could not create entity".equals(chapterResponse)) { - chapterID4 = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = - new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID4); - postData.put("mimeType", "application/rtf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - boolean allBlocked = true; - for (int i = 0; i < facet.length; i++) { - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID4, 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)) { - allBlocked = false; - System.out.println( - "Chapter facet " - + facet[i] - + " incorrectly accepted blocked mimeType: " - + actualResponse); - } - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); - if ("Saved".equals(response) && allBlocked) { - testStatus = true; - } - } - } - - if (!testStatus) { - fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); - } - } - - @Test - @Order(14) - void testDeleteBookAndChapter() { - System.out.println("Test (14) : Delete book (and its chapters)"); - Boolean testStatus = false; - // Delete books (chapters are deleted automatically as they're composition) - String response = api.deleteEntity(appUrl, bookEntityName, bookID); - String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); - String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); - String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); - if (response.equals("Entity Deleted") - && response2.equals("Entity Deleted") - && response3.equals("Entity Deleted") - && response4.equals("Entity Deleted")) testStatus = true; - if (!testStatus) fail("Could not delete books"); - } - - @Test - @Order(15) - void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { - System.out.println( - "Test (15) : Rename & Update secondary property in chapter before book is saved"); - System.out.println("Creating book and chapter"); - - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (!response.equals("Could not create entity")) { - bookID5 = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); - if (!chapterResponse.equals("Could not create entity")) { - chapterID5 = chapterResponse; - - System.out.println("Creating attachment, reference, and footnote in chapter"); - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID5); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - boolean allCreated = true; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); - if (tempID[i] == null || tempID[i].isEmpty()) { - System.out.println("Failed to create attachment for facet: " + facet[i]); - allCreated = false; - } - } - - System.out.println("Attachments, References, and Footnotes created in chapter"); - System.out.println( - "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + tempID[2]); - - if (!allCreated) { - fail("Could not create all attachments for test 15"); - } - - // Reset counter for this test - counter = 0; - - // Use valid dropdown value for customProperty1 - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; - - for (int i = 0; i < facet.length; i++) { - System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - System.out.println("Rename response for " + facet[i] + ": " + response1); - - // Update customProperty1 (String - dropdown value) - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); - - // Update customProperty2 (Integer) - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - - // Update customProperty5 (DateTime) - using customProperty5 like Books test - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - - // Update customProperty6 (Boolean) - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - // Check all updates succeeded - if ("Renamed".equals(response1) - && "Updated".equals(updateSecondaryPropertyResponse1) - && "Updated".equals(updateSecondaryPropertyResponse2) - && "Updated".equals(updateSecondaryPropertyResponse3) - && "Updated".equals(updateSecondaryPropertyResponse4)) { - counter++; - } else { - System.out.println( - "Update failed for " - + facet[i] - + ": rename=" - + response1 - + ", dropdown=" - + updateSecondaryPropertyResponse1 - + ", int=" - + updateSecondaryPropertyResponse2 - + ", datetime=" - + updateSecondaryPropertyResponse3 - + ", bool=" - + updateSecondaryPropertyResponse4); - } - } - - System.out.println("Counter after all facets: " + counter); - if (counter == facet.length) { - // Save the book (not the chapter) - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - System.out.println("Save response: " + response); - if ("Saved".equals(response)) { - testStatus = true; - } - } else { - System.out.println( - "Counter is less than " + facet.length + ", not saving. Counter: " + counter); - } - } - } - - if (!testStatus) { - fail( - "Could not update secondary properties in chapter before book save. Counter: " + counter); - } - } - - @Test - @Order(16) - void testUploadNAttachmentsToChapter() throws IOException { - System.out.println("Test (16) : Upload N attachments to chapter"); - Boolean testStatus = false; - counter = 0; - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); - - for (int j = 0; j < 5; j++) { - // Create temp file with unique name per iteration - File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); - Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID5); - 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, bookEntityName, srvpath, bookID5); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - String check = facetResponse.get(0); - if (check.equals("Attachment created")) { - counter++; - } else { - System.out.println( - "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - } - } - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if (!response.equals("Saved")) { - System.out.println( - "Failed to save book after creating attachments in chapter: " + response); - } - } else { - System.out.println("Could not edit book draft: " + response); - } - tempFile.delete(); - } - - if (counter == 15) { // 5 iterations * 3 facets - testStatus = true; - } - - if (!testStatus) { - fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); - } - } - - @Test - @Order(17) - void testDiscardDraftWithoutChapterAttachments() { - System.out.println("Test (17) : Discard book draft without chapter attachments"); - Boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - // Create chapter but don't add attachments - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft without chapter attachments was not discarded properly"); - } - } - - @Test - @Order(18) - void testDiscardDraftWithChapterAttachments() throws IOException { - System.out.println("Test (18) : Discard book draft with chapter attachments"); - Boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - // Create chapter - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - 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 chapter - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - String check = facetResponse.get(0); - if (!check.equals("Attachment created")) { - System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - } - } - - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft with chapter attachments was not discarded properly"); - } - } - - @Test - @Order(19) - void testUploadChapterAttachmentWithoutSDMRole() throws IOException { - System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); - Boolean testStatus = true; - - String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - try { - List createResponse = - apiNoRoles.createAttachment( - appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); - String check = createResponse.get(0); - - if (check.equals("Attachment created")) { - testStatus = false; - } - } catch (Exception e) { - // Expected to fail - testStatus = true; - } - - apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - } - } - - if (!testStatus) { - fail("Chapter attachment was uploaded without SDM roles"); - } - } - - @Test - @Order(20) - void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { - System.out.println( - "Test (20): Rename & Update secondary property in chapter after book is saved"); - Boolean testStatus = false; - counter = 0; // Reset counter for this test - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - System.out.println("Editing book, response: " + response); - - if (response.equals("Entity in draft mode")) { - // Use unique names that won't conflict with existing attachments - String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; - Integer secondaryPropertyInt = 42; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - System.out.println("Renaming and updating secondary properties for chapter attachment"); - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - // Get the first attachment ID from the chapter - try { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); - if (!metadata.isEmpty()) { - tempID[i] = (String) metadata.get(0).get("ID"); - } - } catch (IOException e) { - fail("Could not fetch metadata for chapter: " + e.getMessage()); - } - - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if (response.equals("Saved")) { - testStatus = true; - System.out.println("Renamed & updated Secondary properties for chapter attachment"); - } - } - } - if (!testStatus) fail("Could not update secondary properties in chapter after book is saved"); - } - - @Test - @Order(21) - void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { - System.out.println( - "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); - System.out.println("Creating book and chapter"); - Boolean testStatus = false; - int localCounter = 0; - int createCounter = 0; - - // Create new book and chapter for this test - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - if (tempID[i] != null) { - createCounter++; - } - } - - // Only proceed if all facets were created successfully - if (createCounter == facet.length) { - // Prepare test data - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testid"; - - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - localCounter++; - } - } - - if (localCounter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - // Fetch metadata and verify values weren't updated due to invalid property - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - - // Parse JSON response and check for expected error messages - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved with expected invalid property errors"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for chapter attachment is unsuccessful"); - } - } else { - System.out.println( - "Not all facets updated successfully. localCounter: " + localCounter); - } - } else { - System.out.println( - "Not all facets created successfully. createCounter: " + createCounter); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, tempBookID); - } - } - if (!testStatus) - fail("Could not update invalid secondary property in chapter before book is saved"); - } - - @Test - @Order(22) - void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { - System.out.println( - "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); - System.out.println("Creating book and chapter"); - Boolean testStatus = false; - int localCounter = 0; - int createCounter = 0; - - // Create new book and chapter - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - if (tempID[i] != null) { - createCounter++; - } - } - - // Only proceed if all facets were created successfully - if (createCounter != facet.length) { - api.deleteEntity(appUrl, bookEntityName, tempBookID); - fail("Not all facets created successfully. createCounter: " + createCounter); - } - - // Save the book first - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (!response.equals("Saved")) { - api.deleteEntity(appUrl, bookEntityName, tempBookID); - fail("Could not save book initially"); - } - - // Now edit to update with invalid property - response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (response.equals("Entity in draft mode")) { - String name1 = "sample.pdf"; - Integer secondaryPropertyInt = 12; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testidinvalid"; - - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - localCounter++; - } - } - - if (localCounter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - - // Parse JSON response and check for expected error messages - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved with expected invalid property errors"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for chapter attachment is unsuccessful"); - } - } else { - System.out.println( - "Not all facets updated successfully. localCounter: " + localCounter); - } - } - api.deleteEntity(appUrl, bookEntityName, tempBookID); - } - } - if (!testStatus) - fail("Could not update invalid secondary property in chapter after book is saved"); - } - - @Test - @Order(23) - void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { - System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); - - boolean testStatus = false; - - // Reuse bookID5 and chapterID5 - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - - if (response.equals("Entity in draft mode")) { - ClassLoader classLoader = getClass().getClassLoader(); - - // Use temp files with unique names to avoid duplicate name errors - File originalPdf = - new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - File originalTxt = - new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); - - File file1 = File.createTempFile("test23_pdf_", ".pdf"); - File file2 = File.createTempFile("test23_txt_", ".txt"); - Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData1 = new HashMap<>(); - postData1.put("up__ID", chapterID5); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - Map postData2 = new HashMap<>(postData1); - postData2.put("up__ID", chapterID5); - postData2.put("mimeType", "text/plain"); - - boolean allCreated = true; - String[] tempID1 = new String[facet.length]; - String[] tempID2 = new String[facet.length]; - - for (int i = 0; i < facet.length; i++) { - List response1 = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); - List response2 = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); - - if (response1.get(0).equals("Attachment created") - && response2.get(0).equals("Attachment created")) { - tempID1[i] = response1.get(1); // to keep one - tempID2[i] = response2.get(1); // will delete this one - } else { - System.out.println("Failed to create attachments for facet " + facet[i]); - System.out.println("Response 1: " + response1.get(0)); - System.out.println("Response 2: " + response2.get(0)); - allCreated = false; - break; - } - - String deleteResponse = - api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); - if (!"Deleted".equals(deleteResponse)) { - allCreated = false; - break; - } - } - - file1.delete(); - file2.delete(); - - if (allCreated) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } else { - System.out.println("Could not edit book: " + response); - } - - if (!testStatus) { - fail("Failed to upload multiple chapter facet entries, delete one per facet and save book"); - } - } - - @Test - @Order(24) - void testUpdateChapterEntityDraft() throws IOException { - System.out.println("Test (24): Update chapter in book draft with new facet content"); - boolean testStatus = false; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - // Use unique temp file name to avoid duplicates - File tempFile = File.createTempFile("test24_sample_", ".pdf"); - Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID5); - 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, bookEntityName, srvpath, bookID5); - if (response.equals("Entity in draft mode")) { - boolean allCreated = true; - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - String check = facetResponse.get(0); - if (!check.equals("Attachment created")) { - allCreated = false; - System.out.println( - "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - } - } - - if (allCreated) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } else { - System.out.println("Could not edit book: " + response); - } - - tempFile.delete(); - - if (!testStatus) { - fail("Failed to update chapter entity draft with new attachments"); - } - } - - @Test - @Order(25) - void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - throws IOException { - System.out.println( - "Test (25): Rename & Update secondary properties for multiple chapter attachments after book is saved"); - System.out.println("Creating book and chapter with multiple attachments"); - - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!"Could not create entity".equals(chapterResponse)) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create PDF attachments - postData.put("mimeType", "application/pdf"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - String[] pdfID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - pdfID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create TXT attachments - postData.put("mimeType", "application/txt"); - file = new File(classLoader.getResource("sample.txt").getFile()); - String[] txtID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - txtID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create EXE attachments - postData.put("mimeType", "application/exe"); - file = new File(classLoader.getResource("sample.exe").getFile()); - String[] exeID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - exeID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Save book first - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (!"Saved".equals(response)) { - fail("Could not save book initially"); - } - - // Edit book to update chapter attachments - response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (response.equals("Entity in draft mode")) { - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; - - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // Update PDF properties - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String renameResp = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - String upd3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - String upd4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - - if ("Renamed".equals(renameResp) - && "Updated".equals(upd1) - && "Updated".equals(upd2) - && "Updated".equals(upd3) - && "Updated".equals(upd4)) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - - // Update TXT properties (only boolean) - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - String upd = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - if ("Updated".equals(upd)) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } - - // Update EXE properties (dropdown and int) - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValueExe = integrationTestUtils.getDropDownValue(); - String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - for (int i = 0; i < facet.length; i++) { - RequestBody bodyDropdownExe = - RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - RequestBody bodyIntExe = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - - if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (response.equals("Saved")) { - System.out.println("Book saved"); - testStatus = true; - System.out.println("Renamed & updated Secondary properties for chapter attachments"); - } - } - } - api.deleteEntity(appUrl, bookEntityName, tempBookID); - } - } - if (!testStatus) { - fail("Could not update secondary property in chapter after book is saved"); - } - } - - @Test - @Order(26) - void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() - throws IOException { - System.out.println( - "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter facets before book is saved"); - System.out.println("Creating book and chapter"); - - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (!"Could not create entity".equals(response)) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!"Could not create entity".equals(chapterResponse)) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create PDF attachments - postData.put("mimeType", "application/pdf"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - String[] pdfID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - pdfID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create TXT attachments - postData.put("mimeType", "application/txt"); - file = new File(classLoader.getResource("sample.txt").getFile()); - String[] txtID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - txtID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create EXE attachments - postData.put("mimeType", "application/exe"); - file = new File(classLoader.getResource("sample.exe").getFile()); - String[] exeID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - exeID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; - - String name1 = "sample1234.pdf"; - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - - // Update PDF properties - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String renameResp = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - String upd3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - String upd4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - String updInvalid = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], invalidPropertyPDF); - - if ("Renamed".equals(renameResp) - && "Updated".equals(upd1) - && "Updated".equals(upd2) - && "Updated".equals(upd3) - && "Updated".equals(upd4) - && "Updated".equals(updInvalid)) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - - // Update TXT properties - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - String upd = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - if ("Updated".equals(upd)) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } - - // Update EXE properties - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValueExe = integrationTestUtils.getDropDownValue(); - String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - for (int i = 0; i < facet.length; i++) { - RequestBody bodyDropdownExe = - RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - RequestBody bodyIntExe = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - - if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - - // Verify PDF metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); - assertEquals(expectedNames[0], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertNull(metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } - - // Verify TXT metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); - assertEquals(expectedNames[1], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertTrue((Boolean) metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } - - // Verify EXE metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); - assertEquals(expectedNames[2], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); - assertEquals(1234, metadata.get("customProperty2")); - } - - // Parse JSON response and check for expected error messages - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved with expected invalid property errors"); - testStatus = true; - System.out.println( - "Rename & update unsuccessful for invalid properties and successful for valid attachments"); - } - } - } - } - - if (!testStatus) { - fail("Could not update secondary property before book is saved"); - } - } - - @Test - @Order(27) - void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - throws IOException { - System.out.println( - "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter attachments after book is saved"); - - // Reuse bookID5 and chapterID5 - System.out.println("Editing book with bookID5: " + bookID5); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - System.out.println("Edit entity response: " + response); - - if (response.equals("Entity in draft mode")) { - // Fetch existing attachments from the chapter - List> attachmentsMeta = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); - List> referencesMeta = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); - List> footnotesMeta = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); - - System.out.println("Attachments count: " + attachmentsMeta.size()); - System.out.println("References count: " + referencesMeta.size()); - System.out.println("Footnotes count: " + footnotesMeta.size()); - - if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) { - String[] pdfID = new String[facet.length]; - String[] txtID = new String[facet.length]; - String[] exeID = new String[facet.length]; - - pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); - pdfID[1] = (String) referencesMeta.get(0).get("ID"); - pdfID[2] = (String) footnotesMeta.get(0).get("ID"); - - txtID[0] = (String) attachmentsMeta.get(1).get("ID"); - txtID[1] = (String) referencesMeta.get(1).get("ID"); - txtID[2] = (String) footnotesMeta.get(1).get("ID"); - - exeID[0] = (String) attachmentsMeta.get(2).get("ID"); - exeID[1] = (String) referencesMeta.get(2).get("ID"); - exeID[2] = (String) footnotesMeta.get(2).get("ID"); - - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; - - String name1 = "sample.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // PDF - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); - - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); - - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); - - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); - - String updateSecondaryPropertyResponse5 = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated") - && updateSecondaryPropertyResponse5.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); - String updateSecondaryPropertyResponseTXT1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } - - Integer secondaryPropertyInt3 = 12; - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - for (int i = 0; i < facet.length; i++) { - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); - - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); - - if (updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // Note: Don't verify specific filenames since previous tests may have changed them - System.out.println("Save response: " + response); - - // Parse JSON response to check for invalid secondary property errors in all three tables - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved"); - testStatus = true; - System.out.println( - "Rename & update unsuccessful for invalid Secondary properties and successful for valid property attachments"); - } else { - System.out.println("Save response did not match expected: " + response); - } - } else { - System.out.println("Not enough attachments in facets - need at least 3 per facet"); - } - } - } else { - System.out.println( - "Could not edit book - it may be stuck in draft mode from a previous test"); - } - if (!testStatus) { - fail("Could not update secondary property before book is saved"); - } - } - - // Tests 28 and 29 removed - chapters have no attachment limit - - // Tests 28-29 skipped - chapters have no attachment limit - - @Test - @Order(30) - void testDiscardBookDraftWithoutChapterAttachments() { - System.out.println("Test (30) : Discard book draft without adding chapter attachments"); - Boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if (response.equals("Entity Draft Deleted")) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft with chapter was not discarded properly"); - } - } - - @Test - @Order(31) - void testDiscardBookDraftWithChapterAttachments() throws IOException { - System.out.println("Test (31): Discard book draft with chapter attachments"); - boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!"Could not create entity".equals(chapterResponse)) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = - new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - 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 < facet.length; i++) { - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - if ("Attachment created".equals(createResponse.get(0))) { - System.out.println("Attachment created in chapter facet: " + facet[i]); - } else { - System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - } - } - - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft with chapter attachments was not discarded properly"); - } - } - - // Tests 32-34 covered in tests 19, 23, 24 - // Tests 37-41 skipped - copy with notes/secondary properties not applicable - - @Test - @Order(42) - void testCreateLinkSuccessInChapter() throws IOException { - System.out.println("Test (42): Create link in chapter"); - List attachments = new ArrayList<>(); - - // Create book and chapter for link testing - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create book"); - } - String createLinkBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - if (chapterResponse.equals("Could not create entity")) { - fail("Could not create chapter"); - } - String createLinkChapterID = chapterResponse; - - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - for (String facetName : facet) { - String createLinkResponse1 = - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - String createLinkResponse2 = - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", linkUrl); - if (!createLinkResponse1.equals("Link created successfully") - || !createLinkResponse2.equals("Link created successfully")) { - fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); - } - } - - String saveEntityResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save book"); - } - - for (String facetName : facet) { - attachments = - api - .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) - .stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String openAttachmentResponse; - for (String attachment : attachments) { - openAttachmentResponse = - api.openAttachment( - appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open created link in chapter facet : " + facetName); - } - } - } - api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - } - - @Test - @Order(43) - void testCreateLinkDifferentChapter() throws IOException { - System.out.println("Test (43): Create link with same name in different chapter"); - - // Create new book and chapter - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (response.equals("Could not edit entity")) { - fail("Could not create book"); - } - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (chapterResponse.equals("Could not create entity")) { - fail("Could not create chapter"); - } - String tempChapterID = chapterResponse; - - String linkName = "sample"; - String linkUrl = "https://example.com"; - for (String facetName : facet) { - String createResponse = - api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); - if (!createResponse.equals("Link created successfully")) { - fail("Could not create link in different chapter with same name"); - } - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (!response.equals("Saved")) { - fail("Could not save book"); - } - - response = api.deleteEntity(appUrl, bookEntityName, tempBookID); - if (!response.equals("Entity Deleted")) { - fail("Could not delete book"); - } - } - - @Test - @Order(44) - void testCreateLinkFailureInChapter() throws IOException { - System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if ("Could not create entity".equals(response)) { - fail("Could not create book"); - } - String createLinkBookID = response; - - response = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - if ("Could not create entity".equals(response)) { - fail("Could not create chapter"); - } - String createLinkChapterID = response; - - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - - ObjectMapper mapper = new ObjectMapper(); - - for (String facetName : facet) { - - // Create initial link for this facet first (so duplicate test works) - response = - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - if (!"Link created successfully".equals(response)) { - fail("Could not create initial link for facet: " + facetName); - } - - /* ---------- INVALID URL ---------- */ - try { - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); - fail("Expected invalid URL error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - assertEquals("400018", error.path("code").asText()); - assertTrue( - error.path("message").asText().contains("expected pattern"), - "Unexpected message: " + error.path("message").asText()); - } - - /* ---------- INVALID NAME ---------- */ - try { - api.createLink( - appUrl, - chapterEntityName, - facetName, - createLinkChapterID, - "sample//", - "https://example.com"); - fail("Expected invalid name error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); - - assertEquals("500", error.path("code").asText()); - assertTrue( - message.contains("contains unsupported characters") - && message.contains("Rename and try again"), - "Unexpected message: " + message); - } - - /* ---------- EMPTY NAME & URL ---------- */ - try { - api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); - fail("Expected missing value error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - assertEquals("409008", error.path("code").asText()); - assertEquals("Provide the missing value.", error.path("message").asText()); - } - - /* ---------- DUPLICATE NAME ---------- */ - try { - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - fail("Expected duplicate name error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - assertEquals("500", error.path("code").asText()); - assertEquals( - "An object named \"sample\" already exists. Rename the object and try again.", - error.path("message").asText()); - } - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - if (!"Saved".equals(response)) { - fail("Could not save book"); - } - - response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - if (!"Entity Deleted".equals(response)) { - fail("Could not delete book"); - } - } - - @Test - @Order(45) - void testCreateLinkNoSDMRolesInChapter() throws IOException { - System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); - - String createLinkBookNoRoles = - apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (createLinkBookNoRoles.equals("Could not edit entity")) { - fail("Could not create book"); - } - - String createLinkChapterNoRoles = - apiNoRoles.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); - if (createLinkChapterNoRoles.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - for (String facetName : facet) { - String linkName = "sample27"; - String linkUrl = "https://example.com"; - try { - apiNoRoles.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); - if (!response.equals("Saved")) { - fail("Could not save book"); - } - - response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); - if (!response.equals("Entity Deleted")) { - fail("Could not delete book"); - } - } - - @Test - @Order(46) - void testDeleteLinkInChapter() throws IOException { - System.out.println("Test (46): Delete link in chapter"); - List> attachments = new ArrayList<>(); - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create book"); - } - String deleteLinkBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); - if (chapterResponse.equals("Could not create entity")) { - fail("Could not create chapter"); - } - String deleteLinkChapterID = chapterResponse; - - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink( - appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for chapter facet : " + facetName); - } - } - - String saveEntityResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save book"); - } - - for (String facetName : facet) { - attachments.add( - api - .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - .stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } - - String editEntityResponse = - api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - int index = 0; - for (String facetName : facet) { - String deleteLinkResponse = - api.deleteAttachment( - appUrl, - chapterEntityName, - facetName, - deleteLinkChapterID, - attachments.get(index).get(0)); - System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - if (!deleteLinkResponse.equals("Deleted")) { - fail("Could not delete created link"); - } - index += 1; - } - - saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save book"); - } - - index = 0; - attachments.clear(); - for (String facetName : facet) { - attachments.add( - api - .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - .stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - System.out.println( - "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - if (attachments.get(index).size() != 0) { - fail("Link wasn't deleted"); - } - index += 1; - } - - response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); - if (!response.equals("Entity Deleted")) { - fail("Could not delete book"); - } - } - - @Test - @Order(35) - void testCopyAttachmentsToNewChapterInSameBook() throws IOException { - System.out.println( - "Test (35): Copy attachments from one chapter to another new chapter in the same book"); - - // Create source book and chapter with attachments - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Load original files for copying content - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(new ArrayList<>()); - } - - // Create attachments in all facets - each upload needs a unique filename - int fileCounter = 0; - for (int i = 0; i < facet.length; i++) { - boolean useTxt = (i == 1); // Use txt for references facet - postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - File originalFile = useTxt ? originalTxt : originalPdf; - String extension = useTxt ? ".txt" : ".pdf"; - - for (int j = 0; j < 2; j++) { // Create 2 attachments per facet - // Create unique temp file for EACH upload to avoid duplicate filename errors - fileCounter++; - File tempFile = - File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - System.out.println("Created attachment ID: " + createResponse.get(1)); - } else { - System.out.println("Failed to create attachment: " + createResponse.get(0)); - fail("Could not create attachment in facet: " + facet[i]); - } - } - } - - // Fetch object IDs from source attachments - List objectIds = new ArrayList<>(); - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - if (metadata.containsKey("objectId")) { - objectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - } - - // Save the source book - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Create target chapter in the SAME book - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source book for adding target chapter"); - } - - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (targetChapterID.equals("Could not create entity")) { - fail("Could not create target chapter in same book"); - } - - // Copy attachments to target chapter - int objectIdIndex = 0; - for (String facetName : facet) { - List facetObjectIds = - objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, facetObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - } - - // Fetch and wait for copied attachments - List> copiedMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - for (Map meta : copiedMetadata) { - String copiedId = (String) meta.get("ID"); - } - objectIdIndex += 2; - } - - // Save the book with new chapter - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book after copying attachments"); - } - - // Verify attachments were copied - read them - for (String facetName : facet) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.size() != 2) { - fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); - } - for (Map meta : targetMetadata) { - String attachmentId = (String) meta.get("ID"); - String readResponse = - api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment in facet: " + facetName); - } - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - } - - @Test - @Order(36) - void testCopyAttachmentsToChapterInDifferentBook() throws IOException { - System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); - - // Create Book1 with source chapter and attachments - copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create Book2 with target chapter - copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Load original files for copying content - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(new ArrayList<>()); - } - - // Create attachments in all facets of source chapter - each upload needs unique filename - int fileCounter = 0; - for (int i = 0; i < facet.length; i++) { - boolean useTxt = (i == 1); // Use txt for references facet - postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - File originalFile = useTxt ? originalTxt : originalPdf; - String extension = useTxt ? ".txt" : ".pdf"; - - for (int j = 0; j < 2; j++) { - // Create unique temp file for EACH upload to avoid duplicate filename errors - fileCounter++; - File tempFile = - File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - System.out.println("Created attachment ID: " + createResponse.get(1)); - } else { - System.out.println("Failed to create attachment: " + createResponse.get(0)); - fail("Could not create attachment in facet: " + facet[i]); - } - } - } - - // Fetch object IDs - sourceObjectIds.clear(); - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - } - - // Save Book1 - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Copy attachments from Book1's chapter to Book2's chapter - if (sourceObjectIds.size() == 6) { - int objectIdIndex = 0; - for (String facetName : facet) { - List facetObjectIds = - sourceObjectIds.subList( - objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, facetObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - } - - objectIdIndex += 2; - } - - // Save Book2 - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book after copying attachments"); - } - - // Verify attachments were copied - for (String facetName : facet) { - List> targetMetadata = - api.fetchEntityMetadata( - appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); - if (targetMetadata.size() != 2) { - fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); - } - for (Map meta : targetMetadata) { - String attachmentId = (String) meta.get("ID"); - String readResponse = - api.readAttachment( - appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, attachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment in facet: " + facetName); - } - } - } - - // Cleanup - delete both books after verification - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - } else { - fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); - } - } - - @Test - @Order(37) - void testCopyAttachmentsWithNotePreserved() throws IOException { - System.out.println("Test (37): Copy attachments with note field preserved"); - - // Create source book and chapter - copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create attachments with notes in source chapter - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] sourceAttachmentIds = new String[facet.length]; - String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); - - for (int i = 0; i < facet.length; i++) { - // Create unique temp file for each facet - File tempFile = - File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - sourceAttachmentIds[i] = createResponse.get(1); - - // Update note field using RequestBody - String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - String noteResponse = - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - noteBody); - if (!noteResponse.equals("Updated")) { - fail("Could not update note for attachment in facet: " + facet[i]); - } - System.out.println("Note updated for facet: " + facet[i]); - } - - // Save source book - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Verify notes were saved in source - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - if (!testNote.equals(metadata.get("note"))) { - fail("Note not saved correctly in source for facet: " + facet[i]); - } - } - - // Create target book and chapter - copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Get object IDs and copy attachments - for (int i = 0; i < facet.length; i++) { - Map sourceMetadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - String objectId = sourceMetadata.get("objectId").toString(); - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - System.out.println("Attachment copied to facet: " + facet[i]); - } - - // Save target book - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify notes were preserved in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); - if (targetMetadata.isEmpty()) { - fail("No attachments found in target facet: " + facet[i]); - } - Map copiedAttachment = targetMetadata.get(0); - String copiedNote = (String) copiedAttachment.get("note"); - if (!testNote.equals(copiedNote)) { - fail( - "Note not preserved after copy in facet: " - + facet[i] - + ". Expected: " - + testNote - + ", Got: " - + copiedNote); - } - System.out.println("Note preserved in target facet: " + facet[i]); - } - - System.out.println("Test 37 passed - notes preserved during copy"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - copyAttachmentSourceBook = null; - copyAttachmentTargetBook = null; - } - - @Test - @Order(38) - void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { - System.out.println("Test (38): Copy attachments with secondary properties preserved"); - - // Use entities from test 37 or create new ones if needed - boolean sourceBookJustCreated = false; - boolean targetBookJustCreated = false; - - if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - copyAttachmentSourceBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - sourceBookJustCreated = true; - } - - if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - copyAttachmentTargetBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - targetBookJustCreated = true; - } - - // If source book was just created, save it first before we can edit it - if (sourceBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created source book"); - } - } - - // If target book was just created, save it first - if (targetBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created target book"); - } - } - - // Edit source book - String editResponse = - api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source book"); - } - - // Create new attachments with secondary properties - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] sourceAttachmentIds = new String[facet.length]; - Boolean testBooleanProp = true; - Integer testIntegerProp = 12345; - - for (int i = 0; i < facet.length; i++) { - // Create unique temp file - File tempFile = - File.createTempFile( - "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - sourceAttachmentIds[i] = createResponse.get(1); - - // Update secondary properties using RequestBody (customProperty6 - Boolean, customProperty2 - - // Integer) - String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - String boolResponse = - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - boolBody); - if (!boolResponse.equals("Updated")) { - System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); - } - - String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - String intResponse = - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - intBody); - if (!intResponse.equals("Updated")) { - System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); - } - - System.out.println("Secondary properties updated for facet: " + facet[i]); - } - - // Save source book - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Edit target book - editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - // Copy attachments to target - for (int i = 0; i < facet.length; i++) { - Map sourceMetadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - String objectId = sourceMetadata.get("objectId").toString(); - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); - } - - // Save target book - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify secondary properties were preserved in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); - - // Find the attachment we just copied (most recent one) - boolean found = false; - for (Map attachment : targetMetadata) { - Object boolProp = attachment.get("customProperty6"); - Object intProp = attachment.get("customProperty2"); - - if (boolProp != null && intProp != null) { - if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { - found = true; - System.out.println("Secondary properties preserved in target facet: " + facet[i]); - break; - } - } - } - if (!found) { - System.out.println( - "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); - } - } - - System.out.println("Test 38 passed - secondary properties checked during copy"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - copyAttachmentSourceBook = null; - copyAttachmentTargetBook = null; - } - - @Test - @Order(39) - void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { - System.out.println( - "Test (39): Copy attachments with both note and secondary properties preserved"); - - // Use entities from previous tests or create new ones - boolean sourceBookJustCreated = false; - boolean targetBookJustCreated = false; - - if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - copyAttachmentSourceBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - sourceBookJustCreated = true; - } - - if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - copyAttachmentTargetBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - targetBookJustCreated = true; - } - - // If source book was just created, save it first before we can edit it - if (sourceBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created source book"); - } - } - - // If target book was just created, save it first - if (targetBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created target book"); - } - } - - // Edit source book - String editResponse = - api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source book"); - } - - // Create new attachments with both note and secondary properties - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] sourceAttachmentIds = new String[facet.length]; - String testNote = "Combined test note - " + System.currentTimeMillis(); - Boolean testBooleanProp = true; - Integer testIntegerProp = 99999; - - for (int i = 0; i < facet.length; i++) { - // Create unique temp file - File tempFile = - File.createTempFile( - "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - sourceAttachmentIds[i] = createResponse.get(1); - - // Update note using RequestBody - String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - noteBody); - - // Update secondary properties using RequestBody - String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - boolBody); - - String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - intBody); - - System.out.println("Note and secondary properties updated for facet: " + facet[i]); - } - - // Save source book - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Edit target book - editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - // Copy attachments to target - for (int i = 0; i < facet.length; i++) { - Map sourceMetadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - String objectId = sourceMetadata.get("objectId").toString(); - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - System.out.println("Attachment with note and properties copied to facet: " + facet[i]); - } - - // Save target book - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify note and secondary properties were preserved in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); - - boolean noteFound = false; - boolean propsFound = false; - - for (Map attachment : targetMetadata) { - String copiedNote = (String) attachment.get("note"); - Object boolProp = attachment.get("customProperty6"); - Object intProp = attachment.get("customProperty2"); - - if (testNote.equals(copiedNote)) { - noteFound = true; - System.out.println("Note preserved in target facet: " + facet[i]); - } - - if (boolProp != null && intProp != null) { - if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { - propsFound = true; - System.out.println("Secondary properties preserved in target facet: " + facet[i]); - } - } - } - - if (!noteFound) { - System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); - } - if (!propsFound) { - System.out.println( - "Warning: Secondary properties may not be preserved in facet: " + facet[i]); - } - } - - // Cleanup - delete both books - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - - // Reset static variables - copyAttachmentSourceBook = null; - copyAttachmentTargetBook = null; - copyAttachmentSourceChapter = null; - copyAttachmentTargetChapter = null; - - System.out.println("Test 39 passed - both note and secondary properties checked during copy"); - } - - @Test - @Order(40) - void testCopyAttachmentsWithInvalidObjectId() throws IOException { - System.out.println("Test (40): Copy attachments with invalid object ID should fail"); - - // Create independent test entities (don't rely on previous tests) - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create test book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create test chapter"); - } - - // Save the book first so it's not in draft mode - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save test book"); - } - - // Now edit it to test copy with invalid object IDs - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit test book"); - } - - // Try to copy with invalid object ID - for (String facetName : facet) { - try { - List invalidObjectIds = new ArrayList<>(); - invalidObjectIds.add("invalidObjectId123"); - invalidObjectIds.add("anotherInvalidId456"); - api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, invalidObjectIds); - fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); - } catch (IOException e) { - // Expected - copy should fail with invalid object ID - System.out.println( - "Expected error received for invalid object ID in facet " - + facetName - + ": " - + e.getMessage()); - } - } - - // Save and cleanup - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - - // Also cleanup test 36 entities if they exist - if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { - try { - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - } catch (Exception e) { - // Ignore - may already be deleted - } - } - if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { - try { - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - } catch (Exception e) { - // Ignore - may already be deleted - } - } - } - - @Test - @Order(41) - void testCopyAttachmentsToExistingChapter() throws IOException { - System.out.println( - "Test (41): Copy attachments to an existing chapter that already has attachments"); - - // Create Book1 with source chapter - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create Book2 with target chapter that has existing attachments - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (targetBookID.equals("Could not create entity")) { - fail("Could not create target book"); - } - - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - if (targetChapterID.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Create temp files with unique names to avoid duplicate filename errors - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test41_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create attachment in source chapter - List sourceObjIds = new ArrayList<>(); - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create source attachment"); - } - String attachmentId = createResponse.get(1); - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - sourceObjIds.add(metadata.get("objectId").toString()); - } - - // Create existing attachment in target chapter - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", targetChapterID); - postData.put("mimeType", "text/plain"); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create existing target attachment"); - } - String attachmentId = createResponse.get(1); - } - - // Save both books - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Edit target book and copy attachments - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - // Copy from source to target (target already has 1 attachment per facet) - for (int i = 0; i < facet.length; i++) { - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjIds.get(i)); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, objectIdsToCopy); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - } - - // Save target book - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) - for (String facetName : facet) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.size() != 2) { - fail( - "Expected 2 attachments in facet " - + facetName - + " (1 existing + 1 copied), found " - + targetMetadata.size()); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - // ============= LINK RENAME TESTS (47-49) ============= - - @Test - @Order(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (47): Rename link in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links in all facets - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit and rename links - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - String renameResponse = - api.renameAttachment( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); - if (!renameResponse.equals("Renamed")) { - fail("Could not rename link in facet: " + facetName); - } - } - - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book after renaming links"); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(48) - void testRenameLinkDuplicate() throws IOException { - System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create two links in all facets - for (String facetName : facet) { - String createLinkResponse1 = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "link1", - "https://www.example1.com"); - String createLinkResponse2 = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "link2", - "https://www.example2.com"); - if (!createLinkResponse1.equals("Link created successfully") - || !createLinkResponse2.equals("Link created successfully")) { - fail("Could not create links in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit and try to rename link2 to link1 (duplicate) - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.size() < 2) { - fail("Expected 2 links in facet: " + facetName); - } - - // Find link2 and rename to link1 - for (Map attachment : attachments) { - if ("link2".equals(attachment.get("fileName"))) { - String linkId = (String) attachment.get("ID"); - api.renameAttachment( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); - break; - } - } - } - - // Save should fail with duplicate error - String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - ObjectMapper mapper = new ObjectMapper(); - try { - JsonNode errorJson = mapper.readTree(saveError); - String errorMessage = errorJson.path("error").path("message").asText(); - if (!errorMessage.contains("already exists")) { - fail("Expected duplicate error but got: " + saveError); - } - } catch (Exception e) { - if (!saveError.contains("already exists")) { - fail("Expected duplicate error but got: " + saveError); - } - } - - // Cleanup - api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(49) - void testRenameLinkUnsupportedCharacters() throws IOException { - System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links in all facets - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit and rename with unsupported characters - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - api.renameAttachment( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); - } - - // Save should fail with unsupported characters error - String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveError.contains("unsupported characters")) { - fail("Expected unsupported characters error but got: " + saveError); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - // ============= LINK EDIT TESTS (50-53) ============= - - @Test - @Order(50) - void testEditLinkSuccess() throws IOException { - System.out.println("Test (50): Edit existing link URL in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links in all facets - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit links - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - String editLinkResponse = - api.editLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - linkId, - "https://www.editedexample.com"); - if (!editLinkResponse.equals("Link edited successfully")) { - fail("Could not edit link in facet: " + facetName); - } - } - - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book after editing links"); - } - - // Verify links open successfully - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - for (Map attachment : attachments) { - String linkId = (String) attachment.get("ID"); - String openResponse = - api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); - if (!openResponse.equals("Attachment opened successfully")) { - fail("Could not open edited link in facet: " + facetName); - } - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(51) - void testEditLinkFailureInvalidURL() throws IOException { - System.out.println("Test (51): Edit link with invalid URL fails in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - try { - api.editLink( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://editedexample"); - fail("Edit link should have failed with invalid URL in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected error received for invalid URL in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(52) - void testEditLinkFailureEmptyURL() throws IOException { - System.out.println("Test (52): Edit link with empty URL fails in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - try { - api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); - fail("Edit link should have failed with empty URL in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected error received for empty URL in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - try { - apiNoRoles.editLink( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://www.edited.com"); - fail("Edit link should have failed without SDM roles in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected permission error received in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - // ============= COPY LINK TESTS (54-58) ============= - - @Test - @Order(54) - void testCopyLinkSuccessNewChapter() throws IOException { - System.out.println("Test (54): Copy link from one chapter to another new chapter"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - if (sourceChapterID.equals("Could not create entity") - || targetChapterID.equals("Could not create entity")) { - fail("Could not create chapters"); - } - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - // Create links in source chapter - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch object IDs - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - // Copy links to target chapter - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link for facet " + facetName + ": " + copyResponse); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify link type and URL - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.isEmpty()) { - fail("No links found in target chapter for facet: " + facetName); - } - - Map copiedLink = targetMetadata.get(0); - String receivedUrl = (String) copiedLink.get("linkUrl"); - assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); - - objectIdIndex++; - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(55) - void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { - System.out.println("Test (55): Copy invalid link object ID to chapter fails"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - for (String facetName : facet) { - try { - List invalidObjectIds = new ArrayList<>(); - invalidObjectIds.add("incorrectObjectId"); - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, invalidObjectIds); - fail("Copy should have thrown error for invalid object ID in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected error received for invalid object ID in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(56) - void testCopyLinkToExistingChapter() throws IOException { - System.out.println("Test (56): Copy link to existing chapter that has attachments"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - // Create links in source chapter - for (int i = 0; i < facet.length; i++) { - String linkName = "sourceLink" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in source chapter for facet: " + facet[i]); - } - } - - // Create existing links in target chapter - for (int i = 0; i < facet.length; i++) { - String linkName = "existingLink" + i; - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facet[i], - targetChapterID, - linkName, - "https://www.existing.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create existing link in target chapter for facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch source object IDs - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - // Copy links to target chapter - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link for facet " + facetName); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify target has 2 links (existing + copied) - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.size() != 2) { - fail( - "Expected 2 links in target chapter facet " - + facetName - + ", found " - + targetMetadata.size()); - } - - objectIdIndex++; - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(57) - void testCopyLinkNoSDMRoles() throws IOException { - System.out.println("Test (57): Copy link fails due to no SDM roles"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch object IDs - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - // Try to copy with no SDM roles - int objectIdIndex = 0; - for (String facetName : facet) { - try { - // Use normal api to put book in draft mode - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // Use apiNoRoles to attempt copy (should fail) - apiNoRoles.copyAttachment( - appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - fail("Copy should have failed without SDM roles in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected permission error in facet " + facetName); - // Discard draft to clean up for next iteration - api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); - } - objectIdIndex++; - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(58) - void testCopyLinkFromDraftChapter() throws IOException { - System.out.println("Test (58): Copy link from draft chapter to another chapter"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - // Create links in source chapter (NOT saved yet - draft mode) - for (int i = 0; i < facet.length; i++) { - String linkName = "draftLink" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - - // Save target book only - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch object IDs from draft - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - if (linkObjectIds.size() != facet.length) { - fail("Could not fetch all object IDs from draft"); - } - - // Copy links from draft to target - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link from draft for facet " + facetName); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify link was copied - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.isEmpty()) { - fail("No links found in target chapter for facet: " + facetName); - } - - objectIdIndex++; - } - - // Cleanup - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= - - @Test - @Order(59) - void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { - System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - if (sourceChapterID.equals("Could not create entity") - || targetChapterID.equals("Could not create entity")) { - fail("Could not create chapters"); - } - - // Create temp files with unique names - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test59_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List sourceObjectIds = new ArrayList<>(); - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(new ArrayList<>()); - } - - // Create attachments in source chapter (still in draft) - for (int i = 0; i < facet.length; i++) { - postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); - File file = i == 1 ? tempTxt : tempPdf; - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment in facet: " + facet[i]); - } - } - - // Fetch object IDs from draft - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - } - - // Save target book only - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Copy attachments from draft to target - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment from draft for facet " + facetName); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify attachment was copied - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.isEmpty()) { - fail("No attachments found in target chapter for facet: " + facetName); - } - - // Read attachment to verify - String attachmentId = (String) targetMetadata.get(0).get("ID"); - String readResponse = - api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment in facet: " + facetName); - } - - objectIdIndex++; - } - - // Cleanup - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - // ============= CHANGELOG TESTS (60-64) ============= - - @Test - @Order(60) - void testViewChangelogForNewlyCreatedAttachment() throws IOException { - System.out.println("Test (60): View changelog for newly created attachment in chapter"); - - for (int i = 0; i < facet.length; i++) { - String facetName = facet[i]; - - // Create book and chapter - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = - File.createTempFile( - "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } - - String attachmentId = createResponse.get(1); - - // Fetch changelog - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); - - // Cleanup - api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - } - } - - @Test - @Order(61) - void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - System.out.println("Test (61): Changelog after modifying note and custom property in chapter"); - - for (int i = 0; i < facet.length; i++) { - String facetName = facet[i]; - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = - File.createTempFile( - "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Update note - String notesValue = "Test note for changelog verification"; - RequestBody updateNotesBody = - RequestBody.create( - MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); - - // Update custom property - RequestBody bodyInt = - RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 12345}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); - - // Save - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit to fetch changelog - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - // Fetch changelog - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - int numItems = (int) changelogResponse.get("numItems"); - assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - } - - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println("Test (62): Changelog after renaming attachment in chapter"); - - for (int i = 0; i < facet.length; i++) { - String facetName = facet[i]; - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = - File.createTempFile( - "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Rename attachment - String renameResponse = - api.renameAttachment( - appUrl, - chapterEntityName, - facetName, - testChapterID, - attachmentId, - "renamed_file.txt"); - if (!renameResponse.equals("Renamed")) { - fail("Could not rename attachment"); - } - - // Save - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit to fetch changelog - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - // Fetch changelog - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - int numItems = (int) changelogResponse.get("numItems"); - assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - } - - @Test - @Order(63) - void testChangelogForCopiedAttachment() throws IOException { - System.out.println("Test (63): Changelog for copied attachment in chapter"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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 in source - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Save both books - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Get object ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - - // Copy to target - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment"); - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch changelog for copied attachment - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); - - editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - Map changelogResponse = - api.fetchChangelog( - appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - int numItems = (int) changelogResponse.get("numItems"); - assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(64) - void testChangelogForNewChapter() throws IOException { - System.out.println("Test (64): Changelog for attachment in newly created chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Fetch changelog before saving - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - assertEquals( - 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); - - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); - - // Cleanup - api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - } - - // ============= MOVE ATTACHMENT TESTS (65-75) ============= - - @Test - @Order(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println("Test (65): Move attachments from source chapter to target chapter"); - - for (int i = 0; i < facet.length; i++) { - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create temp files - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempPdf.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), - tempTxt.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List sourceAttachmentIds = new ArrayList<>(); - File[] files = {tempPdf, tempTxt}; - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source chapter"); - } - } - - // Save source book - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Get object IDs and folder ID - List moveObjectIds = new ArrayList<>(); - String sourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (sourceFolderId == null && metadata.containsKey("folderId")) { - sourceFolderId = metadata.get("folderId").toString(); - } - } - } - - // Create target book and chapter - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (targetBookID.equals("Could not create entity")) { - fail("Could not create target book"); - } - - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - if (targetChapterID.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Save target book before moving attachments (moveAttachments requires Active entity) - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book before move"); - } - - // Move attachments to Active entity - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[i], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } - - // Verify - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - assertEquals( - sourceAttachmentIds.size(), - targetMetadata.size(), - "Target should have all attachments after move"); - - List> sourceMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, targetBookID); - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - } - } - - @Test - @Order(66) - void testMoveAttachmentsToChapterWithDuplicate() throws IOException { - System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Create attachment in source with specific name - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, originalPdf); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create source attachment"); - } - String sourceAttachmentId = createResponse.get(1); - - // Create attachment in target with SAME name (duplicate) - postData.put("up__ID", targetChapterID); - createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, originalPdf); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create target attachment"); - } - - // Save both - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Get source object ID and folder ID - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, sourceAttachmentId); - String objectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move to saved target - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Move should handle duplicate - attachment stays in source - - // Verify source still has attachment (duplicate not moved) - List> sourceMetadataAfter = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertTrue( - sourceMetadataAfter.size() >= 1, - "Source should still have attachment when duplicate exists in target"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(67) - void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { - System.out.println("Test (67): Move attachments with notes and secondary properties"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - // Add note and secondary property - String testNote = "Test note for move"; - RequestBody noteBody = - RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + "\"}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); - - RequestBody propBody = - RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); - - // Save source - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get object ID and folder ID - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // Create target - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null"); - } - - // Verify note was preserved - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - if (!targetMetadata.isEmpty()) { - String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); - Map movedMetadata = - api.fetchMetadata( - appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); - - // Note should be preserved - if (movedMetadata.containsKey("note")) { - assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(68) - void testMoveAttachmentsPartialFailure() throws IOException { - System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get real object ID and folder ID - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String realObjectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // Create target - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Try to move with mix of valid and invalid object IDs - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(realObjectId); - moveObjectIds.add("invalidObjectId123"); - - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Should handle partial failure - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(69) - void testMoveAttachmentsEmptyList() throws IOException { - System.out.println("Test (69): Move attachments with empty object ID list"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Try to move with empty list - List emptyObjectIds = new ArrayList<>(); - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - - try { - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - "someFolderId", - emptyObjectIds, - targetFacet, - sourceFacet); - // Should either fail or do nothing - } catch (Exception e) { - System.out.println("Expected: Move with empty list handled: " + e.getMessage()); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(70) - void testMoveAttachmentsToSameChapter() throws IOException { - System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - 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"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Get object ID and folder ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String folderId = metadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move to same chapter - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - testChapterID, - folderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Should handle gracefully - attachment stays in place - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Verify attachment still exists - List> metadataAfter = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); - assertEquals(1, metadataAfter.size(), "Attachment should still exist"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(71) - void testMoveAttachmentsBetweenFacets() throws IOException { - System.out.println("Test (71): Move attachments between different facets in chapters"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create in attachments facet - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Get object ID and folder ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String sourceFolderId = metadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move from attachments to references facet - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[1], // references facet - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify moved to different facet - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); - assertTrue( - targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(72) - void testMoveMultipleAttachments() throws IOException { - System.out.println("Test (72): Move multiple attachments at once between chapters"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // Create multiple temp files - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test72_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List sourceAttachmentIds = new ArrayList<>(); - File[] files = {tempPdf, tempTxt}; - String[] mimeTypes = {"application/pdf", "text/plain"}; - - for (int i = 0; i < files.length; i++) { - postData.put("mimeType", mimeTypes[i]); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get object IDs - List moveObjectIds = new ArrayList<>(); - String sourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - moveObjectIds.add(metadata.get("objectId").toString()); - if (sourceFolderId == null) { - sourceFolderId = metadata.get("folderId").toString(); - } - } - - // Create target - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Move all at once - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify all moved - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - assertEquals( - sourceAttachmentIds.size(), - targetMetadata.size(), - "All attachments should be moved to target"); - - List> sourceMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(73) - void testMoveAttachmentsAllFacets() throws IOException { - System.out.println("Test (73): Move attachments from all facets between chapters"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Create attachment in each facet - for (int i = 0; i < facet.length; i++) { - String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); - File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Move from each facet - for (int i = 0; i < facet.length; i++) { - List> sourceMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - if (sourceMetadata.isEmpty()) { - continue; - } - - String attachmentId = (String) sourceMetadata.get(0).get("ID"); - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String sourceFolderId = metadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - api.moveAttachment( - appUrl, - chapterEntityName, - facet[i], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - } - - // Verify all facets have attachments in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + facet[i]); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(74) - void testChainMoveAttachments() throws IOException { - System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || target1BookID.equals("Could not create entity") - || target2BookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String target1ChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); - String target2ChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); - - // First move: Source -> Target1 - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move to target1 - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - target1ChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify in target1 - List> target1Metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); - - // Second move: Target1 -> Target2 - String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); - Map target1AttMetadata = - api.fetchMetadata( - appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); - String target1ObjectId = target1AttMetadata.get("objectId").toString(); - String target1FolderId = target1AttMetadata.get("folderId").toString(); - - moveObjectIds.clear(); - moveObjectIds.add(target1ObjectId); - - // Move to target2 - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - target2ChapterID, - target1FolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify final state - List> target2Metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); - assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); - - target1Metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); - - List> sourceFinalMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, target1BookID); - api.deleteEntity(appUrl, bookEntityName, target2BookID); - } - - @Test - @Order(75) - void testMoveAttachmentsWithoutSDMRole() throws IOException { - System.out.println("Test (75): Move attachments fails without SDM role"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = - File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get object ID and folder ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String sourceFolderId = metadata.get("folderId").toString(); - - // Create target with no role user - String targetBookID = - apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (targetBookID.equals("Could not create entity")) { - fail("Could not create target book"); - } - - String targetChapterID = - apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - boolean moveFailed = false; - String errorMessage = null; - - try { - Map moveResult = - apiNoRoles.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null || moveResult.containsKey("error")) { - moveFailed = true; - errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; - } - } catch (Exception e) { - moveFailed = true; - errorMessage = e.getMessage(); - } - - assertTrue(moveFailed, "Move should fail without SDM role"); - System.out.println("Move correctly failed without SDM role: " + errorMessage); - - // Verify source still has attachment - List> sourceMetadataAfter = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(76) - void testRenameChapterAttachmentWithExtensionChange() throws IOException { - System.out.println( - "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); - - // Step 1: Create a new book and chapter - String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (newBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - String newChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - if (newChapterID.equals("Could not create entity")) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not create chapter"); - } - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book: " + saveResponse); - } - - // Step 2: Upload a PDF attachment to each facet in the chapter - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", newChapterID); - 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, bookEntityName, srvpath, newBookID); - if (!"Entity in draft mode".equals(editResponse)) { - fail("Could not put book in draft mode for PDF upload"); - } - - String[] facetAttachmentIDs = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - facetAttachmentIDs[i] = - CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - if (facetAttachmentIDs[i] == null) { - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - } - } - - // Step 3: Save the book - String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - if (!savedAfterUpload.equals("Saved")) { - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not save book after PDF upload: " + savedAfterUpload); - } - - // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt - for (int i = 0; i < facet.length; i++) { - String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - if (!"Entity in draft mode".equals(editDraftResponse)) { - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not put book in draft mode for rename on facet: " + facet[i]); - } - - String renameResponse = - api.renameAttachment( - appUrl, - chapterEntityName, - facet[i], - newChapterID, - facetAttachmentIDs[i], - "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - } - - // Step 6: Save and validate the extension change warning message - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } - - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - } - - // Clean up - api.deleteEntity(appUrl, bookEntityName, newBookID); - } - - @Test - @Order(77) - void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { - System.out.println( - "Test (77) : Upload chapter attachment in draft, rename changing extension before save across all facets - should return extension change warning"); - - for (int i = 0; i < facet.length; i++) { - // Step 1: Create a new book and chapter draft (do NOT save) - String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (newBookID.equals("Could not create entity")) { - fail("Could not create book for facet: " + facet[i]); - } - String newChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - if (newChapterID.equals("Could not create entity")) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not create chapter for facet: " + facet[i]); - } - - // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft (unsaved) - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", newChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String facetAttachmentID = - CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - if (facetAttachmentID == null) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - } - - // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not saved - String renameResponse = - api.renameAttachment( - appUrl, - chapterEntityName, - facet[i], - newChapterID, - facetAttachmentID, - "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - } - - // Step 4: Save the book — should receive extension change warning, not "Saved" - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } - - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - - // Clean up - api.deleteEntity(appUrl, bookEntityName, newBookID); - } - } + // @Test + // @Order(7) + // void testRenameChapterAttachments() { + // System.out.println("Test (7) : Rename single attachment, reference, and footnote in + // chapter"); + // Boolean testStatus = true; + + // try { + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + // if ("Entity in draft mode".equals(response)) { + // String[] name = {"sample123", "reference123", "footnote123"}; + // for (int i = 0; i < facet.length; i++) { + // // Read the facet to ensure it exists + // response = + // api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], + // name[i]); + // if (!"Renamed".equals(response)) { + // testStatus = false; + // System.out.println(facet[i] + " was not renamed: " + response); + // } + // } + // // Save book draft if everything is renamed + // if (testStatus) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if (!"Saved".equals(response)) { + // testStatus = false; + // System.out.println("Book draft was not saved: " + response); + // } + // } else { + // // Attempt save despite potential rename failures + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // } + // } else { + // testStatus = false; + // System.out.println("Book was not put into draft mode: " + response); + // } + // } catch (Exception e) { + // testStatus = false; + // System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); + // } + + // if (!testStatus) { + // fail("There was an error during the rename test process for chapter."); + // } + // } + + // @Test + // @Order(8) + // void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { + // System.out.println("Test (8): Create chapter attachments with unsupported characters"); + // 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("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, bookEntityName, srvpath, bookID); + // if (!"Entity in draft mode".equals(response)) { + // fail("Book not in draft mode: " + response); + // return; + // } + + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", chapterID); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); + + // if (!"Attachment created".equals(createResponse.get(0))) { + // fail("Could not create attachment in chapter facet: " + facet[i]); + // return; + // } + + // String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE + // response = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); + + // System.out.println("Rename response for chapter " + facet[i] + ": " + response); + // } + + // // Save should fail + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + // // ---------------- PARSE JSON ---------------- + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // String message = root.path("error").path("message").asText(); + + // // ---------------- NORMALIZE MESSAGE ---------------- + // // 1. Normalize smart quotes + // // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared + // message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); + + // // ---------------- EXPECTED MESSAGE (EXACT) ---------------- + // String expectedMessage = + // "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" + // + "Table: attachments\n" + // + "Page: IntegrationTestEntity"; + + // if (message.equals(expectedMessage)) { + + // for (int i = 0; i < facet.length; i++) { + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + + // if (!testStatus) { + // fail("Test for unsupported characters in chapter attachments failed"); + // } + // } + + // @Test + // @Order(9) + // void testRenameSingleDuplicateInChapter() throws IOException { + // System.out.println( + // "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); + // Boolean testStatus = false; + // int counter = 0; + + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Edit entity response: " + response); + + // if ("Entity in draft mode".equals(response)) { + // // To create a duplicate within the same facet, we need to rename ID2[i] to + // // the same name as an existing file in that facet. After test 7, the existing files are: + // // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) + // // We rename ID2[i] (sample123.txt from test 8) to these names which already exist + // String[] duplicateNames = {"sample123", "reference123", "footnote123"}; + // String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; + + // // Try to rename to duplicate file names (names that already exist in each facet) + // for (int i = 0; i < facet.length; i++) { + // response = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); + // System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); + // if ("Renamed".equals(response)) { + // counter++; + // } + // } + // System.out.println("Renamed count: " + counter); + + // if (counter == facet.length) { + // // Try to save - should fail with duplicate error + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Save response (expecting error): " + response); + + // // Parse JSON response to check for duplicate error + // ObjectMapper mapper = new ObjectMapper(); + // try { + // JsonNode root = mapper.readTree(response); + // String message = root.path("error").path("message").asText(); + + // if (message.contains("already exists")) { + // System.out.println("Duplicate error detected as expected: " + message); + // counter = 0; + // // Rename with valid different names + // for (int i = 0; i < facet.length; i++) { + // response = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); + // System.out.println("Rename " + facet[i] + " to valid name: " + response); + // if ("Renamed".equals(response)) { + // counter++; + // } + // } + + // if (counter == facet.length) { + // // Save should now succeed + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Final save response: " + response); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } else { + // System.out.println("Unexpected error message: " + message); + // } + // } catch (Exception e) { + // // Response might not be JSON if save succeeded (shouldn't happen with duplicates) + // System.out.println("Response was not JSON error: " + response); + // // If save succeeded unexpectedly, we still need to ensure book is saved + // if ("Saved".equals(response)) { + // System.out.println( + // "Save succeeded unexpectedly - duplicates might be in different facets"); + // } + // } + // } + // } else { + // System.out.println("Book was not put into draft mode: " + response); + // } + + // if (!testStatus) { + // fail("Duplicate rename test failed for chapter"); + // } + // } + + // @Test + // @Order(10) + // void testRenameToValidateNamesInChapter() throws IOException { + // System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); + // Boolean testStatus = false; + + // // Create a new book and chapter for this test + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // bookID3 = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); + // if (!"Could not create entity".equals(chapterResponse)) { + // chapterID3 = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID3); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); + // } + + // String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; + + // boolean allRenamed = true; + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); + // if (!"Renamed".equals(response1)) { + // allRenamed = false; + // System.out.println( + // "Failed to rename " + // + facet[i] + // + " to valid name " + // + validNames[i] + // + ": " + // + response1); + // } + // } + + // if (allRenamed) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } + // } + + // if (!testStatus) { + // fail("Could not rename chapter attachments to valid names"); + // } + // } + + // @Test + // @Order(11) + // void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { + // System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); + // boolean testStatus = true; + + // try { + // String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Edit entity response: " + response); + + // if (response.equals("Entity in draft mode")) { + // String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; + // for (int i = 0; i < facet.length; i++) { + // response = + // apiNoRoles.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); + // System.out.println("Rename response for " + facet[i] + ": " + response); + // if (!"Renamed".equals(response)) { + // testStatus = false; + // } + // } + + // if (testStatus) { + // // Save should fail with permission error + // response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Save response (expecting permission error): " + response); + + // // The expected error should indicate no permissions to update + // String expected = + // "[{\"code\":\"\",\"message\":\"Could not update the following + // files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update + // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3}]"; + + // // Check if response contains permission error + // if (!response.equals(expected) + // && !response.contains("do not have the required permissions")) { + // System.out.println("Expected permission error but got: " + response); + // testStatus = false; + // } else { + // System.out.println("Got expected permission error"); + // } + // } else { + // // Some renames failed - save to release draft + // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // } + // } else { + // System.out.println("Could not edit entity: " + response); + // testStatus = false; + // } + // } catch (Exception e) { + // System.out.println("Exception: " + e.getMessage()); + // testStatus = false; + // } + + // if (!testStatus) { + // fail("Chapter attachment got renamed without SDM roles."); + // } + // } + + // @Test + // @Order(12) + // void testDeleteSingleChapterAttachment() throws IOException { + // System.out.println( + // "Test (12) : Delete single attachment, reference, and footnote from chapter"); + // Boolean testStatus = false; + // int deleteCounter = 0; + + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + // if (response.equals("Deleted")) deleteCounter++; + // } + // if (deleteCounter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if (response.equals("Saved")) { + // int verifyCounter = 0; + // for (int i = 0; i < facet.length; i++) { + // response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + // if (response.equals("Could not read Attachment")) verifyCounter++; + // } + // if (verifyCounter == facet.length) { + // testStatus = true; + // } else { + // fail( + // "Could not verify all deleted chapter facets. Verified: " + // + verifyCounter + // + "/" + // + facet.length); + // } + // } else { + // fail("Could not save book after deleting chapter attachments"); + // } + // } else { + // fail( + // "Could not delete all chapter attachments. Deleted: " + // + deleteCounter + // + "/" + // + facet.length); + // } + // } else { + // fail("Could not edit book to draft mode"); + // } + + // if (!testStatus) { + // fail("Test failed to delete chapter attachments"); + // } + // } + + // @Test + // @Order(13) + // void testUploadBlockedMimeTypeToChapter() throws IOException { + // System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); + // Boolean testStatus = false; + + // // Create new book and chapter + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // bookID4 = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); + // if (!"Could not create entity".equals(chapterResponse)) { + // chapterID4 = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = + // new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID4); + // postData.put("mimeType", "application/rtf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // boolean allBlocked = true; + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID4, 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)) { + // allBlocked = false; + // System.out.println( + // "Chapter facet " + // + facet[i] + // + " incorrectly accepted blocked mimeType: " + // + actualResponse); + // } + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); + // if ("Saved".equals(response) && allBlocked) { + // testStatus = true; + // } + // } + // } + + // if (!testStatus) { + // fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); + // } + // } + + // @Test + // @Order(14) + // void testDeleteBookAndChapter() { + // System.out.println("Test (14) : Delete book (and its chapters)"); + // Boolean testStatus = false; + // // Delete books (chapters are deleted automatically as they're composition) + // String response = api.deleteEntity(appUrl, bookEntityName, bookID); + // String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); + // String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); + // String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); + // if (response.equals("Entity Deleted") + // && response2.equals("Entity Deleted") + // && response3.equals("Entity Deleted") + // && response4.equals("Entity Deleted")) testStatus = true; + // if (!testStatus) fail("Could not delete books"); + // } + + // @Test + // @Order(15) + // void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { + // System.out.println( + // "Test (15) : Rename & Update secondary property in chapter before book is saved"); + // System.out.println("Creating book and chapter"); + + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (!response.equals("Could not create entity")) { + // bookID5 = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); + // if (!chapterResponse.equals("Could not create entity")) { + // chapterID5 = chapterResponse; + + // System.out.println("Creating attachment, reference, and footnote in chapter"); + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID5); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // boolean allCreated = true; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); + // if (tempID[i] == null || tempID[i].isEmpty()) { + // System.out.println("Failed to create attachment for facet: " + facet[i]); + // allCreated = false; + // } + // } + + // System.out.println("Attachments, References, and Footnotes created in chapter"); + // System.out.println( + // "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + + // tempID[2]); + + // if (!allCreated) { + // fail("Could not create all attachments for test 15"); + // } + + // // Reset counter for this test + // counter = 0; + + // // Use valid dropdown value for customProperty1 + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + // for (int i = 0; i < facet.length; i++) { + // System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + // System.out.println("Rename response for " + facet[i] + ": " + response1); + + // // Update customProperty1 (String - dropdown value) + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); + + // // Update customProperty2 (Integer) + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + + // // Update customProperty5 (DateTime) - using customProperty5 like Books test + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + + // // Update customProperty6 (Boolean) + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + // // Check all updates succeeded + // if ("Renamed".equals(response1) + // && "Updated".equals(updateSecondaryPropertyResponse1) + // && "Updated".equals(updateSecondaryPropertyResponse2) + // && "Updated".equals(updateSecondaryPropertyResponse3) + // && "Updated".equals(updateSecondaryPropertyResponse4)) { + // counter++; + // } else { + // System.out.println( + // "Update failed for " + // + facet[i] + // + ": rename=" + // + response1 + // + ", dropdown=" + // + updateSecondaryPropertyResponse1 + // + ", int=" + // + updateSecondaryPropertyResponse2 + // + ", datetime=" + // + updateSecondaryPropertyResponse3 + // + ", bool=" + // + updateSecondaryPropertyResponse4); + // } + // } + + // System.out.println("Counter after all facets: " + counter); + // if (counter == facet.length) { + // // Save the book (not the chapter) + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // System.out.println("Save response: " + response); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } else { + // System.out.println( + // "Counter is less than " + facet.length + ", not saving. Counter: " + counter); + // } + // } + // } + + // if (!testStatus) { + // fail( + // "Could not update secondary properties in chapter before book save. Counter: " + + // counter); + // } + // } + + // @Test + // @Order(16) + // void testUploadNAttachmentsToChapter() throws IOException { + // System.out.println("Test (16) : Upload N attachments to chapter"); + // Boolean testStatus = false; + // counter = 0; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); + + // for (int j = 0; j < 5; j++) { + // // Create temp file with unique name per iteration + // File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); + // Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID5); + // 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, bookEntityName, srvpath, bookID5); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + // String check = facetResponse.get(0); + // if (check.equals("Attachment created")) { + // counter++; + // } else { + // System.out.println( + // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + // } + // } + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if (!response.equals("Saved")) { + // System.out.println( + // "Failed to save book after creating attachments in chapter: " + response); + // } + // } else { + // System.out.println("Could not edit book draft: " + response); + // } + // tempFile.delete(); + // } + + // if (counter == 15) { // 5 iterations * 3 facets + // testStatus = true; + // } + + // if (!testStatus) { + // fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); + // } + // } + + // @Test + // @Order(17) + // void testDiscardDraftWithoutChapterAttachments() { + // System.out.println("Test (17) : Discard book draft without chapter attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // // Create chapter but don't add attachments + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft without chapter attachments was not discarded properly"); + // } + // } + + // @Test + // @Order(18) + // void testDiscardDraftWithChapterAttachments() throws IOException { + // System.out.println("Test (18) : Discard book draft with chapter attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // // Create chapter + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // 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 chapter + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + // String check = facetResponse.get(0); + // if (!check.equals("Attachment created")) { + // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + // } + // } + + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft with chapter attachments was not discarded properly"); + // } + // } + + // @Test + // @Order(19) + // void testUploadChapterAttachmentWithoutSDMRole() throws IOException { + // System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); + // Boolean testStatus = true; + + // String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, + // tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // try { + // List createResponse = + // apiNoRoles.createAttachment( + // appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); + // String check = createResponse.get(0); + + // if (check.equals("Attachment created")) { + // testStatus = false; + // } + // } catch (Exception e) { + // // Expected to fail + // testStatus = true; + // } + + // apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // } + // } + + // if (!testStatus) { + // fail("Chapter attachment was uploaded without SDM roles"); + // } + // } + + // @Test + // @Order(20) + // void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { + // System.out.println( + // "Test (20): Rename & Update secondary property in chapter after book is saved"); + // Boolean testStatus = false; + // counter = 0; // Reset counter for this test + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // System.out.println("Editing book, response: " + response); + + // if (response.equals("Entity in draft mode")) { + // // Use unique names that won't conflict with existing attachments + // String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; + // Integer secondaryPropertyInt = 42; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // System.out.println("Renaming and updating secondary properties for chapter attachment"); + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // // Get the first attachment ID from the chapter + // try { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); + // if (!metadata.isEmpty()) { + // tempID[i] = (String) metadata.get(0).get("ID"); + // } + // } catch (IOException e) { + // fail("Could not fetch metadata for chapter: " + e.getMessage()); + // } + + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if (response.equals("Saved")) { + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for chapter attachment"); + // } + // } + // } + // if (!testStatus) fail("Could not update secondary properties in chapter after book is + // saved"); + // } + + // @Test + // @Order(21) + // void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException + // { + // System.out.println( + // "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); + // System.out.println("Creating book and chapter"); + // Boolean testStatus = false; + // int localCounter = 0; + // int createCounter = 0; + + // // Create new book and chapter for this test + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // if (tempID[i] != null) { + // createCounter++; + // } + // } + + // // Only proceed if all facets were created successfully + // if (createCounter == facet.length) { + // // Prepare test data + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + + // "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], + // invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // localCounter++; + // } + // } + + // if (localCounter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + // // Fetch metadata and verify values weren't updated due to invalid property + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, + // tempID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + + // // Parse JSON response and check for expected error messages + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved with expected invalid property errors"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for chapter attachment is unsuccessful"); + // } + // } else { + // System.out.println( + // "Not all facets updated successfully. localCounter: " + localCounter); + // } + // } else { + // System.out.println( + // "Not all facets created successfully. createCounter: " + createCounter); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // } + // } + // if (!testStatus) + // fail("Could not update invalid secondary property in chapter before book is saved"); + // } + + // @Test + // @Order(22) + // void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { + // System.out.println( + // "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); + // System.out.println("Creating book and chapter"); + // Boolean testStatus = false; + // int localCounter = 0; + // int createCounter = 0; + + // // Create new book and chapter + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // if (tempID[i] != null) { + // createCounter++; + // } + // } + + // // Only proceed if all facets were created successfully + // if (createCounter != facet.length) { + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // fail("Not all facets created successfully. createCounter: " + createCounter); + // } + + // // Save the book first + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (!response.equals("Saved")) { + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // fail("Could not save book initially"); + // } + + // // Now edit to update with invalid property + // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (response.equals("Entity in draft mode")) { + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt = 12; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testidinvalid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + + // "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], + // invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // localCounter++; + // } + // } + + // if (localCounter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, + // tempID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + + // // Parse JSON response and check for expected error messages + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved with expected invalid property errors"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for chapter attachment is unsuccessful"); + // } + // } else { + // System.out.println( + // "Not all facets updated successfully. localCounter: " + localCounter); + // } + // } + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // } + // } + // if (!testStatus) + // fail("Could not update invalid secondary property in chapter after book is saved"); + // } + + // @Test + // @Order(23) + // void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { + // System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); + + // boolean testStatus = false; + + // // Reuse bookID5 and chapterID5 + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + + // if (response.equals("Entity in draft mode")) { + // ClassLoader classLoader = getClass().getClassLoader(); + + // // Use temp files with unique names to avoid duplicate name errors + // File originalPdf = + // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + // File originalTxt = + // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); + + // File file1 = File.createTempFile("test23_pdf_", ".pdf"); + // File file2 = File.createTempFile("test23_txt_", ".txt"); + // Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); + // Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", chapterID5); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // Map postData2 = new HashMap<>(postData1); + // postData2.put("up__ID", chapterID5); + // postData2.put("mimeType", "text/plain"); + + // boolean allCreated = true; + // String[] tempID1 = new String[facet.length]; + // String[] tempID2 = new String[facet.length]; + + // for (int i = 0; i < facet.length; i++) { + // List response1 = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); + // List response2 = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); + + // if (response1.get(0).equals("Attachment created") + // && response2.get(0).equals("Attachment created")) { + // tempID1[i] = response1.get(1); // to keep one + // tempID2[i] = response2.get(1); // will delete this one + // } else { + // System.out.println("Failed to create attachments for facet " + facet[i]); + // System.out.println("Response 1: " + response1.get(0)); + // System.out.println("Response 2: " + response2.get(0)); + // allCreated = false; + // break; + // } + + // String deleteResponse = + // api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); + // if (!"Deleted".equals(deleteResponse)) { + // allCreated = false; + // break; + // } + // } + + // file1.delete(); + // file2.delete(); + + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } else { + // System.out.println("Could not edit book: " + response); + // } + + // if (!testStatus) { + // fail("Failed to upload multiple chapter facet entries, delete one per facet and save + // book"); + // } + // } + + // @Test + // @Order(24) + // void testUpdateChapterEntityDraft() throws IOException { + // System.out.println("Test (24): Update chapter in book draft with new facet content"); + // boolean testStatus = false; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + // // Use unique temp file name to avoid duplicates + // File tempFile = File.createTempFile("test24_sample_", ".pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID5); + // 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, bookEntityName, srvpath, bookID5); + // if (response.equals("Entity in draft mode")) { + // boolean allCreated = true; + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + // String check = facetResponse.get(0); + // if (!check.equals("Attachment created")) { + // allCreated = false; + // System.out.println( + // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + // } + // } + + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } else { + // System.out.println("Could not edit book: " + response); + // } + + // tempFile.delete(); + + // if (!testStatus) { + // fail("Failed to update chapter entity draft with new attachments"); + // } + // } + + // @Test + // @Order(25) + // void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + // throws IOException { + // System.out.println( + // "Test (25): Rename & Update secondary properties for multiple chapter attachments after + // book is saved"); + // System.out.println("Creating book and chapter with multiple attachments"); + + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!"Could not create entity".equals(chapterResponse)) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create PDF attachments + // postData.put("mimeType", "application/pdf"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String[] pdfID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // pdfID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create TXT attachments + // postData.put("mimeType", "application/txt"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // String[] txtID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // txtID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create EXE attachments + // postData.put("mimeType", "application/exe"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // String[] exeID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // exeID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Save book first + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (!"Saved".equals(response)) { + // fail("Could not save book initially"); + // } + + // // Edit book to update chapter attachments + // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (response.equals("Entity in draft mode")) { + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // // Update PDF properties + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String renameResp = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + // String upd3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + // String upd4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + + // if ("Renamed".equals(renameResp) + // && "Updated".equals(upd1) + // && "Updated".equals(upd2) + // && "Updated".equals(upd3) + // && "Updated".equals(upd4)) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // PDF"); + // } + // } + + // // Update TXT properties (only boolean) + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + // String upd = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + // if ("Updated".equals(upd)) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // TXT"); + // } + // } + + // // Update EXE properties (dropdown and int) + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValueExe = integrationTestUtils.getDropDownValue(); + // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyDropdownExe = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + // RequestBody bodyIntExe = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], + // bodyDropdownExe); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // EXE"); + // } + // } + + // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (response.equals("Saved")) { + // System.out.println("Book saved"); + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for chapter + // attachments"); + // } + // } + // } + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property in chapter after book is saved"); + // } + // } + + // @Test + // @Order(26) + // void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() + // throws IOException { + // System.out.println( + // "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter + // facets before book is saved"); + // System.out.println("Creating book and chapter"); + + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (!"Could not create entity".equals(response)) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!"Could not create entity".equals(chapterResponse)) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create PDF attachments + // postData.put("mimeType", "application/pdf"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String[] pdfID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // pdfID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create TXT attachments + // postData.put("mimeType", "application/txt"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // String[] txtID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // txtID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create EXE attachments + // postData.put("mimeType", "application/exe"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // String[] exeID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // exeID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample1234.pdf"; + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + + // // Update PDF properties + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String renameResp = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + // String upd3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + // String upd4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + // String updInvalid = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], + // invalidPropertyPDF); + + // if ("Renamed".equals(renameResp) + // && "Updated".equals(upd1) + // && "Updated".equals(upd2) + // && "Updated".equals(upd3) + // && "Updated".equals(upd4) + // && "Updated".equals(updInvalid)) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // PDF"); + // } + // } + + // // Update TXT properties + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + // String upd = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + // if ("Updated".equals(upd)) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // TXT"); + // } + // } + + // // Update EXE properties + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValueExe = integrationTestUtils.getDropDownValue(); + // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyDropdownExe = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + // RequestBody bodyIntExe = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // EXE"); + // } + // } + + // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + + // // Verify PDF metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); + // assertEquals(expectedNames[0], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertNull(metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } + + // // Verify TXT metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); + // assertEquals(expectedNames[1], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertTrue((Boolean) metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } + + // // Verify EXE metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); + // assertEquals(expectedNames[2], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); + // assertEquals(1234, metadata.get("customProperty2")); + // } + + // // Parse JSON response and check for expected error messages + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved with expected invalid property errors"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessful for invalid properties and successful for valid + // attachments"); + // } + // } + // } + // } + + // if (!testStatus) { + // fail("Could not update secondary property before book is saved"); + // } + // } + + // @Test + // @Order(27) + // void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + // throws IOException { + // System.out.println( + // "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter + // attachments after book is saved"); + + // // Reuse bookID5 and chapterID5 + // System.out.println("Editing book with bookID5: " + bookID5); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // System.out.println("Edit entity response: " + response); + + // if (response.equals("Entity in draft mode")) { + // // Fetch existing attachments from the chapter + // List> attachmentsMeta = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); + // List> referencesMeta = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); + // List> footnotesMeta = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); + + // System.out.println("Attachments count: " + attachmentsMeta.size()); + // System.out.println("References count: " + referencesMeta.size()); + // System.out.println("Footnotes count: " + footnotesMeta.size()); + + // if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) + // { + // String[] pdfID = new String[facet.length]; + // String[] txtID = new String[facet.length]; + // String[] exeID = new String[facet.length]; + + // pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); + // pdfID[1] = (String) referencesMeta.get(0).get("ID"); + // pdfID[2] = (String) footnotesMeta.get(0).get("ID"); + + // txtID[0] = (String) attachmentsMeta.get(1).get("ID"); + // txtID[1] = (String) referencesMeta.get(1).get("ID"); + // txtID[2] = (String) footnotesMeta.get(1).get("ID"); + + // exeID[0] = (String) attachmentsMeta.get(2).get("ID"); + // exeID[1] = (String) referencesMeta.get(2).get("ID"); + // exeID[2] = (String) footnotesMeta.get(2).get("ID"); + + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // // PDF + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); + + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); + + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); + + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); + + // String updateSecondaryPropertyResponse5 = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated") + // && updateSecondaryPropertyResponse5.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // PDF"); + // } + // } + + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); + // String updateSecondaryPropertyResponseTXT1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // TXT"); + // } + // } + + // Integer secondaryPropertyInt3 = 12; + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // for (int i = 0; i < facet.length; i++) { + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); + + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); + + // if (updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // EXE"); + // } + // } + + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // // Note: Don't verify specific filenames since previous tests may have changed them + // System.out.println("Save response: " + response); + + // // Parse JSON response to check for invalid secondary property errors in all three + // tables + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessful for invalid Secondary properties and successful for + // valid property attachments"); + // } else { + // System.out.println("Save response did not match expected: " + response); + // } + // } else { + // System.out.println("Not enough attachments in facets - need at least 3 per facet"); + // } + // } + // } else { + // System.out.println( + // "Could not edit book - it may be stuck in draft mode from a previous test"); + // } + // if (!testStatus) { + // fail("Could not update secondary property before book is saved"); + // } + // } + + // // Tests 28 and 29 removed - chapters have no attachment limit + + // // Tests 28-29 skipped - chapters have no attachment limit + + // @Test + // @Order(30) + // void testDiscardBookDraftWithoutChapterAttachments() { + // System.out.println("Test (30) : Discard book draft without adding chapter attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if (response.equals("Entity Draft Deleted")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft with chapter was not discarded properly"); + // } + // } + + // @Test + // @Order(31) + // void testDiscardBookDraftWithChapterAttachments() throws IOException { + // System.out.println("Test (31): Discard book draft with chapter attachments"); + // boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!"Could not create entity".equals(chapterResponse)) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = + // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // 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 < facet.length; i++) { + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + // if ("Attachment created".equals(createResponse.get(0))) { + // System.out.println("Attachment created in chapter facet: " + facet[i]); + // } else { + // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + // } + // } + + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft with chapter attachments was not discarded properly"); + // } + // } + + // // Tests 32-34 covered in tests 19, 23, 24 + // // Tests 37-41 skipped - copy with notes/secondary properties not applicable + + // @Test + // @Order(42) + // void testCreateLinkSuccessInChapter() throws IOException { + // System.out.println("Test (42): Create link in chapter"); + // List attachments = new ArrayList<>(); + + // // Create book and chapter for link testing + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (response.equals("Could not create entity")) { + // fail("Could not create book"); + // } + // String createLinkBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + // if (chapterResponse.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + // String createLinkChapterID = chapterResponse; + + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // for (String facetName : facet) { + // String createLinkResponse1 = + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + // String createLinkResponse2 = + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", + // linkUrl); + // if (!createLinkResponse1.equals("Link created successfully") + // || !createLinkResponse2.equals("Link created successfully")) { + // fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); + // } + // } + + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // for (String facetName : facet) { + // attachments = + // api + // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) + // .stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String openAttachmentResponse; + // for (String attachment : attachments) { + // openAttachmentResponse = + // api.openAttachment( + // appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open created link in chapter facet : " + facetName); + // } + // } + // } + // api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + // } + + // @Test + // @Order(43) + // void testCreateLinkDifferentChapter() throws IOException { + // System.out.println("Test (43): Create link with same name in different chapter"); + + // // Create new book and chapter + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (response.equals("Could not edit entity")) { + // fail("Could not create book"); + // } + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (chapterResponse.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + // String tempChapterID = chapterResponse; + + // String linkName = "sample"; + // String linkUrl = "https://example.com"; + // for (String facetName : facet) { + // String createResponse = + // api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); + // if (!createResponse.equals("Link created successfully")) { + // fail("Could not create link in different chapter with same name"); + // } + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (!response.equals("Saved")) { + // fail("Could not save book"); + // } + + // response = api.deleteEntity(appUrl, bookEntityName, tempBookID); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(44) + // void testCreateLinkFailureInChapter() throws IOException { + // System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if ("Could not create entity".equals(response)) { + // fail("Could not create book"); + // } + // String createLinkBookID = response; + + // response = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + // if ("Could not create entity".equals(response)) { + // fail("Could not create chapter"); + // } + // String createLinkChapterID = response; + + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + + // ObjectMapper mapper = new ObjectMapper(); + + // for (String facetName : facet) { + + // // Create initial link for this facet first (so duplicate test works) + // response = + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + // if (!"Link created successfully".equals(response)) { + // fail("Could not create initial link for facet: " + facetName); + // } + + // /* ---------- INVALID URL ---------- */ + // try { + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); + // fail("Expected invalid URL error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // assertEquals("400018", error.path("code").asText()); + // assertTrue( + // error.path("message").asText().contains("expected pattern"), + // "Unexpected message: " + error.path("message").asText()); + // } + + // /* ---------- INVALID NAME ---------- */ + // try { + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // createLinkChapterID, + // "sample//", + // "https://example.com"); + // fail("Expected invalid name error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); + + // assertEquals("500", error.path("code").asText()); + // assertTrue( + // message.contains("contains unsupported characters") + // && message.contains("Rename and try again"), + // "Unexpected message: " + message); + // } + + // /* ---------- EMPTY NAME & URL ---------- */ + // try { + // api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); + // fail("Expected missing value error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // assertEquals("409008", error.path("code").asText()); + // assertEquals("Provide the missing value.", error.path("message").asText()); + // } + + // /* ---------- DUPLICATE NAME ---------- */ + // try { + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + // fail("Expected duplicate name error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // assertEquals("500", error.path("code").asText()); + // assertEquals( + // "An object named \"sample\" already exists. Rename the object and try again.", + // error.path("message").asText()); + // } + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + // if (!"Saved".equals(response)) { + // fail("Could not save book"); + // } + + // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + // if (!"Entity Deleted".equals(response)) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(45) + // void testCreateLinkNoSDMRolesInChapter() throws IOException { + // System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); + + // String createLinkBookNoRoles = + // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (createLinkBookNoRoles.equals("Could not edit entity")) { + // fail("Could not create book"); + // } + + // String createLinkChapterNoRoles = + // apiNoRoles.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); + // if (createLinkChapterNoRoles.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // for (String facetName : facet) { + // String linkName = "sample27"; + // String linkUrl = "https://example.com"; + // try { + // apiNoRoles.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); + // if (!response.equals("Saved")) { + // fail("Could not save book"); + // } + + // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(46) + // void testDeleteLinkInChapter() throws IOException { + // System.out.println("Test (46): Delete link in chapter"); + // List> attachments = new ArrayList<>(); + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (response.equals("Could not create entity")) { + // fail("Could not create book"); + // } + // String deleteLinkBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); + // if (chapterResponse.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + // String deleteLinkChapterID = chapterResponse; + + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink( + // appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for chapter facet : " + facetName); + // } + // } + + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // for (String facetName : facet) { + // attachments.add( + // api + // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + // .stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } + + // String editEntityResponse = + // api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // int index = 0; + // for (String facetName : facet) { + // String deleteLinkResponse = + // api.deleteAttachment( + // appUrl, + // chapterEntityName, + // facetName, + // deleteLinkChapterID, + // attachments.get(index).get(0)); + // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + // if (!deleteLinkResponse.equals("Deleted")) { + // fail("Could not delete created link"); + // } + // index += 1; + // } + + // saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // index = 0; + // attachments.clear(); + // for (String facetName : facet) { + // attachments.add( + // api + // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + // .stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // System.out.println( + // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + // if (attachments.get(index).size() != 0) { + // fail("Link wasn't deleted"); + // } + // index += 1; + // } + + // response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(35) + // void testCopyAttachmentsToNewChapterInSameBook() throws IOException { + // System.out.println( + // "Test (35): Copy attachments from one chapter to another new chapter in the same book"); + + // // Create source book and chapter with attachments + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Load original files for copying content + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(new ArrayList<>()); + // } + + // // Create attachments in all facets - each upload needs a unique filename + // int fileCounter = 0; + // for (int i = 0; i < facet.length; i++) { + // boolean useTxt = (i == 1); // Use txt for references facet + // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + // File originalFile = useTxt ? originalTxt : originalPdf; + // String extension = useTxt ? ".txt" : ".pdf"; + + // for (int j = 0; j < 2; j++) { // Create 2 attachments per facet + // // Create unique temp file for EACH upload to avoid duplicate filename errors + // fileCounter++; + // File tempFile = + // File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, + // tempFile); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // System.out.println("Created attachment ID: " + createResponse.get(1)); + // } else { + // System.out.println("Failed to create attachment: " + createResponse.get(0)); + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + // } + + // // Fetch object IDs from source attachments + // List objectIds = new ArrayList<>(); + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + // if (metadata.containsKey("objectId")) { + // objectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // } + + // // Save the source book + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Create target chapter in the SAME book + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source book for adding target chapter"); + // } + + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (targetChapterID.equals("Could not create entity")) { + // fail("Could not create target chapter in same book"); + // } + + // // Copy attachments to target chapter + // int objectIdIndex = 0; + // for (String facetName : facet) { + // List facetObjectIds = + // objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // facetObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + // } + + // // Fetch and wait for copied attachments + // List> copiedMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // for (Map meta : copiedMetadata) { + // String copiedId = (String) meta.get("ID"); + // } + // objectIdIndex += 2; + // } + + // // Save the book with new chapter + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book after copying attachments"); + // } + + // // Verify attachments were copied - read them + // for (String facetName : facet) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.size() != 2) { + // fail("Expected 2 attachments in facet " + facetName + ", found " + + // targetMetadata.size()); + // } + // for (Map meta : targetMetadata) { + // String attachmentId = (String) meta.get("ID"); + // String readResponse = + // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // attachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment in facet: " + facetName); + // } + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // } + + // @Test + // @Order(36) + // void testCopyAttachmentsToChapterInDifferentBook() throws IOException { + // System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); + + // // Create Book1 with source chapter and attachments + // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create Book2 with target chapter + // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Load original files for copying content + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(new ArrayList<>()); + // } + + // // Create attachments in all facets of source chapter - each upload needs unique filename + // int fileCounter = 0; + // for (int i = 0; i < facet.length; i++) { + // boolean useTxt = (i == 1); // Use txt for references facet + // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + // File originalFile = useTxt ? originalTxt : originalPdf; + // String extension = useTxt ? ".txt" : ".pdf"; + + // for (int j = 0; j < 2; j++) { + // // Create unique temp file for EACH upload to avoid duplicate filename errors + // fileCounter++; + // File tempFile = + // File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // System.out.println("Created attachment ID: " + createResponse.get(1)); + // } else { + // System.out.println("Failed to create attachment: " + createResponse.get(0)); + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + // } + + // // Fetch object IDs + // sourceObjectIds.clear(); + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // } + + // // Save Book1 + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Copy attachments from Book1's chapter to Book2's chapter + // if (sourceObjectIds.size() == 6) { + // int objectIdIndex = 0; + // for (String facetName : facet) { + // List facetObjectIds = + // sourceObjectIds.subList( + // objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, + // facetObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + // } + + // objectIdIndex += 2; + // } + + // // Save Book2 + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book after copying attachments"); + // } + + // // Verify attachments were copied + // for (String facetName : facet) { + // List> targetMetadata = + // api.fetchEntityMetadata( + // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); + // if (targetMetadata.size() != 2) { + // fail("Expected 2 attachments in facet " + facetName + ", found " + + // targetMetadata.size()); + // } + // for (Map meta : targetMetadata) { + // String attachmentId = (String) meta.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, + // attachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment in facet: " + facetName); + // } + // } + // } + + // // Cleanup - delete both books after verification + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // } else { + // fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); + // } + // } + + // @Test + // @Order(37) + // void testCopyAttachmentsWithNotePreserved() throws IOException { + // System.out.println("Test (37): Copy attachments with note field preserved"); + + // // Create source book and chapter + // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create attachments with notes in source chapter + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] sourceAttachmentIds = new String[facet.length]; + // String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); + + // for (int i = 0; i < facet.length; i++) { + // // Create unique temp file for each facet + // File tempFile = + // File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), + // ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // sourceAttachmentIds[i] = createResponse.get(1); + + // // Update note field using RequestBody + // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + // String noteResponse = + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // noteBody); + // if (!noteResponse.equals("Updated")) { + // fail("Could not update note for attachment in facet: " + facet[i]); + // } + // System.out.println("Note updated for facet: " + facet[i]); + // } + + // // Save source book + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Verify notes were saved in source + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // if (!testNote.equals(metadata.get("note"))) { + // fail("Note not saved correctly in source for facet: " + facet[i]); + // } + // } + + // // Create target book and chapter + // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Get object IDs and copy attachments + // for (int i = 0; i < facet.length; i++) { + // Map sourceMetadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // String objectId = sourceMetadata.get("objectId").toString(); + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // System.out.println("Attachment copied to facet: " + facet[i]); + // } + + // // Save target book + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify notes were preserved in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], + // copyAttachmentTargetChapter); + // if (targetMetadata.isEmpty()) { + // fail("No attachments found in target facet: " + facet[i]); + // } + // Map copiedAttachment = targetMetadata.get(0); + // String copiedNote = (String) copiedAttachment.get("note"); + // if (!testNote.equals(copiedNote)) { + // fail( + // "Note not preserved after copy in facet: " + // + facet[i] + // + ". Expected: " + // + testNote + // + ", Got: " + // + copiedNote); + // } + // System.out.println("Note preserved in target facet: " + facet[i]); + // } + + // System.out.println("Test 37 passed - notes preserved during copy"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // copyAttachmentSourceBook = null; + // copyAttachmentTargetBook = null; + // } + + // @Test + // @Order(38) + // void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { + // System.out.println("Test (38): Copy attachments with secondary properties preserved"); + + // // Use entities from test 37 or create new ones if needed + // boolean sourceBookJustCreated = false; + // boolean targetBookJustCreated = false; + + // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + // copyAttachmentSourceBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + // sourceBookJustCreated = true; + // } + + // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + // copyAttachmentTargetBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + // targetBookJustCreated = true; + // } + + // // If source book was just created, save it first before we can edit it + // if (sourceBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created source book"); + // } + // } + + // // If target book was just created, save it first + // if (targetBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created target book"); + // } + // } + + // // Edit source book + // String editResponse = + // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source book"); + // } + + // // Create new attachments with secondary properties + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] sourceAttachmentIds = new String[facet.length]; + // Boolean testBooleanProp = true; + // Integer testIntegerProp = 12345; + + // for (int i = 0; i < facet.length; i++) { + // // Create unique temp file + // File tempFile = + // File.createTempFile( + // "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // sourceAttachmentIds[i] = createResponse.get(1); + + // // Update secondary properties using RequestBody (customProperty6 - Boolean, + // customProperty2 - + // // Integer) + // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + // String boolResponse = + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // boolBody); + // if (!boolResponse.equals("Updated")) { + // System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); + // } + + // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + // String intResponse = + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // intBody); + // if (!intResponse.equals("Updated")) { + // System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); + // } + + // System.out.println("Secondary properties updated for facet: " + facet[i]); + // } + + // // Save source book + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Edit target book + // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // // Copy attachments to target + // for (int i = 0; i < facet.length; i++) { + // Map sourceMetadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // String objectId = sourceMetadata.get("objectId").toString(); + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); + // } + + // // Save target book + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify secondary properties were preserved in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], + // copyAttachmentTargetChapter); + + // // Find the attachment we just copied (most recent one) + // boolean found = false; + // for (Map attachment : targetMetadata) { + // Object boolProp = attachment.get("customProperty6"); + // Object intProp = attachment.get("customProperty2"); + + // if (boolProp != null && intProp != null) { + // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { + // found = true; + // System.out.println("Secondary properties preserved in target facet: " + facet[i]); + // break; + // } + // } + // } + // if (!found) { + // System.out.println( + // "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); + // } + // } + + // System.out.println("Test 38 passed - secondary properties checked during copy"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // copyAttachmentSourceBook = null; + // copyAttachmentTargetBook = null; + // } + + // @Test + // @Order(39) + // void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { + // System.out.println( + // "Test (39): Copy attachments with both note and secondary properties preserved"); + + // // Use entities from previous tests or create new ones + // boolean sourceBookJustCreated = false; + // boolean targetBookJustCreated = false; + + // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + // copyAttachmentSourceBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + // sourceBookJustCreated = true; + // } + + // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + // copyAttachmentTargetBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + // targetBookJustCreated = true; + // } + + // // If source book was just created, save it first before we can edit it + // if (sourceBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created source book"); + // } + // } + + // // If target book was just created, save it first + // if (targetBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created target book"); + // } + // } + + // // Edit source book + // String editResponse = + // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source book"); + // } + + // // Create new attachments with both note and secondary properties + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] sourceAttachmentIds = new String[facet.length]; + // String testNote = "Combined test note - " + System.currentTimeMillis(); + // Boolean testBooleanProp = true; + // Integer testIntegerProp = 99999; + + // for (int i = 0; i < facet.length; i++) { + // // Create unique temp file + // File tempFile = + // File.createTempFile( + // "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // sourceAttachmentIds[i] = createResponse.get(1); + + // // Update note using RequestBody + // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // noteBody); + + // // Update secondary properties using RequestBody + // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // boolBody); + + // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // intBody); + + // System.out.println("Note and secondary properties updated for facet: " + facet[i]); + // } + + // // Save source book + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Edit target book + // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // // Copy attachments to target + // for (int i = 0; i < facet.length; i++) { + // Map sourceMetadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // String objectId = sourceMetadata.get("objectId").toString(); + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // System.out.println("Attachment with note and properties copied to facet: " + facet[i]); + // } + + // // Save target book + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify note and secondary properties were preserved in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], + // copyAttachmentTargetChapter); + + // boolean noteFound = false; + // boolean propsFound = false; + + // for (Map attachment : targetMetadata) { + // String copiedNote = (String) attachment.get("note"); + // Object boolProp = attachment.get("customProperty6"); + // Object intProp = attachment.get("customProperty2"); + + // if (testNote.equals(copiedNote)) { + // noteFound = true; + // System.out.println("Note preserved in target facet: " + facet[i]); + // } + + // if (boolProp != null && intProp != null) { + // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { + // propsFound = true; + // System.out.println("Secondary properties preserved in target facet: " + facet[i]); + // } + // } + // } + + // if (!noteFound) { + // System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); + // } + // if (!propsFound) { + // System.out.println( + // "Warning: Secondary properties may not be preserved in facet: " + facet[i]); + // } + // } + + // // Cleanup - delete both books + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + + // // Reset static variables + // copyAttachmentSourceBook = null; + // copyAttachmentTargetBook = null; + // copyAttachmentSourceChapter = null; + // copyAttachmentTargetChapter = null; + + // System.out.println("Test 39 passed - both note and secondary properties checked during + // copy"); + // } + + // @Test + // @Order(40) + // void testCopyAttachmentsWithInvalidObjectId() throws IOException { + // System.out.println("Test (40): Copy attachments with invalid object ID should fail"); + + // // Create independent test entities (don't rely on previous tests) + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create test book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create test chapter"); + // } + + // // Save the book first so it's not in draft mode + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save test book"); + // } + + // // Now edit it to test copy with invalid object IDs + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit test book"); + // } + + // // Try to copy with invalid object ID + // for (String facetName : facet) { + // try { + // List invalidObjectIds = new ArrayList<>(); + // invalidObjectIds.add("invalidObjectId123"); + // invalidObjectIds.add("anotherInvalidId456"); + // api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, + // invalidObjectIds); + // fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); + // } catch (IOException e) { + // // Expected - copy should fail with invalid object ID + // System.out.println( + // "Expected error received for invalid object ID in facet " + // + facetName + // + ": " + // + e.getMessage()); + // } + // } + + // // Save and cleanup + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + + // // Also cleanup test 36 entities if they exist + // if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { + // try { + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // } catch (Exception e) { + // // Ignore - may already be deleted + // } + // } + // if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { + // try { + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // } catch (Exception e) { + // // Ignore - may already be deleted + // } + // } + // } + + // @Test + // @Order(41) + // void testCopyAttachmentsToExistingChapter() throws IOException { + // System.out.println( + // "Test (41): Copy attachments to an existing chapter that already has attachments"); + + // // Create Book1 with source chapter + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create Book2 with target chapter that has existing attachments + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (targetBookID.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + // if (targetChapterID.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Create temp files with unique names to avoid duplicate filename errors + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test41_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create attachment in source chapter + // List sourceObjIds = new ArrayList<>(); + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create source attachment"); + // } + // String attachmentId = createResponse.get(1); + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + // sourceObjIds.add(metadata.get("objectId").toString()); + // } + + // // Create existing attachment in target chapter + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", targetChapterID); + // postData.put("mimeType", "text/plain"); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create existing target attachment"); + // } + // String attachmentId = createResponse.get(1); + // } + + // // Save both books + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Edit target book and copy attachments + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // // Copy from source to target (target already has 1 attachment per facet) + // for (int i = 0; i < facet.length; i++) { + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjIds.get(i)); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, + // objectIdsToCopy); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // } + + // // Save target book + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) + // for (String facetName : facet) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.size() != 2) { + // fail( + // "Expected 2 attachments in facet " + // + facetName + // + " (1 existing + 1 copied), found " + // + targetMetadata.size()); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // // ============= LINK RENAME TESTS (47-49) ============= + + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (47): Rename link in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links in all facets + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit and rename links + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // String renameResponse = + // api.renameAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); + // if (!renameResponse.equals("Renamed")) { + // fail("Could not rename link in facet: " + facetName); + // } + // } + + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book after renaming links"); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(48) + // void testRenameLinkDuplicate() throws IOException { + // System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create two links in all facets + // for (String facetName : facet) { + // String createLinkResponse1 = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "link1", + // "https://www.example1.com"); + // String createLinkResponse2 = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "link2", + // "https://www.example2.com"); + // if (!createLinkResponse1.equals("Link created successfully") + // || !createLinkResponse2.equals("Link created successfully")) { + // fail("Could not create links in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit and try to rename link2 to link1 (duplicate) + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.size() < 2) { + // fail("Expected 2 links in facet: " + facetName); + // } + + // // Find link2 and rename to link1 + // for (Map attachment : attachments) { + // if ("link2".equals(attachment.get("fileName"))) { + // String linkId = (String) attachment.get("ID"); + // api.renameAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); + // break; + // } + // } + // } + + // // Save should fail with duplicate error + // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // ObjectMapper mapper = new ObjectMapper(); + // try { + // JsonNode errorJson = mapper.readTree(saveError); + // String errorMessage = errorJson.path("error").path("message").asText(); + // if (!errorMessage.contains("already exists")) { + // fail("Expected duplicate error but got: " + saveError); + // } + // } catch (Exception e) { + // if (!saveError.contains("already exists")) { + // fail("Expected duplicate error but got: " + saveError); + // } + // } + + // // Cleanup + // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(49) + // void testRenameLinkUnsupportedCharacters() throws IOException { + // System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links in all facets + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit and rename with unsupported characters + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // api.renameAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); + // } + + // // Save should fail with unsupported characters error + // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveError.contains("unsupported characters")) { + // fail("Expected unsupported characters error but got: " + saveError); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // // ============= LINK EDIT TESTS (50-53) ============= + + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (50): Edit existing link URL in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links in all facets + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit links + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // String editLinkResponse = + // api.editLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // linkId, + // "https://www.editedexample.com"); + // if (!editLinkResponse.equals("Link edited successfully")) { + // fail("Could not edit link in facet: " + facetName); + // } + // } + + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book after editing links"); + // } + + // // Verify links open successfully + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // for (Map attachment : attachments) { + // String linkId = (String) attachment.get("ID"); + // String openResponse = + // api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); + // if (!openResponse.equals("Attachment opened successfully")) { + // fail("Could not open edited link in facet: " + facetName); + // } + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(51) + // void testEditLinkFailureInvalidURL() throws IOException { + // System.out.println("Test (51): Edit link with invalid URL fails in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // try { + // api.editLink( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, + // "https://editedexample"); + // fail("Edit link should have failed with invalid URL in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected error received for invalid URL in facet " + facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(52) + // void testEditLinkFailureEmptyURL() throws IOException { + // System.out.println("Test (52): Edit link with empty URL fails in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // try { + // api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); + // fail("Edit link should have failed with empty URL in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected error received for empty URL in facet " + facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, + // testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // try { + // apiNoRoles.editLink( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, + // "https://www.edited.com"); + // fail("Edit link should have failed without SDM roles in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected permission error received in facet " + facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // // ============= COPY LINK TESTS (54-58) ============= + + // @Test + // @Order(54) + // void testCopyLinkSuccessNewChapter() throws IOException { + // System.out.println("Test (54): Copy link from one chapter to another new chapter"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // if (sourceChapterID.equals("Could not create entity") + // || targetChapterID.equals("Could not create entity")) { + // fail("Could not create chapters"); + // } + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // // Create links in source chapter + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch object IDs + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // // Copy links to target chapter + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link for facet " + facetName + ": " + copyResponse); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify link type and URL + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.isEmpty()) { + // fail("No links found in target chapter for facet: " + facetName); + // } + + // Map copiedLink = targetMetadata.get(0); + // String receivedUrl = (String) copiedLink.get("linkUrl"); + // assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); + + // objectIdIndex++; + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(55) + // void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { + // System.out.println("Test (55): Copy invalid link object ID to chapter fails"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // for (String facetName : facet) { + // try { + // List invalidObjectIds = new ArrayList<>(); + // invalidObjectIds.add("incorrectObjectId"); + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // invalidObjectIds); + // fail("Copy should have thrown error for invalid object ID in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected error received for invalid object ID in facet " + + // facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(56) + // void testCopyLinkToExistingChapter() throws IOException { + // System.out.println("Test (56): Copy link to existing chapter that has attachments"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // // Create links in source chapter + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sourceLink" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in source chapter for facet: " + facet[i]); + // } + // } + + // // Create existing links in target chapter + // for (int i = 0; i < facet.length; i++) { + // String linkName = "existingLink" + i; + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facet[i], + // targetChapterID, + // linkName, + // "https://www.existing.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create existing link in target chapter for facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch source object IDs + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // // Copy links to target chapter + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link for facet " + facetName); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify target has 2 links (existing + copied) + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.size() != 2) { + // fail( + // "Expected 2 links in target chapter facet " + // + facetName + // + ", found " + // + targetMetadata.size()); + // } + + // objectIdIndex++; + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(57) + // void testCopyLinkNoSDMRoles() throws IOException { + // System.out.println("Test (57): Copy link fails due to no SDM roles"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch object IDs + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // // Try to copy with no SDM roles + // int objectIdIndex = 0; + // for (String facetName : facet) { + // try { + // // Use normal api to put book in draft mode + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // // Use apiNoRoles to attempt copy (should fail) + // apiNoRoles.copyAttachment( + // appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + // fail("Copy should have failed without SDM roles in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected permission error in facet " + facetName); + // // Discard draft to clean up for next iteration + // api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); + // } + // objectIdIndex++; + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(58) + // void testCopyLinkFromDraftChapter() throws IOException { + // System.out.println("Test (58): Copy link from draft chapter to another chapter"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // // Create links in source chapter (NOT saved yet - draft mode) + // for (int i = 0; i < facet.length; i++) { + // String linkName = "draftLink" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + + // // Save target book only + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch object IDs from draft + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // if (linkObjectIds.size() != facet.length) { + // fail("Could not fetch all object IDs from draft"); + // } + + // // Copy links from draft to target + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link from draft for facet " + facetName); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify link was copied + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.isEmpty()) { + // fail("No links found in target chapter for facet: " + facetName); + // } + + // objectIdIndex++; + // } + + // // Cleanup + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= + + // @Test + // @Order(59) + // void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { + // System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // if (sourceChapterID.equals("Could not create entity") + // || targetChapterID.equals("Could not create entity")) { + // fail("Could not create chapters"); + // } + + // // Create temp files with unique names + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test59_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List sourceObjectIds = new ArrayList<>(); + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(new ArrayList<>()); + // } + + // // Create attachments in source chapter (still in draft) + // for (int i = 0; i < facet.length; i++) { + // postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); + // File file = i == 1 ? tempTxt : tempPdf; + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + + // // Fetch object IDs from draft + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // } + + // // Save target book only + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Copy attachments from draft to target + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment from draft for facet " + facetName); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify attachment was copied + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.isEmpty()) { + // fail("No attachments found in target chapter for facet: " + facetName); + // } + + // // Read attachment to verify + // String attachmentId = (String) targetMetadata.get(0).get("ID"); + // String readResponse = + // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // attachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment in facet: " + facetName); + // } + + // objectIdIndex++; + // } + + // // Cleanup + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // // ============= CHANGELOG TESTS (60-64) ============= + + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println("Test (60): View changelog for newly created attachment in chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String facetName = facet[i]; + + // // Create book and chapter + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = + // File.createTempFile( + // "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } + + // String attachmentId = createResponse.get(1); + + // // Fetch changelog + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); + + // // Cleanup + // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + // } + // } + + // @Test + // @Order(61) + // void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { + // System.out.println("Test (61): Changelog after modifying note and custom property in + // chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String facetName = facet[i]; + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = + // File.createTempFile( + // "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Update note + // String notesValue = "Test note for changelog verification"; + // RequestBody updateNotesBody = + // RequestBody.create( + // MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); + + // // Update custom property + // RequestBody bodyInt = + // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": + // 12345}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); + + // // Save + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // // Fetch changelog + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // int numItems = (int) changelogResponse.get("numItems"); + // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + // } + + // @Test + // @Order(62) + // void testChangelogAfterRenamingAttachment() throws IOException { + // System.out.println("Test (62): Changelog after renaming attachment in chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String facetName = facet[i]; + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = + // File.createTempFile( + // "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Rename attachment + // String renameResponse = + // api.renameAttachment( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // attachmentId, + // "renamed_file.txt"); + // if (!renameResponse.equals("Renamed")) { + // fail("Could not rename attachment"); + // } + + // // Save + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // // Fetch changelog + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // int numItems = (int) changelogResponse.get("numItems"); + // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + // } + + // @Test + // @Order(63) + // void testChangelogForCopiedAttachment() throws IOException { + // System.out.println("Test (63): Changelog for copied attachment in chapter"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), + // ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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 in source + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Save both books + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Get object ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + + // // Copy to target + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment"); + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch changelog for copied attachment + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + // String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); + + // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // Map changelogResponse = + // api.fetchChangelog( + // appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // int numItems = (int) changelogResponse.get("numItems"); + // assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(64) + // void testChangelogForNewChapter() throws IOException { + // System.out.println("Test (64): Changelog for attachment in newly created chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), + // ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Fetch changelog before saving + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // assertEquals( + // 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); + + // @SuppressWarnings("unchecked") + // List> changeLogs = + // (List>) changelogResponse.get("changeLogs"); + // assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); + + // // Cleanup + // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + // } + + // // ============= MOVE ATTACHMENT TESTS (65-75) ============= + + // @Test + // @Order(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println("Test (65): Move attachments from source chapter to target chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create temp files + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), + // tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List sourceAttachmentIds = new ArrayList<>(); + // File[] files = {tempPdf, tempTxt}; + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source chapter"); + // } + // } + + // // Save source book + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Get object IDs and folder ID + // List moveObjectIds = new ArrayList<>(); + // String sourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, + // attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (sourceFolderId == null && metadata.containsKey("folderId")) { + // sourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } + + // // Create target book and chapter + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (targetBookID.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + // if (targetChapterID.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Save target book before moving attachments (moveAttachments requires Active entity) + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book before move"); + // } + + // // Move attachments to Active entity + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } + + // // Verify + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadata.size(), + // "Target should have all attachments after move"); + + // List> sourceMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // } + // } + + // @Test + // @Order(66) + // void testMoveAttachmentsToChapterWithDuplicate() throws IOException { + // System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Create attachment in source with specific name + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, + // originalPdf); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create source attachment"); + // } + // String sourceAttachmentId = createResponse.get(1); + + // // Create attachment in target with SAME name (duplicate) + // postData.put("up__ID", targetChapterID); + // createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, + // originalPdf); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create target attachment"); + // } + + // // Save both + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Get source object ID and folder ID + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, + // sourceAttachmentId); + // String objectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move to saved target + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Move should handle duplicate - attachment stays in source + + // // Verify source still has attachment (duplicate not moved) + // List> sourceMetadataAfter = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertTrue( + // sourceMetadataAfter.size() >= 1, + // "Source should still have attachment when duplicate exists in target"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(67) + // void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { + // System.out.println("Test (67): Move attachments with notes and secondary properties"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // // Add note and secondary property + // String testNote = "Test note for move"; + // RequestBody noteBody = + // RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + + // "\"}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); + + // RequestBody propBody = + // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); + + // // Save source + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get object ID and folder ID + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // // Create target + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Save target before move + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null"); + // } + + // // Verify note was preserved + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + // if (!targetMetadata.isEmpty()) { + // String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); + // Map movedMetadata = + // api.fetchMetadata( + // appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); + + // // Note should be preserved + // if (movedMetadata.containsKey("note")) { + // assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(68) + // void testMoveAttachmentsPartialFailure() throws IOException { + // System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get real object ID and folder ID + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String realObjectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // // Create target + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Save target before move + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Try to move with mix of valid and invalid object IDs + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(realObjectId); + // moveObjectIds.add("invalidObjectId123"); + + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Should handle partial failure + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(69) + // void testMoveAttachmentsEmptyList() throws IOException { + // System.out.println("Test (69): Move attachments with empty object ID list"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Try to move with empty list + // List emptyObjectIds = new ArrayList<>(); + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + + // try { + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // "someFolderId", + // emptyObjectIds, + // targetFacet, + // sourceFacet); + // // Should either fail or do nothing + // } catch (Exception e) { + // System.out.println("Expected: Move with empty list handled: " + e.getMessage()); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(70) + // void testMoveAttachmentsToSameChapter() throws IOException { + // System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // 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"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Get object ID and folder ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String folderId = metadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move to same chapter + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // testChapterID, + // folderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Should handle gracefully - attachment stays in place + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Verify attachment still exists + // List> metadataAfter = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); + // assertEquals(1, metadataAfter.size(), "Attachment should still exist"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(71) + // void testMoveAttachmentsBetweenFacets() throws IOException { + // System.out.println("Test (71): Move attachments between different facets in chapters"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create in attachments facet + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Get object ID and folder ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String sourceFolderId = metadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move from attachments to references facet + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[1], // references facet + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify moved to different facet + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); + // assertTrue( + // targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(72) + // void testMoveMultipleAttachments() throws IOException { + // System.out.println("Test (72): Move multiple attachments at once between chapters"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // // Create multiple temp files + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test72_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List sourceAttachmentIds = new ArrayList<>(); + // File[] files = {tempPdf, tempTxt}; + // String[] mimeTypes = {"application/pdf", "text/plain"}; + + // for (int i = 0; i < files.length; i++) { + // postData.put("mimeType", mimeTypes[i]); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get object IDs + // List moveObjectIds = new ArrayList<>(); + // String sourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (sourceFolderId == null) { + // sourceFolderId = metadata.get("folderId").toString(); + // } + // } + + // // Create target + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Save target before move + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Move all at once + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify all moved + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadata.size(), + // "All attachments should be moved to target"); + + // List> sourceMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(73) + // void testMoveAttachmentsAllFacets() throws IOException { + // System.out.println("Test (73): Move attachments from all facets between chapters"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // // Create attachment in each facet + // for (int i = 0; i < facet.length; i++) { + // String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); + // File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Move from each facet + // for (int i = 0; i < facet.length; i++) { + // List> sourceMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // if (sourceMetadata.isEmpty()) { + // continue; + // } + + // String attachmentId = (String) sourceMetadata.get(0).get("ID"); + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String sourceFolderId = metadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + // } + + // // Verify all facets have attachments in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + // assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + + // facet[i]); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(74) + // void testChainMoveAttachments() throws IOException { + // System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || target1BookID.equals("Could not create entity") + // || target2BookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String target1ChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); + // String target2ChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), + // ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); + + // // First move: Source -> Target1 + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move to target1 + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // target1ChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify in target1 + // List> target1Metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + // assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); + + // // Second move: Target1 -> Target2 + // String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); + // Map target1AttMetadata = + // api.fetchMetadata( + // appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); + // String target1ObjectId = target1AttMetadata.get("objectId").toString(); + // String target1FolderId = target1AttMetadata.get("folderId").toString(); + + // moveObjectIds.clear(); + // moveObjectIds.add(target1ObjectId); + + // // Move to target2 + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // target2ChapterID, + // target1FolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify final state + // List> target2Metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); + // assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); + + // target1Metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + // assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); + + // List> sourceFinalMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, target1BookID); + // api.deleteEntity(appUrl, bookEntityName, target2BookID); + // } + + // @Test + // @Order(75) + // void testMoveAttachmentsWithoutSDMRole() throws IOException { + // System.out.println("Test (75): Move attachments fails without SDM role"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = + // File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get object ID and folder ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String sourceFolderId = metadata.get("folderId").toString(); + + // // Create target with no role user + // String targetBookID = + // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (targetBookID.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // String targetChapterID = + // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, + // targetBookID); + + // // Save target before move + // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // boolean moveFailed = false; + // String errorMessage = null; + + // try { + // Map moveResult = + // apiNoRoles.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null || moveResult.containsKey("error")) { + // moveFailed = true; + // errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; + // } + // } catch (Exception e) { + // moveFailed = true; + // errorMessage = e.getMessage(); + // } + + // assertTrue(moveFailed, "Move should fail without SDM role"); + // System.out.println("Move correctly failed without SDM role: " + errorMessage); + + // // Verify source still has attachment + // List> sourceMetadataAfter = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(76) + // void testRenameChapterAttachmentWithExtensionChange() throws IOException { + // System.out.println( + // "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all + // facets - should return extension change warning"); + + // // Step 1: Create a new book and chapter + // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (newBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + // String newChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + // if (newChapterID.equals("Could not create entity")) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not create chapter"); + // } + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book: " + saveResponse); + // } + + // // Step 2: Upload a PDF attachment to each facet in the chapter + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", newChapterID); + // 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, bookEntityName, srvpath, newBookID); + // if (!"Entity in draft mode".equals(editResponse)) { + // fail("Could not put book in draft mode for PDF upload"); + // } + + // String[] facetAttachmentIDs = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // facetAttachmentIDs[i] = + // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + // if (facetAttachmentIDs[i] == null) { + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + // } + // } + + // // Step 3: Save the book + // String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // if (!savedAfterUpload.equals("Saved")) { + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not save book after PDF upload: " + savedAfterUpload); + // } + + // // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt + // for (int i = 0; i < facet.length; i++) { + // String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // if (!"Entity in draft mode".equals(editDraftResponse)) { + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not put book in draft mode for rename on facet: " + facet[i]); + // } + + // String renameResponse = + // api.renameAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // newChapterID, + // facetAttachmentIDs[i], + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + // } + + // // Step 6: Save and validate the extension change warning message + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } + + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + // } + + // // Clean up + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // } + + // @Test + // @Order(77) + // void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { + // System.out.println( + // "Test (77) : Upload chapter attachment in draft, rename changing extension before save + // across all facets - should return extension change warning"); + + // for (int i = 0; i < facet.length; i++) { + // // Step 1: Create a new book and chapter draft (do NOT save) + // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (newBookID.equals("Could not create entity")) { + // fail("Could not create book for facet: " + facet[i]); + // } + // String newChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + // if (newChapterID.equals("Could not create entity")) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not create chapter for facet: " + facet[i]); + // } + + // // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft + // (unsaved) + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", newChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String facetAttachmentID = + // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + // if (facetAttachmentID == null) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + // } + + // // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not + // saved + // String renameResponse = + // api.renameAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // newChapterID, + // facetAttachmentID, + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + // } + + // // Step 4: Save the book — should receive extension change warning, not "Saved" + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } + + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + + // // Clean up + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // } + // } @Test @Order(78) 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 3afbcee4f..b07527109 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 @@ -2,18 +2,14 @@ import static org.junit.jupiter.api.Assertions.*; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; 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.*; @@ -392,6073 +388,6237 @@ void testUploadAttachmentWithoutSDMRole() throws IOException { } } - @Test - @Order(7) - void testUploadSingleAttachmentPDFDuplicate() throws IOException { - System.out.println("Test (7) : Upload duplicate pdf"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Boolean testStatus = false; - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID); - 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, entityID); - if (response == "Entity in draft mode") { - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - testStatus = false; - } else { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - String expectedJson = - "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" already exists. Rename the object and try again.\"}}"; - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode actualJsonNode = objectMapper.readTree(check); - JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); - if (expectedJsonNode.equals(actualJsonNode)) { - testStatus = true; - } - } - } - } - if (!testStatus) { - fail("Attachment created"); - } - } - - @Test - @Order(8) - void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { - System.out.println("Test (8) : Upload duplicate pdf in different entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - entityID2 = response; - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if (response == "Saved") { - response = api.checkEntity(appUrl, entityName, entityID2); - if (response.equals("Entity exists")) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Could not create entity"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID2); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - if (response == "Entity in draft mode") { - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, file); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - attachmentID4 = createResponse.get(1); - response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, attachmentID4); - if (response.equals("OK")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if (response.equals("Saved")) { - response = api.readAttachment(appUrl, entityName, facetName, entityID2, attachmentID4); - - if (response.equals("OK")) { - testStatus = true; - } - } - } - } - } - if (!testStatus) { - fail("Could not upload sample.pdf " + response); - } - } - - @Test - @Order(9) - void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { - System.out.println("Test (9): Create attachment with restricted character in filename"); - - 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", entityID); - 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, entityID); - if (response.equals("Entity in draft mode")) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - attachmentID6 = createResponse.get(1); - - String restrictedFilename = "a/\\bc.pdf"; - response = - api.renameAttachment( - appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); - - if (response.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - api.renameAttachment( - appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if ("Saved".equals(response)) testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - } - if (!testStatus) { - fail("Attachment created with restricted character in filename"); - } - } - - @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); - - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } - - // Store objectId in array - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.isEmpty()) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(0, sourceObjectId); - } - - 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); - } - - // Create target entity - copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } - - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array - - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } - - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } - - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } - - // 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); - } - - // 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 (readResponse.equals("OK")) { - testStatus = true; - } - if (!testStatus) { - fail("Could not verify that notes field was copied from source to target attachment"); - } - } - - @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(7) + // void testUploadSingleAttachmentPDFDuplicate() throws IOException { + // System.out.println("Test (7) : Upload duplicate pdf"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Boolean testStatus = false; - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample1.pdf").getFile()); + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response == "Entity in draft mode") { + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // testStatus = false; + // } else { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // String expectedJson = + // "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" + // already exists. Rename the object and try again.\"}}"; + // ObjectMapper objectMapper = new ObjectMapper(); + // JsonNode actualJsonNode = objectMapper.readTree(check); + // JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); + // if (expectedJsonNode.equals(actualJsonNode)) { + // testStatus = true; + // } + // } + // } + // } + // if (!testStatus) { + // fail("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"); + // @Test + // @Order(8) + // void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { + // System.out.println("Test (8) : Upload duplicate pdf in different entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + // entityID2 = response; + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if (response == "Saved") { + // response = api.checkEntity(appUrl, entityName, entityID2); + // if (response.equals("Entity exists")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Could not create entity"); + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID2); + // 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); - } + // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + // if (response == "Entity in draft mode") { + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, + // file); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // attachmentID4 = createResponse.get(1); + // response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, + // attachmentID4); + // if (response.equals("OK")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if (response.equals("Saved")) { + // response = api.readAttachment(appUrl, entityName, facetName, entityID2, + // attachmentID4); + + // if (response.equals("OK")) { + // testStatus = true; + // } + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not upload sample.pdf " + response); + // } + // } - // 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(9) + // void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { + // System.out.println("Test (9): Create attachment with restricted character in filename"); - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity. Response: " + saveSourceResponse); - } + // boolean testStatus = false; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // Fetch attachment metadata to get objectId and verify secondary properties - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // 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 response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Entity in draft mode")) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // attachmentID6 = createResponse.get(1); + + // String restrictedFilename = "a/\\bc.pdf"; + // response = + // api.renameAttachment( + // appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); + + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // api.renameAttachment( + // appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if ("Saved".equals(response)) testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // } + // if (!testStatus) { + // fail("Attachment created with restricted character in filename"); + // } + // } - // 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); - } + // @Test + // @Order(10) + // void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { + // System.out.println("Test (10): Upload attachments, delete one and create entity"); - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment. Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } + // 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); + // } - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target 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); - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Failed to create entity after deleting one attachment"); + // } + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // @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()); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // 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"); - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // 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); + // } - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in 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"); + // } + // } - // 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); + // @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 (copiedAttachmentMetadata == null) { - fail("Could not find the copied attachment with file 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"); + // } + // } - 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); - } + // @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 customProperty2 - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied. Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // @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); + // } + // } - // 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 (!testStatus) { + // fail("Multiple renames should have failed due to one unsupported characters"); + // } + // } - 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(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(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; + // @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 editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // @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"); + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // @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"); + // } + // } - 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(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"); + // } + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // @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 (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } + // @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"); + // } + // } - String sourceAttachmentId = createResponse.get(1); + // @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"); + // } + // } - // 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); + // @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"); + // } + // } - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateNotesBody); + // @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 (!updateNotesResponse.equals("Updated")) { - fail("Could not update attachment notes field"); - } + // @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"); + // } - // 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); - } + // 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"); + // } - // 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); - } + // 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"); + // } - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity. Response: " + saveSourceResponse); - } + // 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"); + // } + // } - // Fetch attachment metadata to get objectId and verify notes and secondary properties - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // @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; + // } - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // 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 sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.size() < 3) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(2, sourceObjectId); - } + // 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; + // } - 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 (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"); + // } + // } - 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); - } + // @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"); + // } - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment. Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } + // 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"); + // } - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit 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"); + // } - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array + // 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"); + // } + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // @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; + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // 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; + // } - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // 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; + // } - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // 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 (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } + // @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"); + // } - // 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); + // 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 (copiedAttachmentMetadata == null) { - fail("Could not find the copied attachment with fil 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", 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"); + // } - // 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); - } + // 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"); + // } - // 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); - } + // 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"); + // } + // } - // Verify customProperty2 - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied. Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // @Test + // @Order(32) + // void testUploadNAttachments() throws IOException { + // System.out.println("Test (32): Upload maximum 4 attachments in an exsisting 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 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"); + // } + // } + // } - 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(33) + // void testDiscardDraftWithoutAttachments() { + // System.out.println("Test (33) : Discard draft without adding attachments"); - @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()); - } - } + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - sourceObjectIds.clear(); - for (Map metadata : attachmentsMetadata) { - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } + // if (response.equals("Could not create entity")) { + // 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"); - } - } + // response = api.deleteEntityDraft(appUrl, entityName, response); + // if (!response.equals("Entity Draft Deleted")) { + // fail("Draft was not discarded properly"); + // } + // } - @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"); - } - } + // @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(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(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(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.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(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(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(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"); - } - } + // @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(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"); - } - } + // // 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(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (47): Rename link in entity"); - List attachments = new ArrayList<>(); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } - 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 sourceAttachmentId = createResponse.get(1); - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save 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); - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String updateResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, + // updateBody); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // if (!updateResponse.equals("Updated")) { + // fail("Could not update attachment notes field"); + // } - 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"); + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - } + // // Fetch attachment metadata to get objectId + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - @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"); - } + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - 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"); - } + // // Store objectId in array + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.isEmpty()) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(0, sourceObjectId); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save 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); + // } - editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // // Create target entity + // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomTargetEntity.equals("Could not create entity")) { + // fail("Could not create target 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"); - } - } + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array - @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<>(); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - String 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); + // } - 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"); - } + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - 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"); - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target 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"); - } - } + // // 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(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 notes field was 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(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"); + // } - 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("sample1.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); - testStatus = true; - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!testStatus) { - fail("Could not edit link with an invalid URL"); - } - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create 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 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); + // } - 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"); - } - } + // // 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(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity. Response: " + saveSourceResponse); + // } - Boolean testStatus = false; - List attachments = new ArrayList<>(); + // // Fetch attachment metadata to get objectId and verify secondary properties + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - 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); - } + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - @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<>(); + // // Store objectId in array for reuse + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.size() < 2) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(1, sourceObjectId); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // // 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 (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(1)); // 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"); + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - 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"); - } + // // 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 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"); - } - } + // if (copiedAttachmentMetadata == null) { + // fail("Could not find the copied attachment with file in target 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"); + // 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); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // // Verify customProperty2 + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied. Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or 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); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - List invalidObjectIds = Collections.singletonList("incorrectObjectId"); + // if (readResponse.equals("OK")) { + // testStatus = true; + // } + // if (!testStatus) { + // fail( + // "Could not verify that all secondary properties were copied from source to target + // attachment"); + // } + // } - 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()); - } + // @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 saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveResponse.equals("Saved")) { - fail("Could not save target entity after unsuccessful copy"); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - if (!deleteSourceResponse.equals("Entity Deleted")) { - fail("Could not delete source 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(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"); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - 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 (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save new source entity"); - } + // 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"); + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } + // // 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 sourceObjectIds = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // // 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 (sourceObjectIds.isEmpty()) { - fail("Could not fetch objectId from new source entity"); - } + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity. Response: " + saveSourceResponse); + // } - 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); - } + // // Fetch attachment metadata to get objectId and verify notes and secondary properties + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity after copying link"); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.size() < 3) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(2, sourceObjectId); + // } - 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 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); + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); + // 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); + // } - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment. Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } - System.out.println("Attachment type and URL validated successfully."); + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity"); + // } - String attachmentId = (String) copiedAttachment.get("ID"); - assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open the attachment"); - } + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - if (!deleteResponse.equals("Entity Deleted")) { - fail("Could not delete new source entity"); - } - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to 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"); + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create new source entity"); - } + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - 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 (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save new source 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); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } + // if (copiedAttachmentMetadata == null) { + // fail("Could not find the copied attachment with fil in target entity"); + // } - List invalidObjectIds = Collections.singletonList("invalidObjectId123"); + // // 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); + // } - 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 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); + // } - // 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"); - } + // // Verify customProperty2 + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied. Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - 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"); - } - } + // // 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(58) - void testCopyLinkSuccessNewEntityDraft() throws IOException { - System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); + // 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); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // @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 (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or 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"); + // } + // } - 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 (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"); + // } + // } - List sourceObjectIds = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // @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 (sourceObjectIds.isEmpty()) { - fail("Could not fetch object Id for link"); - } + // @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 copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link: " + copyResponse); - } + // @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"); + // } + // } - 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"); + // @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 expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); + // @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"); + // } + // } - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // @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"); + // } + // } - System.out.println("Attachment type and URL validated successfully."); + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (47): Rename link in entity"); + // List attachments = new ArrayList<>(); - String attachmentId = (String) copiedAttachment.get("ID"); - assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - 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, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save 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"); + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - sourceObjectIds.clear(); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - 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"); - } - } + // 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"); - 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"); - } - } + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save 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; - 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); - } + // @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"); + // } - @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, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - // 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); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - // 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"); + // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - // 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"); + // 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 createResponse = - api.createAttachment( - appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); + // @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<>(); - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - changelogAttachmentID = createResponse.get(1); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - 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"); + // 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"); + // } - // Fetch changelog for the newly created attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // 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"); + // } - // 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"); - } + // 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(61) - void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - System.out.println( - "Test (61): Modify note field and custom property, then verify changelog shows created + 3 updated entries"); + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (50): Edit existing link in 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); + // 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 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"); + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // 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"); + // } + // } + // } - // 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"); + // @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"); + // } + // } - // Fetch changelog after modifications - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // @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"); + // } + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); - // 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)"); + // Boolean testStatus = false; + // List attachments = new ArrayList<>(); - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); + // 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); + // } - // 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"); - } + // @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<>(); - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println( - "Test (62): Rename attachment and verify changelog increases with rename entry"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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"); + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - // 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 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"); + // } - // Save entity after rename - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // 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"); + // List sourceObjectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // Fetch changelog after rename - Map changelogAfterRename = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Id for link"); + // } - assertNotNull(changelogAfterRename, "Changelog response should not be null after rename"); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link: " + copyResponse); + // } - // 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"); + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } - @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); - } + // 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(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 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"); + // } + // } - // 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(55) + // void testCopyLinkUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (55): Copy invalid type of link from one entity to another new 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"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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 (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // List invalidObjectIds = Collections.singletonList("incorrectObjectId"); - 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"); + // 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, newEntityID); - 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 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 deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // if (!deleteSourceResponse.equals("Entity Deleted")) { + // fail("Could not delete source entity"); + // } + // } - // Fetch changelog after initial save - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // @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 has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + - // customProperty2) - assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); + // 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(3, changeLogs.size(), "Should have exactly 3 changelog entries"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } - // Save entity again without any modifications - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // List sourceObjectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // 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"); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch objectId from new source entity"); + // } - // Fetch changelog after second save - Map changelogAfterSecondSave = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // 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); + // } - assertNotNull( - changelogAfterSecondSave, "Changelog response should not be null after second save"); + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // 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"); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } - @SuppressWarnings("unchecked") - List> changeLogsAfterSecondSave = - (List>) changelogAfterSecondSave.get("changeLogs"); - assertEquals( - 3, - changeLogsAfterSecondSave.size(), - "Should still have exactly 3 changelog entries after second save"); + // attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // Clean up the entity - api.deleteEntity(appUrl, entityName, newEntityID); - } + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch. Expected '" + // + expectedType + // + "' but got '" + // + receivedType + // + "'."); - @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"); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - // 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); + // System.out.println("Attachment type and URL validated successfully."); - // 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"); + // String attachmentId = (String) copiedAttachment.get("ID"); + // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - // 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"); + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open the attachment"); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // if (!deleteResponse.equals("Entity Deleted")) { + // fail("Could not delete new source entity"); + // } + // } - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - String newAttachmentID = createResponse.get(1); + // @Test + // @Order(57) + // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println( + // "Test (57): Copy invalid type of link from new entity to existing target 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"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create new source entity"); + // } - // Save the entity immediately without any modifications - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // 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"); + // } - // 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"); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save new source entity"); + // } - // Save entity again without modifying the attachment - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } - // Edit entity to fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // List invalidObjectIds = Collections.singletonList("invalidObjectId123"); - // Fetch changelog for the attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); + // 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()); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // // 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"); + // } - // 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); - } + // 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(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println( - "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // @Test + // @Order(58) + // void testCopyLinkSuccessNewEntityDraft() throws IOException { + // System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); - // 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"); - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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 (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or 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"); + // 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 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 sourceObjectIds = + // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, + // copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Id for link"); + // } - // 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 copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link: " + copyResponse); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // 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); + // } - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetBeforeMoveResponse); - } + // @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"); + // } + // } - // 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"); - } - } + // 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"); + // } + // } - // 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 (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); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println("Test (60): View changelog for newly created attachment"); - @Test - @Order(66) - public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - System.out.println( - "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // // 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 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"); - } + // // 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 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 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"); - 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"); + // 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"); + // } - // 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"); + + // // 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"); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // @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); + // } - // 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(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"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from 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); - // 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 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"); - 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 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"); - File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - List targetCreateResponse = - api.createAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - srvpath, - targetPostData, - duplicateFile); + // 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); + // } - if (!targetCreateResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment on target entity"); - } + // @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"); - // 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"); + // // 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"); - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // 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"); - @Test - @Order(67) - public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - System.out.println( - "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + // 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 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(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println( + // "Test (65): Move attachments from Source Entity to Target Entity with 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"); - // 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); - } - } + // // 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); - } + // // 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()); + // } + // } - // 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); - } - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - 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"); + // } - // 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: " + saveTargetBeforeMoveResponse); + // } - // 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); - } + // // 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"); + // } + // } - // 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"); - } + // // 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 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); - } + // // 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(66) + // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + // System.out.println( + // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // 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"); + // // 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(68) - public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - System.out.println( - "Test (68): Move valid attachments from Source Entity to Target Entity 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())); + // // 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 - 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 - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // 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"); + // } - // 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()); - } - } + // 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"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target 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); + // } - // 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 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); + // } - // 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"); - } + // @Test + // @Order(67) + // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + // System.out.println( + // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); - // 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"); - } - } + // // 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: 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"); + // // 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 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); - } + // 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(69) - public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - System.out.println( - "Test (69): Move attachments into existing Target Entity when duplicate exists without sourceFacet"); + // // 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); + // } + // } - // 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"); - } + // // 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); + // } + // } - // 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 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 - 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 - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // 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(); - } - } else { - fail("Attachment metadata does not contain objectId"); - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // // 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); + // } - 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"); + // } - // 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"); - } + // // 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); + // } - // 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"); + // // 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); + // } + // } - 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"); - } + // // 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 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(); + // @Test + // @Order(68) + // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (68): Move valid attachments from Source Entity to Target Entity without + // sourceFacet"); - // 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); + // // 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())); - int nonDuplicateCount = moveObjectIds.size() - 1; - int expectedTargetCount = initialTargetCount + nonDuplicateCount; + // 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( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have initial attachments plus non-duplicate moved attachments"); + // // 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 at least one non-duplicate attachment was moved - assertTrue( - targetMetadataAfterMove.size() > initialTargetCount, - "Target should have more attachments after move (non-duplicates added)"); + // // 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 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); - } + // // 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()); + // } + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - @Test - @Order(70) - public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - throws Exception { - System.out.println( - "Test (70): Move attachments with notes and secondary properties without 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())); + // // 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"); + // } - 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 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"); + // } + // } - // 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"); - } - } + // // 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); + // } - // 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); - } - } + // // 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(69) + // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (69): Move attachments into existing Target Entity when duplicate exists without + // 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"); - // Get source attachment count before move - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create 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 before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // // 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 target attachment count before move - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // 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"); - } + // // 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"); + // } - // 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); - } + // // 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"); - // 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); - } - } + // 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"); + // } - // 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); - } + // // 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(71) - public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - System.out.println( - "Test (71): Move attachments with invalid or undefined secondary properties"); + // // 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())); - files.add(new File(classLoader.getResource("WDIRSCodeList.csv").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"); + // } - // 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 + // 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"); - } + // // 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); + // } + // } - // Get source attachment count before move - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create 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 before move - String saveTargetBeforeMoveResponse68 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse68.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); - } + // // 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); + // } + // } - // 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"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - // Verify attachments moved to target - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // // Get source attachment count before move + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - 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"); - } - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // 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"); + // // 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); - } + // // 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"); + // } - @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"); + // // 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 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"); - } + // // 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); + // } + // } - // 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 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); + // } - 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(71) + // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + // System.out.println( + // "Test (71): Move attachments with invalid or undefined secondary properties"); - // 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"); + // } - // 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"); + // // 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())); - 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()); - // 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()); - } - } + // // 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"); + // } + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // 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); + // } - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // // 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); + // } - 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"); - } + // // 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 saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // // Get source attachment count before move + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // 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"); - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // 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"); + // // 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 all expected attachments are in target - Set targetFileNames = - targetMetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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"); + // } - for (File file : files) { - assertTrue( - targetFileNames.contains(file.getName()), - "Target should contain attachment: " + file.getName()); - } + // // 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"); + // } + // } - // 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 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); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - sourceCountBeforeMove, - sourceMetadataAfterMove.size(), - "Source entity in draft mode retains attachments after move (copy behavior)"); + // @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"); + // } - Set sourceFileNamesAfterMove = - sourceMetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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())); - for (File file : files) { - assertTrue( - sourceFileNamesAfterMove.contains(file.getName()), - "Source (draft) should still 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"); - // 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(73) - public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { - System.out.println( - "Test (73): Edit attachment file name in Source Entity and move it to Target 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); + // } - // 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"); - } + // // 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()); + // } + // } - // Add attachment with original name (sample.txt) - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = 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", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - 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"); - } + // 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"); + // } - String attachmentId = createResponse.get(1); - assertNotNull(attachmentId, "Attachment 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"); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Save target before move + // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - // 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"); - } + // // 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"); + // } - // 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"); + // // 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()); + // } - // 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); - } + // // 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 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> 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 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(73) + // public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { + // System.out.println( + // "Test (73): Edit attachment file name in Source Entity and move it to Target 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); + // // 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 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"); + // // Add attachment with original name (sample.txt) + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = 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", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - @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"); + // 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"); + // } - // 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 attachmentId = createResponse.get(1); + // assertNotNull(attachmentId, "Attachment 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())); + // // 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"); + // // 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"); + // } - // 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"); - } - } + // // 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); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // 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"); + // } - // 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()); - } - } + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // 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"); + // } - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // // 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 Target Entity 1 - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity 1"); - } + // @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"); - // 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 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 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"); - } + // // 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 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"); + // 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 all expected files are in Target Entity 1 - Set target1FileNames = - target1MetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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"); + // } + // } - for (File file : files) { - assertTrue( - target1FileNames.contains(file.getName()), - "Target 1 should 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); + // } - // 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"); - } + // // Get count of attachments in source + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have 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"); - } + // // 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()); + // } + // } - // 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); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - 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(moveSourceFolderId, "Source folder ID should not be null"); - assertNotNull(target1FolderId, "Target 1 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"); + // } - // 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"); - } + // // 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"); + // } - // 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"); + // // 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 all expected files are in Target Entity 2 - Set target2FileNames = - target2MetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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()); + // } - for (File file : files) { - assertTrue( - target2FileNames.contains(file.getName()), - "Target 2 should contain attachment: " + file.getName()); - } + // // 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 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); - } + // // 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"); + // } - @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"); - } + // // 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); + // } - // 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())); + // 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()); + // } + // } - 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"); + // 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"); + // } - // 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"); - } - } + // // 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()); + // } - // 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); - } + // // 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); + // } - // 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()); - } - } + // @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"); + // } - 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())); - assertNotNull(moveSourceFolderId, "Source folder ID 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"); - // 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"); - } + // // 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"); + // } + // } - // 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(); - } + // // 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); + // } - // 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); + // // Get count of attachments in source + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - // 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"); + // // 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 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"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Clean up - delete both entities using SDM role - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - @Test - @Order(76) - void testRenameAttachmentWithExtensionChange() throws IOException { - System.out.println( - "Test (76) : Rename attachment changing extension from .pdf to .txt - should return extension change warning"); + // // 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 1: Create a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (newEntityID.equals("Could not create entity")) { - fail("Could not create entity"); - } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!saveResponse.equals("Saved")) { - fail("Could not save new entity: " + saveResponse); - } + // // 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 2: Upload a PDF attachment - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // // 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); + // } - 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"); + // @Test + // @Order(76) + // void testRenameAttachmentWithExtensionChange() throws IOException { + // System.out.println( + // "Test (76) : Rename attachment changing extension from .pdf to .txt - should return + // extension change warning"); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (editResponse != "Entity in draft mode") { - fail("Could not put entity in draft mode for PDF upload"); - } + // // Step 1: Create a new entity + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (newEntityID.equals("Could not create entity")) { + // fail("Could not create entity"); + // } + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save new entity: " + saveResponse); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - String check = createResponse.get(0); - if (!check.equals("Attachment created")) { - fail("Could not upload sample.pdf: " + check); - } - String newAttachmentID = createResponse.get(1); + // // Step 2: Upload a PDF attachment + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Step 3: Save the entity - String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!savedAfterUpload.equals("Saved")) { - fail("Could not save entity after PDF upload: " + savedAfterUpload); - } + // 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"); - // Step 4: Edit the entity - String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (editDraftResponse != "Entity in draft mode") { - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not put entity in draft mode for rename"); - } + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (editResponse != "Entity in draft mode") { + // fail("Could not put entity in draft mode for PDF upload"); + // } - // Step 5: Rename the attachment changing the extension from .pdf to .txt - String renameResponse = - api.renameAttachment( - appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); - if (!renameResponse.equals("Renamed")) { - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not rename attachment: " + renameResponse); - } + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); + // String check = createResponse.get(0); + // if (!check.equals("Attachment created")) { + // fail("Could not upload sample.pdf: " + check); + // } + // String newAttachmentID = createResponse.get(1); - // Step 6: Save and validate the extension change error message - String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertNotNull(saveWithWarningResponse, "Response should not be null"); + // // Step 3: Save the entity + // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!savedAfterUpload.equals("Saved")) { + // fail("Could not save entity after PDF upload: " + savedAfterUpload); + // } - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + // // Step 4: Edit the entity + // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (editDraftResponse != "Entity in draft mode") { + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not put entity in draft mode for rename"); + // } - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); + // // Step 5: Rename the attachment changing the extension from .pdf to .txt + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); + // if (!renameResponse.equals("Renamed")) { + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not rename attachment: " + renameResponse); + // } - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match expected value"); - break; - } - } - } + // // Step 6: Save and validate the extension change error message + // String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // newEntityID); + // assertNotNull(saveWithWarningResponse, "Response should not be null"); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match expected value"); + // break; + // } + // } + // } - assertTrue( - foundExtensionError, - "Expected extension change warning not found in response. Full response: " - + saveWithWarningResponse); + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found in response. Full response: " + // + saveWithWarningResponse); - // Clean up - api.deleteEntity(appUrl, entityName, newEntityID); - } + // // Clean up + // api.deleteEntity(appUrl, entityName, newEntityID); + // } @Test @Order(77) From a126821f3f04b3c84d532494051859f1c0cf28de Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 12:07:04 +0530 Subject: [PATCH 05/13] Test fix for multitenant --- .../java/integration/com/sap/cds/sdm/Api.java | 53 +++++++ .../com/sap/cds/sdm/ApiInterface.java | 4 + .../integration/com/sap/cds/sdm/ApiMT.java | 49 +++++++ .../cds/sdm/IntegrationTest_SingleFacet.java | 131 ++---------------- 4 files changed, 118 insertions(+), 119 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 a16d520de..176f20264 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,59 @@ public List> fetchEntityMetadataDraft( } } + @Override + public String downloadSelectedAttachmentsDraft( + String appUrl, String entityName, String facetName, String entityID, List ids) + throws IOException { + String url = + "https://" + + appUrl + + "/odata/v4/" + + serviceName + + "/" + + entityName + + "(ID=" + + entityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + entityID + + ",ID=" + + ids.get(0) + + ",IsActiveEntity=false)" + + "/" + + 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); + } + } + @Override public String downloadSelectedAttachments( String appUrl, String entityName, String facetName, String entityID, List ids) 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 bc05d3715..034841d83 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 @@ -125,4 +125,8 @@ public Map fetchChangelog( public String downloadSelectedAttachments( String appUrl, String entityName, String facetName, String entityID, List ids) throws IOException; + + public String downloadSelectedAttachmentsDraft( + 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 99e0fa3b8..8a88e2dff 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,55 @@ public List> fetchEntityMetadataDraft( } } + @Override + public String downloadSelectedAttachmentsDraft( + String appUrl, String entityName, String facetName, String entityID, List ids) + throws IOException { + String url = + "https://" + + appUrl + + "/api/admin/" + + entityName + + "(ID=" + + entityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + entityID + + ",ID=" + + ids.get(0) + + ",IsActiveEntity=false)" + + "/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); + } + } + @Override public String downloadSelectedAttachments( String appUrl, String entityName, String facetName, String entityID, List ids) 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 b07527109..3604ce602 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 @@ -6990,67 +6990,10 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { } 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)); + api.downloadSelectedAttachmentsDraft( + appUrl, entityName, facetName, draftEntityID, List.of(draftAttachmentID1)); JSONArray singleResultArray = new JSONArray(singleDownloadResult); assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); JSONObject singleResult = singleResultArray.getJSONObject(0); @@ -7063,8 +7006,11 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { // Step 4: Select all 3 and download while entity is still in draft state String multiDownloadResult = - callDraftDownload.apply( - draftAttachmentID1, + api.downloadSelectedAttachmentsDraft( + appUrl, + entityName, + facetName, + draftEntityID, List.of(draftAttachmentID1, draftAttachmentID2, draftAttachmentID3)); JSONArray multiResultArray = new JSONArray(multiDownloadResult); assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); @@ -7167,64 +7113,10 @@ void testDownloadButtonWithPdfAndLinkInDraftState() throws IOException { 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)); + String pdfOnlyResult = + api.downloadSelectedAttachmentsDraft( + 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( @@ -7235,7 +7127,8 @@ void testDownloadButtonWithPdfAndLinkInDraftState() throws IOException { // 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)); + api.downloadSelectedAttachmentsDraft( + 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"); From 83c0479c758ae1f0247097cbd66dfc0dd4808748 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 12:10:15 +0530 Subject: [PATCH 06/13] Spotless --- .../com/sap/cds/sdm/IntegrationTest_SingleFacet.java | 6 +++++- 1 file changed, 5 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 3604ce602..482340853 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 @@ -7128,7 +7128,11 @@ void testDownloadButtonWithPdfAndLinkInDraftState() throws IOException { // (link attachment returns error status, disabling the download) String mixedResult = api.downloadSelectedAttachmentsDraft( - appUrl, entityName, facetName, testEntityID, List.of(pdfAttachmentID, linkAttachmentID)); + 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"); From e827a284a043848f86f0b3a72bf978b95a755242 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 13:06:37 +0530 Subject: [PATCH 07/13] Fix for multifacet & chapters --- ...ntegrationTest_Chapters_MultipleFacet.java | 263 ++++-------------- .../sdm/IntegrationTest_MultipleFacet.java | 129 +-------- 2 files changed, 62 insertions(+), 330 deletions(-) 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 f0554780d..b7b6c8441 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 @@ -6765,112 +6765,35 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { 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); + + String singleResult = + api.downloadSelectedAttachmentsDraft( + appUrl, chapterEntityName, facetName, draftChapterID, 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 in draft state for facet " + facetName); + assertTrue( + singleArray.getJSONObject(0).has("content"), + "Attachment should have content field for facet " + facetName); + + String multiResult = + api.downloadSelectedAttachmentsDraft( + appUrl, chapterEntityName, facetName, draftChapterID, 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", - singleArray.getJSONObject(0).getString("status"), - "Download button should be enabled in draft state for facet " + facetName); + multiArray.getJSONObject(j).getString("status"), + "Attachment " + (j + 1) + " should download successfully 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); - } + multiArray.getJSONObject(j).has("content"), + "Attachment " + (j + 1) + " should have content field for facet " + facetName); } } @@ -6964,119 +6887,43 @@ void testDownloadButtonDisabledWithLinkInDraftState() throws IOException { 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; - } + String pdfResult = + api.downloadSelectedAttachmentsDraft( + appUrl, chapterEntityName, facetName, testChapterID, List.of(pdfId)); + JSONArray pdfArray = new JSONArray(pdfResult); + 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 mixedResult = + api.downloadSelectedAttachmentsDraft( + 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); } + 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); 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 2a23e4f2b..0b98fc280 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 @@ -7520,69 +7520,11 @@ void testDownloadAttachmentsAcrossMultipleFacetsInDraftState() throws IOExceptio 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])); + String downloadResult = + api.downloadSelectedAttachmentsDraft( + appUrl, entityName, facet[i], draftEntityID, List.of(draftFacetAttachmentIDs[i])); JSONArray resultArray = new JSONArray(downloadResult); assertEquals( 1, resultArray.length(), "Expected 1 result from facet in draft state: " + facet[i]); @@ -7679,68 +7621,10 @@ void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOExcepti 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)); + api.downloadSelectedAttachmentsDraft( + appUrl, entityName, facet[0], testEntityID, List.of(pdfAttachmentID)); JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); assertEquals( 1, @@ -7753,7 +7637,8 @@ void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOExcepti // 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)); + api.downloadSelectedAttachmentsDraft( + appUrl, entityName, facet[1], testEntityID, List.of(linkAttachmentID)); JSONArray linkOnlyArray = new JSONArray(linkOnlyResult); assertEquals( 1, From d680ae86bd3cee56d1e09bb592217f12f831c301 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 14:04:38 +0530 Subject: [PATCH 08/13] Uncommenting tests --- ...ntegrationTest_Chapters_MultipleFacet.java | 12332 ++++++++------- .../sdm/IntegrationTest_MultipleFacet.java | 12421 ++++++++-------- .../cds/sdm/IntegrationTest_SingleFacet.java | 11438 +++++++------- 3 files changed, 17846 insertions(+), 18345 deletions(-) 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 b7b6c8441..8efd27964 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 @@ -467,6235 +467,6109 @@ void testCreateNewBookWithChapterAndAttachments() throws IOException { } } - // @Test - // @Order(7) - // void testRenameChapterAttachments() { - // System.out.println("Test (7) : Rename single attachment, reference, and footnote in - // chapter"); - // Boolean testStatus = true; - - // try { - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - // if ("Entity in draft mode".equals(response)) { - // String[] name = {"sample123", "reference123", "footnote123"}; - // for (int i = 0; i < facet.length; i++) { - // // Read the facet to ensure it exists - // response = - // api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], - // name[i]); - // if (!"Renamed".equals(response)) { - // testStatus = false; - // System.out.println(facet[i] + " was not renamed: " + response); - // } - // } - // // Save book draft if everything is renamed - // if (testStatus) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if (!"Saved".equals(response)) { - // testStatus = false; - // System.out.println("Book draft was not saved: " + response); - // } - // } else { - // // Attempt save despite potential rename failures - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // } - // } else { - // testStatus = false; - // System.out.println("Book was not put into draft mode: " + response); - // } - // } catch (Exception e) { - // testStatus = false; - // System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); - // } - - // if (!testStatus) { - // fail("There was an error during the rename test process for chapter."); - // } - // } - - // @Test - // @Order(8) - // void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { - // System.out.println("Test (8): Create chapter attachments with unsupported characters"); - // 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("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, bookEntityName, srvpath, bookID); - // if (!"Entity in draft mode".equals(response)) { - // fail("Book not in draft mode: " + response); - // return; - // } - - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", chapterID); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); - - // if (!"Attachment created".equals(createResponse.get(0))) { - // fail("Could not create attachment in chapter facet: " + facet[i]); - // return; - // } - - // String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE - // response = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); - - // System.out.println("Rename response for chapter " + facet[i] + ": " + response); - // } - - // // Save should fail - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - // // ---------------- PARSE JSON ---------------- - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // String message = root.path("error").path("message").asText(); - - // // ---------------- NORMALIZE MESSAGE ---------------- - // // 1. Normalize smart quotes - // // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared - // message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); - - // // ---------------- EXPECTED MESSAGE (EXACT) ---------------- - // String expectedMessage = - // "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" - // + "Table: attachments\n" - // + "Page: IntegrationTestEntity"; - - // if (message.equals(expectedMessage)) { - - // for (int i = 0; i < facet.length; i++) { - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - - // if (!testStatus) { - // fail("Test for unsupported characters in chapter attachments failed"); - // } - // } - - // @Test - // @Order(9) - // void testRenameSingleDuplicateInChapter() throws IOException { - // System.out.println( - // "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); - // Boolean testStatus = false; - // int counter = 0; - - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Edit entity response: " + response); - - // if ("Entity in draft mode".equals(response)) { - // // To create a duplicate within the same facet, we need to rename ID2[i] to - // // the same name as an existing file in that facet. After test 7, the existing files are: - // // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) - // // We rename ID2[i] (sample123.txt from test 8) to these names which already exist - // String[] duplicateNames = {"sample123", "reference123", "footnote123"}; - // String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; - - // // Try to rename to duplicate file names (names that already exist in each facet) - // for (int i = 0; i < facet.length; i++) { - // response = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); - // System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); - // if ("Renamed".equals(response)) { - // counter++; - // } - // } - // System.out.println("Renamed count: " + counter); - - // if (counter == facet.length) { - // // Try to save - should fail with duplicate error - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Save response (expecting error): " + response); - - // // Parse JSON response to check for duplicate error - // ObjectMapper mapper = new ObjectMapper(); - // try { - // JsonNode root = mapper.readTree(response); - // String message = root.path("error").path("message").asText(); - - // if (message.contains("already exists")) { - // System.out.println("Duplicate error detected as expected: " + message); - // counter = 0; - // // Rename with valid different names - // for (int i = 0; i < facet.length; i++) { - // response = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); - // System.out.println("Rename " + facet[i] + " to valid name: " + response); - // if ("Renamed".equals(response)) { - // counter++; - // } - // } - - // if (counter == facet.length) { - // // Save should now succeed - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Final save response: " + response); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } else { - // System.out.println("Unexpected error message: " + message); - // } - // } catch (Exception e) { - // // Response might not be JSON if save succeeded (shouldn't happen with duplicates) - // System.out.println("Response was not JSON error: " + response); - // // If save succeeded unexpectedly, we still need to ensure book is saved - // if ("Saved".equals(response)) { - // System.out.println( - // "Save succeeded unexpectedly - duplicates might be in different facets"); - // } - // } - // } - // } else { - // System.out.println("Book was not put into draft mode: " + response); - // } - - // if (!testStatus) { - // fail("Duplicate rename test failed for chapter"); - // } - // } - - // @Test - // @Order(10) - // void testRenameToValidateNamesInChapter() throws IOException { - // System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); - // Boolean testStatus = false; - - // // Create a new book and chapter for this test - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // bookID3 = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); - // if (!"Could not create entity".equals(chapterResponse)) { - // chapterID3 = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID3); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); - // } - - // String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; - - // boolean allRenamed = true; - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); - // if (!"Renamed".equals(response1)) { - // allRenamed = false; - // System.out.println( - // "Failed to rename " - // + facet[i] - // + " to valid name " - // + validNames[i] - // + ": " - // + response1); - // } - // } - - // if (allRenamed) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } - // } - - // if (!testStatus) { - // fail("Could not rename chapter attachments to valid names"); - // } - // } - - // @Test - // @Order(11) - // void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { - // System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); - // boolean testStatus = true; - - // try { - // String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Edit entity response: " + response); - - // if (response.equals("Entity in draft mode")) { - // String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; - // for (int i = 0; i < facet.length; i++) { - // response = - // apiNoRoles.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); - // System.out.println("Rename response for " + facet[i] + ": " + response); - // if (!"Renamed".equals(response)) { - // testStatus = false; - // } - // } - - // if (testStatus) { - // // Save should fail with permission error - // response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Save response (expecting permission error): " + response); - - // // The expected error should indicate no permissions to update - // String expected = - // "[{\"code\":\"\",\"message\":\"Could not update the following - // files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update - // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3}]"; - - // // Check if response contains permission error - // if (!response.equals(expected) - // && !response.contains("do not have the required permissions")) { - // System.out.println("Expected permission error but got: " + response); - // testStatus = false; - // } else { - // System.out.println("Got expected permission error"); - // } - // } else { - // // Some renames failed - save to release draft - // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // } - // } else { - // System.out.println("Could not edit entity: " + response); - // testStatus = false; - // } - // } catch (Exception e) { - // System.out.println("Exception: " + e.getMessage()); - // testStatus = false; - // } - - // if (!testStatus) { - // fail("Chapter attachment got renamed without SDM roles."); - // } - // } - - // @Test - // @Order(12) - // void testDeleteSingleChapterAttachment() throws IOException { - // System.out.println( - // "Test (12) : Delete single attachment, reference, and footnote from chapter"); - // Boolean testStatus = false; - // int deleteCounter = 0; - - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - // if (response.equals("Deleted")) deleteCounter++; - // } - // if (deleteCounter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if (response.equals("Saved")) { - // int verifyCounter = 0; - // for (int i = 0; i < facet.length; i++) { - // response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - // if (response.equals("Could not read Attachment")) verifyCounter++; - // } - // if (verifyCounter == facet.length) { - // testStatus = true; - // } else { - // fail( - // "Could not verify all deleted chapter facets. Verified: " - // + verifyCounter - // + "/" - // + facet.length); - // } - // } else { - // fail("Could not save book after deleting chapter attachments"); - // } - // } else { - // fail( - // "Could not delete all chapter attachments. Deleted: " - // + deleteCounter - // + "/" - // + facet.length); - // } - // } else { - // fail("Could not edit book to draft mode"); - // } - - // if (!testStatus) { - // fail("Test failed to delete chapter attachments"); - // } - // } - - // @Test - // @Order(13) - // void testUploadBlockedMimeTypeToChapter() throws IOException { - // System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); - // Boolean testStatus = false; - - // // Create new book and chapter - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // bookID4 = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); - // if (!"Could not create entity".equals(chapterResponse)) { - // chapterID4 = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = - // new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID4); - // postData.put("mimeType", "application/rtf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // boolean allBlocked = true; - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID4, 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)) { - // allBlocked = false; - // System.out.println( - // "Chapter facet " - // + facet[i] - // + " incorrectly accepted blocked mimeType: " - // + actualResponse); - // } - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); - // if ("Saved".equals(response) && allBlocked) { - // testStatus = true; - // } - // } - // } - - // if (!testStatus) { - // fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); - // } - // } - - // @Test - // @Order(14) - // void testDeleteBookAndChapter() { - // System.out.println("Test (14) : Delete book (and its chapters)"); - // Boolean testStatus = false; - // // Delete books (chapters are deleted automatically as they're composition) - // String response = api.deleteEntity(appUrl, bookEntityName, bookID); - // String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); - // String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); - // String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); - // if (response.equals("Entity Deleted") - // && response2.equals("Entity Deleted") - // && response3.equals("Entity Deleted") - // && response4.equals("Entity Deleted")) testStatus = true; - // if (!testStatus) fail("Could not delete books"); - // } - - // @Test - // @Order(15) - // void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { - // System.out.println( - // "Test (15) : Rename & Update secondary property in chapter before book is saved"); - // System.out.println("Creating book and chapter"); - - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (!response.equals("Could not create entity")) { - // bookID5 = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); - // if (!chapterResponse.equals("Could not create entity")) { - // chapterID5 = chapterResponse; - - // System.out.println("Creating attachment, reference, and footnote in chapter"); - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID5); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // boolean allCreated = true; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); - // if (tempID[i] == null || tempID[i].isEmpty()) { - // System.out.println("Failed to create attachment for facet: " + facet[i]); - // allCreated = false; - // } - // } - - // System.out.println("Attachments, References, and Footnotes created in chapter"); - // System.out.println( - // "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + - // tempID[2]); - - // if (!allCreated) { - // fail("Could not create all attachments for test 15"); - // } - - // // Reset counter for this test - // counter = 0; - - // // Use valid dropdown value for customProperty1 - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; - - // for (int i = 0; i < facet.length; i++) { - // System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - // System.out.println("Rename response for " + facet[i] + ": " + response1); - - // // Update customProperty1 (String - dropdown value) - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); - - // // Update customProperty2 (Integer) - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - - // // Update customProperty5 (DateTime) - using customProperty5 like Books test - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - - // // Update customProperty6 (Boolean) - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - // // Check all updates succeeded - // if ("Renamed".equals(response1) - // && "Updated".equals(updateSecondaryPropertyResponse1) - // && "Updated".equals(updateSecondaryPropertyResponse2) - // && "Updated".equals(updateSecondaryPropertyResponse3) - // && "Updated".equals(updateSecondaryPropertyResponse4)) { - // counter++; - // } else { - // System.out.println( - // "Update failed for " - // + facet[i] - // + ": rename=" - // + response1 - // + ", dropdown=" - // + updateSecondaryPropertyResponse1 - // + ", int=" - // + updateSecondaryPropertyResponse2 - // + ", datetime=" - // + updateSecondaryPropertyResponse3 - // + ", bool=" - // + updateSecondaryPropertyResponse4); - // } - // } - - // System.out.println("Counter after all facets: " + counter); - // if (counter == facet.length) { - // // Save the book (not the chapter) - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // System.out.println("Save response: " + response); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } else { - // System.out.println( - // "Counter is less than " + facet.length + ", not saving. Counter: " + counter); - // } - // } - // } - - // if (!testStatus) { - // fail( - // "Could not update secondary properties in chapter before book save. Counter: " + - // counter); - // } - // } - - // @Test - // @Order(16) - // void testUploadNAttachmentsToChapter() throws IOException { - // System.out.println("Test (16) : Upload N attachments to chapter"); - // Boolean testStatus = false; - // counter = 0; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); - - // for (int j = 0; j < 5; j++) { - // // Create temp file with unique name per iteration - // File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); - // Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID5); - // 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, bookEntityName, srvpath, bookID5); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - // String check = facetResponse.get(0); - // if (check.equals("Attachment created")) { - // counter++; - // } else { - // System.out.println( - // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - // } - // } - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if (!response.equals("Saved")) { - // System.out.println( - // "Failed to save book after creating attachments in chapter: " + response); - // } - // } else { - // System.out.println("Could not edit book draft: " + response); - // } - // tempFile.delete(); - // } - - // if (counter == 15) { // 5 iterations * 3 facets - // testStatus = true; - // } - - // if (!testStatus) { - // fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); - // } - // } - - // @Test - // @Order(17) - // void testDiscardDraftWithoutChapterAttachments() { - // System.out.println("Test (17) : Discard book draft without chapter attachments"); - // Boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // // Create chapter but don't add attachments - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft without chapter attachments was not discarded properly"); - // } - // } - - // @Test - // @Order(18) - // void testDiscardDraftWithChapterAttachments() throws IOException { - // System.out.println("Test (18) : Discard book draft with chapter attachments"); - // Boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // // Create chapter - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // 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 chapter - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - // String check = facetResponse.get(0); - // if (!check.equals("Attachment created")) { - // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - // } - // } - - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft with chapter attachments was not discarded properly"); - // } - // } - - // @Test - // @Order(19) - // void testUploadChapterAttachmentWithoutSDMRole() throws IOException { - // System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); - // Boolean testStatus = true; - - // String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, - // tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // try { - // List createResponse = - // apiNoRoles.createAttachment( - // appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); - // String check = createResponse.get(0); - - // if (check.equals("Attachment created")) { - // testStatus = false; - // } - // } catch (Exception e) { - // // Expected to fail - // testStatus = true; - // } - - // apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // } - // } - - // if (!testStatus) { - // fail("Chapter attachment was uploaded without SDM roles"); - // } - // } - - // @Test - // @Order(20) - // void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { - // System.out.println( - // "Test (20): Rename & Update secondary property in chapter after book is saved"); - // Boolean testStatus = false; - // counter = 0; // Reset counter for this test - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // System.out.println("Editing book, response: " + response); - - // if (response.equals("Entity in draft mode")) { - // // Use unique names that won't conflict with existing attachments - // String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; - // Integer secondaryPropertyInt = 42; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // System.out.println("Renaming and updating secondary properties for chapter attachment"); - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // // Get the first attachment ID from the chapter - // try { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); - // if (!metadata.isEmpty()) { - // tempID[i] = (String) metadata.get(0).get("ID"); - // } - // } catch (IOException e) { - // fail("Could not fetch metadata for chapter: " + e.getMessage()); - // } - - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if (response.equals("Saved")) { - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for chapter attachment"); - // } - // } - // } - // if (!testStatus) fail("Could not update secondary properties in chapter after book is - // saved"); - // } - - // @Test - // @Order(21) - // void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException - // { - // System.out.println( - // "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); - // System.out.println("Creating book and chapter"); - // Boolean testStatus = false; - // int localCounter = 0; - // int createCounter = 0; - - // // Create new book and chapter for this test - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // if (tempID[i] != null) { - // createCounter++; - // } - // } - - // // Only proceed if all facets were created successfully - // if (createCounter == facet.length) { - // // Prepare test data - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + - // "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], - // invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // localCounter++; - // } - // } - - // if (localCounter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - // // Fetch metadata and verify values weren't updated due to invalid property - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, - // tempID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - - // // Parse JSON response and check for expected error messages - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved with expected invalid property errors"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for chapter attachment is unsuccessful"); - // } - // } else { - // System.out.println( - // "Not all facets updated successfully. localCounter: " + localCounter); - // } - // } else { - // System.out.println( - // "Not all facets created successfully. createCounter: " + createCounter); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // } - // } - // if (!testStatus) - // fail("Could not update invalid secondary property in chapter before book is saved"); - // } - - // @Test - // @Order(22) - // void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { - // System.out.println( - // "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); - // System.out.println("Creating book and chapter"); - // Boolean testStatus = false; - // int localCounter = 0; - // int createCounter = 0; - - // // Create new book and chapter - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // if (tempID[i] != null) { - // createCounter++; - // } - // } - - // // Only proceed if all facets were created successfully - // if (createCounter != facet.length) { - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // fail("Not all facets created successfully. createCounter: " + createCounter); - // } - - // // Save the book first - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (!response.equals("Saved")) { - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // fail("Could not save book initially"); - // } - - // // Now edit to update with invalid property - // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (response.equals("Entity in draft mode")) { - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt = 12; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testidinvalid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + - // "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], - // invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // localCounter++; - // } - // } - - // if (localCounter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, - // tempID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - - // // Parse JSON response and check for expected error messages - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved with expected invalid property errors"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for chapter attachment is unsuccessful"); - // } - // } else { - // System.out.println( - // "Not all facets updated successfully. localCounter: " + localCounter); - // } - // } - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // } - // } - // if (!testStatus) - // fail("Could not update invalid secondary property in chapter after book is saved"); - // } - - // @Test - // @Order(23) - // void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { - // System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); - - // boolean testStatus = false; - - // // Reuse bookID5 and chapterID5 - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - - // if (response.equals("Entity in draft mode")) { - // ClassLoader classLoader = getClass().getClassLoader(); - - // // Use temp files with unique names to avoid duplicate name errors - // File originalPdf = - // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // File originalTxt = - // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); - - // File file1 = File.createTempFile("test23_pdf_", ".pdf"); - // File file2 = File.createTempFile("test23_txt_", ".txt"); - // Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); - // Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); - - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", chapterID5); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // Map postData2 = new HashMap<>(postData1); - // postData2.put("up__ID", chapterID5); - // postData2.put("mimeType", "text/plain"); - - // boolean allCreated = true; - // String[] tempID1 = new String[facet.length]; - // String[] tempID2 = new String[facet.length]; - - // for (int i = 0; i < facet.length; i++) { - // List response1 = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); - // List response2 = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); - - // if (response1.get(0).equals("Attachment created") - // && response2.get(0).equals("Attachment created")) { - // tempID1[i] = response1.get(1); // to keep one - // tempID2[i] = response2.get(1); // will delete this one - // } else { - // System.out.println("Failed to create attachments for facet " + facet[i]); - // System.out.println("Response 1: " + response1.get(0)); - // System.out.println("Response 2: " + response2.get(0)); - // allCreated = false; - // break; - // } - - // String deleteResponse = - // api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); - // if (!"Deleted".equals(deleteResponse)) { - // allCreated = false; - // break; - // } - // } - - // file1.delete(); - // file2.delete(); - - // if (allCreated) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } else { - // System.out.println("Could not edit book: " + response); - // } - - // if (!testStatus) { - // fail("Failed to upload multiple chapter facet entries, delete one per facet and save - // book"); - // } - // } - - // @Test - // @Order(24) - // void testUpdateChapterEntityDraft() throws IOException { - // System.out.println("Test (24): Update chapter in book draft with new facet content"); - // boolean testStatus = false; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - // // Use unique temp file name to avoid duplicates - // File tempFile = File.createTempFile("test24_sample_", ".pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID5); - // 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, bookEntityName, srvpath, bookID5); - // if (response.equals("Entity in draft mode")) { - // boolean allCreated = true; - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - // String check = facetResponse.get(0); - // if (!check.equals("Attachment created")) { - // allCreated = false; - // System.out.println( - // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - // } - // } - - // if (allCreated) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } else { - // System.out.println("Could not edit book: " + response); - // } - - // tempFile.delete(); - - // if (!testStatus) { - // fail("Failed to update chapter entity draft with new attachments"); - // } - // } - - // @Test - // @Order(25) - // void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - // throws IOException { - // System.out.println( - // "Test (25): Rename & Update secondary properties for multiple chapter attachments after - // book is saved"); - // System.out.println("Creating book and chapter with multiple attachments"); - - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!"Could not create entity".equals(chapterResponse)) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create PDF attachments - // postData.put("mimeType", "application/pdf"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // String[] pdfID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // pdfID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create TXT attachments - // postData.put("mimeType", "application/txt"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // String[] txtID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // txtID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create EXE attachments - // postData.put("mimeType", "application/exe"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // String[] exeID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // exeID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Save book first - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (!"Saved".equals(response)) { - // fail("Could not save book initially"); - // } - - // // Edit book to update chapter attachments - // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (response.equals("Entity in draft mode")) { - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // // Update PDF properties - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String renameResp = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - // String upd3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - // String upd4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - - // if ("Renamed".equals(renameResp) - // && "Updated".equals(upd1) - // && "Updated".equals(upd2) - // && "Updated".equals(upd3) - // && "Updated".equals(upd4)) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // PDF"); - // } - // } - - // // Update TXT properties (only boolean) - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - // String upd = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - // if ("Updated".equals(upd)) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // TXT"); - // } - // } - - // // Update EXE properties (dropdown and int) - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValueExe = integrationTestUtils.getDropDownValue(); - // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyDropdownExe = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - // RequestBody bodyIntExe = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], - // bodyDropdownExe); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // EXE"); - // } - // } - - // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (response.equals("Saved")) { - // System.out.println("Book saved"); - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for chapter - // attachments"); - // } - // } - // } - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property in chapter after book is saved"); - // } - // } - - // @Test - // @Order(26) - // void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() - // throws IOException { - // System.out.println( - // "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter - // facets before book is saved"); - // System.out.println("Creating book and chapter"); - - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (!"Could not create entity".equals(response)) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!"Could not create entity".equals(chapterResponse)) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create PDF attachments - // postData.put("mimeType", "application/pdf"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // String[] pdfID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // pdfID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create TXT attachments - // postData.put("mimeType", "application/txt"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // String[] txtID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // txtID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create EXE attachments - // postData.put("mimeType", "application/exe"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // String[] exeID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // exeID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample1234.pdf"; - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - - // // Update PDF properties - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String renameResp = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - // String upd3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - // String upd4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - // String updInvalid = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], - // invalidPropertyPDF); - - // if ("Renamed".equals(renameResp) - // && "Updated".equals(upd1) - // && "Updated".equals(upd2) - // && "Updated".equals(upd3) - // && "Updated".equals(upd4) - // && "Updated".equals(updInvalid)) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // PDF"); - // } - // } - - // // Update TXT properties - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - // String upd = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - // if ("Updated".equals(upd)) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // TXT"); - // } - // } - - // // Update EXE properties - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValueExe = integrationTestUtils.getDropDownValue(); - // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyDropdownExe = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - // RequestBody bodyIntExe = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // EXE"); - // } - // } - - // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - - // // Verify PDF metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); - // assertEquals(expectedNames[0], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertNull(metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } - - // // Verify TXT metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); - // assertEquals(expectedNames[1], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertTrue((Boolean) metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } - - // // Verify EXE metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); - // assertEquals(expectedNames[2], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); - // assertEquals(1234, metadata.get("customProperty2")); - // } - - // // Parse JSON response and check for expected error messages - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved with expected invalid property errors"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessful for invalid properties and successful for valid - // attachments"); - // } - // } - // } - // } - - // if (!testStatus) { - // fail("Could not update secondary property before book is saved"); - // } - // } - - // @Test - // @Order(27) - // void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - // throws IOException { - // System.out.println( - // "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter - // attachments after book is saved"); - - // // Reuse bookID5 and chapterID5 - // System.out.println("Editing book with bookID5: " + bookID5); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // System.out.println("Edit entity response: " + response); - - // if (response.equals("Entity in draft mode")) { - // // Fetch existing attachments from the chapter - // List> attachmentsMeta = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); - // List> referencesMeta = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); - // List> footnotesMeta = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); - - // System.out.println("Attachments count: " + attachmentsMeta.size()); - // System.out.println("References count: " + referencesMeta.size()); - // System.out.println("Footnotes count: " + footnotesMeta.size()); - - // if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) - // { - // String[] pdfID = new String[facet.length]; - // String[] txtID = new String[facet.length]; - // String[] exeID = new String[facet.length]; - - // pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); - // pdfID[1] = (String) referencesMeta.get(0).get("ID"); - // pdfID[2] = (String) footnotesMeta.get(0).get("ID"); - - // txtID[0] = (String) attachmentsMeta.get(1).get("ID"); - // txtID[1] = (String) referencesMeta.get(1).get("ID"); - // txtID[2] = (String) footnotesMeta.get(1).get("ID"); - - // exeID[0] = (String) attachmentsMeta.get(2).get("ID"); - // exeID[1] = (String) referencesMeta.get(2).get("ID"); - // exeID[2] = (String) footnotesMeta.get(2).get("ID"); - - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // // PDF - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); - - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); - - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); - - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); - - // String updateSecondaryPropertyResponse5 = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated") - // && updateSecondaryPropertyResponse5.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // PDF"); - // } - // } - - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); - // String updateSecondaryPropertyResponseTXT1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // TXT"); - // } - // } - - // Integer secondaryPropertyInt3 = 12; - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // for (int i = 0; i < facet.length; i++) { - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); - - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); - - // if (updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // EXE"); - // } - // } - - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // // Note: Don't verify specific filenames since previous tests may have changed them - // System.out.println("Save response: " + response); - - // // Parse JSON response to check for invalid secondary property errors in all three - // tables - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessful for invalid Secondary properties and successful for - // valid property attachments"); - // } else { - // System.out.println("Save response did not match expected: " + response); - // } - // } else { - // System.out.println("Not enough attachments in facets - need at least 3 per facet"); - // } - // } - // } else { - // System.out.println( - // "Could not edit book - it may be stuck in draft mode from a previous test"); - // } - // if (!testStatus) { - // fail("Could not update secondary property before book is saved"); - // } - // } - - // // Tests 28 and 29 removed - chapters have no attachment limit - - // // Tests 28-29 skipped - chapters have no attachment limit - - // @Test - // @Order(30) - // void testDiscardBookDraftWithoutChapterAttachments() { - // System.out.println("Test (30) : Discard book draft without adding chapter attachments"); - // Boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if (response.equals("Entity Draft Deleted")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft with chapter was not discarded properly"); - // } - // } - - // @Test - // @Order(31) - // void testDiscardBookDraftWithChapterAttachments() throws IOException { - // System.out.println("Test (31): Discard book draft with chapter attachments"); - // boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!"Could not create entity".equals(chapterResponse)) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = - // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // 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 < facet.length; i++) { - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - // if ("Attachment created".equals(createResponse.get(0))) { - // System.out.println("Attachment created in chapter facet: " + facet[i]); - // } else { - // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - // } - // } - - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft with chapter attachments was not discarded properly"); - // } - // } - - // // Tests 32-34 covered in tests 19, 23, 24 - // // Tests 37-41 skipped - copy with notes/secondary properties not applicable - - // @Test - // @Order(42) - // void testCreateLinkSuccessInChapter() throws IOException { - // System.out.println("Test (42): Create link in chapter"); - // List attachments = new ArrayList<>(); - - // // Create book and chapter for link testing - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (response.equals("Could not create entity")) { - // fail("Could not create book"); - // } - // String createLinkBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - // if (chapterResponse.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - // String createLinkChapterID = chapterResponse; - - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // for (String facetName : facet) { - // String createLinkResponse1 = - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - // String createLinkResponse2 = - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", - // linkUrl); - // if (!createLinkResponse1.equals("Link created successfully") - // || !createLinkResponse2.equals("Link created successfully")) { - // fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); - // } - // } - - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // for (String facetName : facet) { - // attachments = - // api - // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) - // .stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String openAttachmentResponse; - // for (String attachment : attachments) { - // openAttachmentResponse = - // api.openAttachment( - // appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open created link in chapter facet : " + facetName); - // } - // } - // } - // api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - // } - - // @Test - // @Order(43) - // void testCreateLinkDifferentChapter() throws IOException { - // System.out.println("Test (43): Create link with same name in different chapter"); - - // // Create new book and chapter - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (response.equals("Could not edit entity")) { - // fail("Could not create book"); - // } - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (chapterResponse.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - // String tempChapterID = chapterResponse; - - // String linkName = "sample"; - // String linkUrl = "https://example.com"; - // for (String facetName : facet) { - // String createResponse = - // api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); - // if (!createResponse.equals("Link created successfully")) { - // fail("Could not create link in different chapter with same name"); - // } - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (!response.equals("Saved")) { - // fail("Could not save book"); - // } - - // response = api.deleteEntity(appUrl, bookEntityName, tempBookID); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(44) - // void testCreateLinkFailureInChapter() throws IOException { - // System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if ("Could not create entity".equals(response)) { - // fail("Could not create book"); - // } - // String createLinkBookID = response; - - // response = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - // if ("Could not create entity".equals(response)) { - // fail("Could not create chapter"); - // } - // String createLinkChapterID = response; - - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - - // ObjectMapper mapper = new ObjectMapper(); - - // for (String facetName : facet) { - - // // Create initial link for this facet first (so duplicate test works) - // response = - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - // if (!"Link created successfully".equals(response)) { - // fail("Could not create initial link for facet: " + facetName); - // } - - // /* ---------- INVALID URL ---------- */ - // try { - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); - // fail("Expected invalid URL error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // assertEquals("400018", error.path("code").asText()); - // assertTrue( - // error.path("message").asText().contains("expected pattern"), - // "Unexpected message: " + error.path("message").asText()); - // } - - // /* ---------- INVALID NAME ---------- */ - // try { - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // createLinkChapterID, - // "sample//", - // "https://example.com"); - // fail("Expected invalid name error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); - - // assertEquals("500", error.path("code").asText()); - // assertTrue( - // message.contains("contains unsupported characters") - // && message.contains("Rename and try again"), - // "Unexpected message: " + message); - // } - - // /* ---------- EMPTY NAME & URL ---------- */ - // try { - // api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); - // fail("Expected missing value error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // assertEquals("409008", error.path("code").asText()); - // assertEquals("Provide the missing value.", error.path("message").asText()); - // } - - // /* ---------- DUPLICATE NAME ---------- */ - // try { - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - // fail("Expected duplicate name error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // assertEquals("500", error.path("code").asText()); - // assertEquals( - // "An object named \"sample\" already exists. Rename the object and try again.", - // error.path("message").asText()); - // } - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - // if (!"Saved".equals(response)) { - // fail("Could not save book"); - // } - - // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - // if (!"Entity Deleted".equals(response)) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(45) - // void testCreateLinkNoSDMRolesInChapter() throws IOException { - // System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); - - // String createLinkBookNoRoles = - // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (createLinkBookNoRoles.equals("Could not edit entity")) { - // fail("Could not create book"); - // } - - // String createLinkChapterNoRoles = - // apiNoRoles.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); - // if (createLinkChapterNoRoles.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // for (String facetName : facet) { - // String linkName = "sample27"; - // String linkUrl = "https://example.com"; - // try { - // apiNoRoles.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); - // if (!response.equals("Saved")) { - // fail("Could not save book"); - // } - - // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(46) - // void testDeleteLinkInChapter() throws IOException { - // System.out.println("Test (46): Delete link in chapter"); - // List> attachments = new ArrayList<>(); - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (response.equals("Could not create entity")) { - // fail("Could not create book"); - // } - // String deleteLinkBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); - // if (chapterResponse.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - // String deleteLinkChapterID = chapterResponse; - - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink( - // appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for chapter facet : " + facetName); - // } - // } - - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // for (String facetName : facet) { - // attachments.add( - // api - // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - // .stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // } - - // String editEntityResponse = - // api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // int index = 0; - // for (String facetName : facet) { - // String deleteLinkResponse = - // api.deleteAttachment( - // appUrl, - // chapterEntityName, - // facetName, - // deleteLinkChapterID, - // attachments.get(index).get(0)); - // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - // if (!deleteLinkResponse.equals("Deleted")) { - // fail("Could not delete created link"); - // } - // index += 1; - // } - - // saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // index = 0; - // attachments.clear(); - // for (String facetName : facet) { - // attachments.add( - // api - // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - // .stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // System.out.println( - // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - // if (attachments.get(index).size() != 0) { - // fail("Link wasn't deleted"); - // } - // index += 1; - // } - - // response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(35) - // void testCopyAttachmentsToNewChapterInSameBook() throws IOException { - // System.out.println( - // "Test (35): Copy attachments from one chapter to another new chapter in the same book"); - - // // Create source book and chapter with attachments - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Load original files for copying content - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(new ArrayList<>()); - // } - - // // Create attachments in all facets - each upload needs a unique filename - // int fileCounter = 0; - // for (int i = 0; i < facet.length; i++) { - // boolean useTxt = (i == 1); // Use txt for references facet - // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - // File originalFile = useTxt ? originalTxt : originalPdf; - // String extension = useTxt ? ".txt" : ".pdf"; - - // for (int j = 0; j < 2; j++) { // Create 2 attachments per facet - // // Create unique temp file for EACH upload to avoid duplicate filename errors - // fileCounter++; - // File tempFile = - // File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, - // tempFile); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // System.out.println("Created attachment ID: " + createResponse.get(1)); - // } else { - // System.out.println("Failed to create attachment: " + createResponse.get(0)); - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - // } - - // // Fetch object IDs from source attachments - // List objectIds = new ArrayList<>(); - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - // if (metadata.containsKey("objectId")) { - // objectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } - // } - - // // Save the source book - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Create target chapter in the SAME book - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source book for adding target chapter"); - // } - - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (targetChapterID.equals("Could not create entity")) { - // fail("Could not create target chapter in same book"); - // } - - // // Copy attachments to target chapter - // int objectIdIndex = 0; - // for (String facetName : facet) { - // List facetObjectIds = - // objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // facetObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - // } - - // // Fetch and wait for copied attachments - // List> copiedMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // for (Map meta : copiedMetadata) { - // String copiedId = (String) meta.get("ID"); - // } - // objectIdIndex += 2; - // } - - // // Save the book with new chapter - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book after copying attachments"); - // } - - // // Verify attachments were copied - read them - // for (String facetName : facet) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.size() != 2) { - // fail("Expected 2 attachments in facet " + facetName + ", found " + - // targetMetadata.size()); - // } - // for (Map meta : targetMetadata) { - // String attachmentId = (String) meta.get("ID"); - // String readResponse = - // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // attachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment in facet: " + facetName); - // } - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // } - - // @Test - // @Order(36) - // void testCopyAttachmentsToChapterInDifferentBook() throws IOException { - // System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); - - // // Create Book1 with source chapter and attachments - // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create Book2 with target chapter - // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Load original files for copying content - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(new ArrayList<>()); - // } - - // // Create attachments in all facets of source chapter - each upload needs unique filename - // int fileCounter = 0; - // for (int i = 0; i < facet.length; i++) { - // boolean useTxt = (i == 1); // Use txt for references facet - // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - // File originalFile = useTxt ? originalTxt : originalPdf; - // String extension = useTxt ? ".txt" : ".pdf"; - - // for (int j = 0; j < 2; j++) { - // // Create unique temp file for EACH upload to avoid duplicate filename errors - // fileCounter++; - // File tempFile = - // File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // System.out.println("Created attachment ID: " + createResponse.get(1)); - // } else { - // System.out.println("Failed to create attachment: " + createResponse.get(0)); - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - // } - - // // Fetch object IDs - // sourceObjectIds.clear(); - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } - // } - - // // Save Book1 - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Copy attachments from Book1's chapter to Book2's chapter - // if (sourceObjectIds.size() == 6) { - // int objectIdIndex = 0; - // for (String facetName : facet) { - // List facetObjectIds = - // sourceObjectIds.subList( - // objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, - // facetObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - // } - - // objectIdIndex += 2; - // } - - // // Save Book2 - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book after copying attachments"); - // } - - // // Verify attachments were copied - // for (String facetName : facet) { - // List> targetMetadata = - // api.fetchEntityMetadata( - // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); - // if (targetMetadata.size() != 2) { - // fail("Expected 2 attachments in facet " + facetName + ", found " + - // targetMetadata.size()); - // } - // for (Map meta : targetMetadata) { - // String attachmentId = (String) meta.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, - // attachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment in facet: " + facetName); - // } - // } - // } - - // // Cleanup - delete both books after verification - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // } else { - // fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); - // } - // } - - // @Test - // @Order(37) - // void testCopyAttachmentsWithNotePreserved() throws IOException { - // System.out.println("Test (37): Copy attachments with note field preserved"); - - // // Create source book and chapter - // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create attachments with notes in source chapter - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] sourceAttachmentIds = new String[facet.length]; - // String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); - - // for (int i = 0; i < facet.length; i++) { - // // Create unique temp file for each facet - // File tempFile = - // File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), - // ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // sourceAttachmentIds[i] = createResponse.get(1); - - // // Update note field using RequestBody - // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - // String noteResponse = - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // noteBody); - // if (!noteResponse.equals("Updated")) { - // fail("Could not update note for attachment in facet: " + facet[i]); - // } - // System.out.println("Note updated for facet: " + facet[i]); - // } - - // // Save source book - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Verify notes were saved in source - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // if (!testNote.equals(metadata.get("note"))) { - // fail("Note not saved correctly in source for facet: " + facet[i]); - // } - // } - - // // Create target book and chapter - // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Get object IDs and copy attachments - // for (int i = 0; i < facet.length; i++) { - // Map sourceMetadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // String objectId = sourceMetadata.get("objectId").toString(); - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // System.out.println("Attachment copied to facet: " + facet[i]); - // } - - // // Save target book - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify notes were preserved in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], - // copyAttachmentTargetChapter); - // if (targetMetadata.isEmpty()) { - // fail("No attachments found in target facet: " + facet[i]); - // } - // Map copiedAttachment = targetMetadata.get(0); - // String copiedNote = (String) copiedAttachment.get("note"); - // if (!testNote.equals(copiedNote)) { - // fail( - // "Note not preserved after copy in facet: " - // + facet[i] - // + ". Expected: " - // + testNote - // + ", Got: " - // + copiedNote); - // } - // System.out.println("Note preserved in target facet: " + facet[i]); - // } - - // System.out.println("Test 37 passed - notes preserved during copy"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // copyAttachmentSourceBook = null; - // copyAttachmentTargetBook = null; - // } - - // @Test - // @Order(38) - // void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { - // System.out.println("Test (38): Copy attachments with secondary properties preserved"); - - // // Use entities from test 37 or create new ones if needed - // boolean sourceBookJustCreated = false; - // boolean targetBookJustCreated = false; - - // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - // copyAttachmentSourceBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - // sourceBookJustCreated = true; - // } - - // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - // copyAttachmentTargetBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - // targetBookJustCreated = true; - // } - - // // If source book was just created, save it first before we can edit it - // if (sourceBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created source book"); - // } - // } - - // // If target book was just created, save it first - // if (targetBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created target book"); - // } - // } - - // // Edit source book - // String editResponse = - // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source book"); - // } - - // // Create new attachments with secondary properties - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] sourceAttachmentIds = new String[facet.length]; - // Boolean testBooleanProp = true; - // Integer testIntegerProp = 12345; - - // for (int i = 0; i < facet.length; i++) { - // // Create unique temp file - // File tempFile = - // File.createTempFile( - // "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // sourceAttachmentIds[i] = createResponse.get(1); - - // // Update secondary properties using RequestBody (customProperty6 - Boolean, - // customProperty2 - - // // Integer) - // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - // String boolResponse = - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // boolBody); - // if (!boolResponse.equals("Updated")) { - // System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); - // } - - // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - // String intResponse = - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // intBody); - // if (!intResponse.equals("Updated")) { - // System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); - // } - - // System.out.println("Secondary properties updated for facet: " + facet[i]); - // } - - // // Save source book - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Edit target book - // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // // Copy attachments to target - // for (int i = 0; i < facet.length; i++) { - // Map sourceMetadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // String objectId = sourceMetadata.get("objectId").toString(); - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); - // } - - // // Save target book - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify secondary properties were preserved in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], - // copyAttachmentTargetChapter); - - // // Find the attachment we just copied (most recent one) - // boolean found = false; - // for (Map attachment : targetMetadata) { - // Object boolProp = attachment.get("customProperty6"); - // Object intProp = attachment.get("customProperty2"); - - // if (boolProp != null && intProp != null) { - // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { - // found = true; - // System.out.println("Secondary properties preserved in target facet: " + facet[i]); - // break; - // } - // } - // } - // if (!found) { - // System.out.println( - // "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); - // } - // } - - // System.out.println("Test 38 passed - secondary properties checked during copy"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // copyAttachmentSourceBook = null; - // copyAttachmentTargetBook = null; - // } - - // @Test - // @Order(39) - // void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { - // System.out.println( - // "Test (39): Copy attachments with both note and secondary properties preserved"); - - // // Use entities from previous tests or create new ones - // boolean sourceBookJustCreated = false; - // boolean targetBookJustCreated = false; - - // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - // copyAttachmentSourceBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - // sourceBookJustCreated = true; - // } - - // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - // copyAttachmentTargetBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - // targetBookJustCreated = true; - // } - - // // If source book was just created, save it first before we can edit it - // if (sourceBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created source book"); - // } - // } - - // // If target book was just created, save it first - // if (targetBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created target book"); - // } - // } - - // // Edit source book - // String editResponse = - // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source book"); - // } - - // // Create new attachments with both note and secondary properties - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] sourceAttachmentIds = new String[facet.length]; - // String testNote = "Combined test note - " + System.currentTimeMillis(); - // Boolean testBooleanProp = true; - // Integer testIntegerProp = 99999; - - // for (int i = 0; i < facet.length; i++) { - // // Create unique temp file - // File tempFile = - // File.createTempFile( - // "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // sourceAttachmentIds[i] = createResponse.get(1); - - // // Update note using RequestBody - // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // noteBody); - - // // Update secondary properties using RequestBody - // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // boolBody); - - // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // intBody); - - // System.out.println("Note and secondary properties updated for facet: " + facet[i]); - // } - - // // Save source book - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Edit target book - // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // // Copy attachments to target - // for (int i = 0; i < facet.length; i++) { - // Map sourceMetadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // String objectId = sourceMetadata.get("objectId").toString(); - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // System.out.println("Attachment with note and properties copied to facet: " + facet[i]); - // } - - // // Save target book - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify note and secondary properties were preserved in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], - // copyAttachmentTargetChapter); - - // boolean noteFound = false; - // boolean propsFound = false; - - // for (Map attachment : targetMetadata) { - // String copiedNote = (String) attachment.get("note"); - // Object boolProp = attachment.get("customProperty6"); - // Object intProp = attachment.get("customProperty2"); - - // if (testNote.equals(copiedNote)) { - // noteFound = true; - // System.out.println("Note preserved in target facet: " + facet[i]); - // } - - // if (boolProp != null && intProp != null) { - // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { - // propsFound = true; - // System.out.println("Secondary properties preserved in target facet: " + facet[i]); - // } - // } - // } - - // if (!noteFound) { - // System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); - // } - // if (!propsFound) { - // System.out.println( - // "Warning: Secondary properties may not be preserved in facet: " + facet[i]); - // } - // } - - // // Cleanup - delete both books - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - - // // Reset static variables - // copyAttachmentSourceBook = null; - // copyAttachmentTargetBook = null; - // copyAttachmentSourceChapter = null; - // copyAttachmentTargetChapter = null; - - // System.out.println("Test 39 passed - both note and secondary properties checked during - // copy"); - // } - - // @Test - // @Order(40) - // void testCopyAttachmentsWithInvalidObjectId() throws IOException { - // System.out.println("Test (40): Copy attachments with invalid object ID should fail"); - - // // Create independent test entities (don't rely on previous tests) - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create test book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create test chapter"); - // } - - // // Save the book first so it's not in draft mode - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save test book"); - // } - - // // Now edit it to test copy with invalid object IDs - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit test book"); - // } - - // // Try to copy with invalid object ID - // for (String facetName : facet) { - // try { - // List invalidObjectIds = new ArrayList<>(); - // invalidObjectIds.add("invalidObjectId123"); - // invalidObjectIds.add("anotherInvalidId456"); - // api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, - // invalidObjectIds); - // fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); - // } catch (IOException e) { - // // Expected - copy should fail with invalid object ID - // System.out.println( - // "Expected error received for invalid object ID in facet " - // + facetName - // + ": " - // + e.getMessage()); - // } - // } - - // // Save and cleanup - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - - // // Also cleanup test 36 entities if they exist - // if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { - // try { - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // } catch (Exception e) { - // // Ignore - may already be deleted - // } - // } - // if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { - // try { - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // } catch (Exception e) { - // // Ignore - may already be deleted - // } - // } - // } - - // @Test - // @Order(41) - // void testCopyAttachmentsToExistingChapter() throws IOException { - // System.out.println( - // "Test (41): Copy attachments to an existing chapter that already has attachments"); - - // // Create Book1 with source chapter - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create Book2 with target chapter that has existing attachments - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (targetBookID.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - // if (targetChapterID.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Create temp files with unique names to avoid duplicate filename errors - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test41_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create attachment in source chapter - // List sourceObjIds = new ArrayList<>(); - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create source attachment"); - // } - // String attachmentId = createResponse.get(1); - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - // sourceObjIds.add(metadata.get("objectId").toString()); - // } - - // // Create existing attachment in target chapter - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", targetChapterID); - // postData.put("mimeType", "text/plain"); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create existing target attachment"); - // } - // String attachmentId = createResponse.get(1); - // } - - // // Save both books - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Edit target book and copy attachments - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // // Copy from source to target (target already has 1 attachment per facet) - // for (int i = 0; i < facet.length; i++) { - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjIds.get(i)); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, - // objectIdsToCopy); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // } - - // // Save target book - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) - // for (String facetName : facet) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.size() != 2) { - // fail( - // "Expected 2 attachments in facet " - // + facetName - // + " (1 existing + 1 copied), found " - // + targetMetadata.size()); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // // ============= LINK RENAME TESTS (47-49) ============= - - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (47): Rename link in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links in all facets - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit and rename links - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // String renameResponse = - // api.renameAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); - // if (!renameResponse.equals("Renamed")) { - // fail("Could not rename link in facet: " + facetName); - // } - // } - - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book after renaming links"); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(48) - // void testRenameLinkDuplicate() throws IOException { - // System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create two links in all facets - // for (String facetName : facet) { - // String createLinkResponse1 = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "link1", - // "https://www.example1.com"); - // String createLinkResponse2 = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "link2", - // "https://www.example2.com"); - // if (!createLinkResponse1.equals("Link created successfully") - // || !createLinkResponse2.equals("Link created successfully")) { - // fail("Could not create links in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit and try to rename link2 to link1 (duplicate) - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.size() < 2) { - // fail("Expected 2 links in facet: " + facetName); - // } - - // // Find link2 and rename to link1 - // for (Map attachment : attachments) { - // if ("link2".equals(attachment.get("fileName"))) { - // String linkId = (String) attachment.get("ID"); - // api.renameAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); - // break; - // } - // } - // } - - // // Save should fail with duplicate error - // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // ObjectMapper mapper = new ObjectMapper(); - // try { - // JsonNode errorJson = mapper.readTree(saveError); - // String errorMessage = errorJson.path("error").path("message").asText(); - // if (!errorMessage.contains("already exists")) { - // fail("Expected duplicate error but got: " + saveError); - // } - // } catch (Exception e) { - // if (!saveError.contains("already exists")) { - // fail("Expected duplicate error but got: " + saveError); - // } - // } - - // // Cleanup - // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(49) - // void testRenameLinkUnsupportedCharacters() throws IOException { - // System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links in all facets - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit and rename with unsupported characters - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // api.renameAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); - // } - - // // Save should fail with unsupported characters error - // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveError.contains("unsupported characters")) { - // fail("Expected unsupported characters error but got: " + saveError); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // // ============= LINK EDIT TESTS (50-53) ============= - - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (50): Edit existing link URL in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links in all facets - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit links - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // String editLinkResponse = - // api.editLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // linkId, - // "https://www.editedexample.com"); - // if (!editLinkResponse.equals("Link edited successfully")) { - // fail("Could not edit link in facet: " + facetName); - // } - // } - - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book after editing links"); - // } - - // // Verify links open successfully - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // for (Map attachment : attachments) { - // String linkId = (String) attachment.get("ID"); - // String openResponse = - // api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); - // if (!openResponse.equals("Attachment opened successfully")) { - // fail("Could not open edited link in facet: " + facetName); - // } - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(51) - // void testEditLinkFailureInvalidURL() throws IOException { - // System.out.println("Test (51): Edit link with invalid URL fails in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // try { - // api.editLink( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, - // "https://editedexample"); - // fail("Edit link should have failed with invalid URL in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected error received for invalid URL in facet " + facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(52) - // void testEditLinkFailureEmptyURL() throws IOException { - // System.out.println("Test (52): Edit link with empty URL fails in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // try { - // api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); - // fail("Edit link should have failed with empty URL in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected error received for empty URL in facet " + facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, - // testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // try { - // apiNoRoles.editLink( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, - // "https://www.edited.com"); - // fail("Edit link should have failed without SDM roles in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected permission error received in facet " + facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // // ============= COPY LINK TESTS (54-58) ============= - - // @Test - // @Order(54) - // void testCopyLinkSuccessNewChapter() throws IOException { - // System.out.println("Test (54): Copy link from one chapter to another new chapter"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // if (sourceChapterID.equals("Could not create entity") - // || targetChapterID.equals("Could not create entity")) { - // fail("Could not create chapters"); - // } - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // // Create links in source chapter - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch object IDs - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // // Copy links to target chapter - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link for facet " + facetName + ": " + copyResponse); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify link type and URL - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.isEmpty()) { - // fail("No links found in target chapter for facet: " + facetName); - // } - - // Map copiedLink = targetMetadata.get(0); - // String receivedUrl = (String) copiedLink.get("linkUrl"); - // assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); - - // objectIdIndex++; - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(55) - // void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { - // System.out.println("Test (55): Copy invalid link object ID to chapter fails"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // for (String facetName : facet) { - // try { - // List invalidObjectIds = new ArrayList<>(); - // invalidObjectIds.add("incorrectObjectId"); - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // invalidObjectIds); - // fail("Copy should have thrown error for invalid object ID in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected error received for invalid object ID in facet " + - // facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(56) - // void testCopyLinkToExistingChapter() throws IOException { - // System.out.println("Test (56): Copy link to existing chapter that has attachments"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // // Create links in source chapter - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sourceLink" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in source chapter for facet: " + facet[i]); - // } - // } - - // // Create existing links in target chapter - // for (int i = 0; i < facet.length; i++) { - // String linkName = "existingLink" + i; - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facet[i], - // targetChapterID, - // linkName, - // "https://www.existing.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create existing link in target chapter for facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch source object IDs - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // // Copy links to target chapter - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link for facet " + facetName); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify target has 2 links (existing + copied) - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.size() != 2) { - // fail( - // "Expected 2 links in target chapter facet " - // + facetName - // + ", found " - // + targetMetadata.size()); - // } - - // objectIdIndex++; - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(57) - // void testCopyLinkNoSDMRoles() throws IOException { - // System.out.println("Test (57): Copy link fails due to no SDM roles"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch object IDs - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // // Try to copy with no SDM roles - // int objectIdIndex = 0; - // for (String facetName : facet) { - // try { - // // Use normal api to put book in draft mode - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // // Use apiNoRoles to attempt copy (should fail) - // apiNoRoles.copyAttachment( - // appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - // fail("Copy should have failed without SDM roles in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected permission error in facet " + facetName); - // // Discard draft to clean up for next iteration - // api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); - // } - // objectIdIndex++; - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(58) - // void testCopyLinkFromDraftChapter() throws IOException { - // System.out.println("Test (58): Copy link from draft chapter to another chapter"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // // Create links in source chapter (NOT saved yet - draft mode) - // for (int i = 0; i < facet.length; i++) { - // String linkName = "draftLink" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - - // // Save target book only - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch object IDs from draft - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // if (linkObjectIds.size() != facet.length) { - // fail("Could not fetch all object IDs from draft"); - // } - - // // Copy links from draft to target - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link from draft for facet " + facetName); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify link was copied - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.isEmpty()) { - // fail("No links found in target chapter for facet: " + facetName); - // } - - // objectIdIndex++; - // } - - // // Cleanup - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= - - // @Test - // @Order(59) - // void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { - // System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // if (sourceChapterID.equals("Could not create entity") - // || targetChapterID.equals("Could not create entity")) { - // fail("Could not create chapters"); - // } - - // // Create temp files with unique names - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test59_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List sourceObjectIds = new ArrayList<>(); - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(new ArrayList<>()); - // } - - // // Create attachments in source chapter (still in draft) - // for (int i = 0; i < facet.length; i++) { - // postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); - // File file = i == 1 ? tempTxt : tempPdf; - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - - // // Fetch object IDs from draft - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } - // } - - // // Save target book only - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Copy attachments from draft to target - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment from draft for facet " + facetName); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify attachment was copied - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.isEmpty()) { - // fail("No attachments found in target chapter for facet: " + facetName); - // } - - // // Read attachment to verify - // String attachmentId = (String) targetMetadata.get(0).get("ID"); - // String readResponse = - // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // attachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment in facet: " + facetName); - // } - - // objectIdIndex++; - // } - - // // Cleanup - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // // ============= CHANGELOG TESTS (60-64) ============= - - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println("Test (60): View changelog for newly created attachment in chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String facetName = facet[i]; - - // // Create book and chapter - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = - // File.createTempFile( - // "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } - - // String attachmentId = createResponse.get(1); - - // // Fetch changelog - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); - - // // Cleanup - // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - // } - // } - - // @Test - // @Order(61) - // void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - // System.out.println("Test (61): Changelog after modifying note and custom property in - // chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String facetName = facet[i]; - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = - // File.createTempFile( - // "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Update note - // String notesValue = "Test note for changelog verification"; - // RequestBody updateNotesBody = - // RequestBody.create( - // MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); - - // // Update custom property - // RequestBody bodyInt = - // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": - // 12345}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); - - // // Save - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // // Fetch changelog - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // int numItems = (int) changelogResponse.get("numItems"); - // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - // } - - // @Test - // @Order(62) - // void testChangelogAfterRenamingAttachment() throws IOException { - // System.out.println("Test (62): Changelog after renaming attachment in chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String facetName = facet[i]; - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = - // File.createTempFile( - // "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Rename attachment - // String renameResponse = - // api.renameAttachment( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // attachmentId, - // "renamed_file.txt"); - // if (!renameResponse.equals("Renamed")) { - // fail("Could not rename attachment"); - // } - - // // Save - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // // Fetch changelog - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // int numItems = (int) changelogResponse.get("numItems"); - // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - // } - - // @Test - // @Order(63) - // void testChangelogForCopiedAttachment() throws IOException { - // System.out.println("Test (63): Changelog for copied attachment in chapter"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), - // ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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 in source - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Save both books - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Get object ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - - // // Copy to target - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment"); - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch changelog for copied attachment - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - // String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); - - // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // Map changelogResponse = - // api.fetchChangelog( - // appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // int numItems = (int) changelogResponse.get("numItems"); - // assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(64) - // void testChangelogForNewChapter() throws IOException { - // System.out.println("Test (64): Changelog for attachment in newly created chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), - // ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Fetch changelog before saving - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // assertEquals( - // 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); - - // @SuppressWarnings("unchecked") - // List> changeLogs = - // (List>) changelogResponse.get("changeLogs"); - // assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); - - // // Cleanup - // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - // } - - // // ============= MOVE ATTACHMENT TESTS (65-75) ============= - - // @Test - // @Order(65) - // void testMoveAttachmentsWithSourceFacet() throws IOException { - // System.out.println("Test (65): Move attachments from source chapter to target chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create temp files - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), - // tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List sourceAttachmentIds = new ArrayList<>(); - // File[] files = {tempPdf, tempTxt}; - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source chapter"); - // } - // } - - // // Save source book - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Get object IDs and folder ID - // List moveObjectIds = new ArrayList<>(); - // String sourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, - // attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (sourceFolderId == null && metadata.containsKey("folderId")) { - // sourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } - - // // Create target book and chapter - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (targetBookID.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - // if (targetChapterID.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Save target book before moving attachments (moveAttachments requires Active entity) - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book before move"); - // } - - // // Move attachments to Active entity - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } - - // // Verify - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadata.size(), - // "Target should have all attachments after move"); - - // List> sourceMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // } - // } - - // @Test - // @Order(66) - // void testMoveAttachmentsToChapterWithDuplicate() throws IOException { - // System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Create attachment in source with specific name - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, - // originalPdf); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create source attachment"); - // } - // String sourceAttachmentId = createResponse.get(1); - - // // Create attachment in target with SAME name (duplicate) - // postData.put("up__ID", targetChapterID); - // createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, - // originalPdf); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create target attachment"); - // } - - // // Save both - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Get source object ID and folder ID - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, - // sourceAttachmentId); - // String objectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move to saved target - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Move should handle duplicate - attachment stays in source - - // // Verify source still has attachment (duplicate not moved) - // List> sourceMetadataAfter = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertTrue( - // sourceMetadataAfter.size() >= 1, - // "Source should still have attachment when duplicate exists in target"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(67) - // void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { - // System.out.println("Test (67): Move attachments with notes and secondary properties"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // // Add note and secondary property - // String testNote = "Test note for move"; - // RequestBody noteBody = - // RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + - // "\"}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); - - // RequestBody propBody = - // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); - - // // Save source - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get object ID and folder ID - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // // Create target - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Save target before move - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null"); - // } - - // // Verify note was preserved - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - // if (!targetMetadata.isEmpty()) { - // String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); - // Map movedMetadata = - // api.fetchMetadata( - // appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); - - // // Note should be preserved - // if (movedMetadata.containsKey("note")) { - // assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(68) - // void testMoveAttachmentsPartialFailure() throws IOException { - // System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get real object ID and folder ID - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String realObjectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // // Create target - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Save target before move - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Try to move with mix of valid and invalid object IDs - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(realObjectId); - // moveObjectIds.add("invalidObjectId123"); - - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Should handle partial failure - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(69) - // void testMoveAttachmentsEmptyList() throws IOException { - // System.out.println("Test (69): Move attachments with empty object ID list"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Try to move with empty list - // List emptyObjectIds = new ArrayList<>(); - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - - // try { - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // "someFolderId", - // emptyObjectIds, - // targetFacet, - // sourceFacet); - // // Should either fail or do nothing - // } catch (Exception e) { - // System.out.println("Expected: Move with empty list handled: " + e.getMessage()); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(70) - // void testMoveAttachmentsToSameChapter() throws IOException { - // System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // 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"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Get object ID and folder ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String folderId = metadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move to same chapter - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // testChapterID, - // folderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Should handle gracefully - attachment stays in place - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Verify attachment still exists - // List> metadataAfter = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); - // assertEquals(1, metadataAfter.size(), "Attachment should still exist"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(71) - // void testMoveAttachmentsBetweenFacets() throws IOException { - // System.out.println("Test (71): Move attachments between different facets in chapters"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create in attachments facet - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Get object ID and folder ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String sourceFolderId = metadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move from attachments to references facet - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[1], // references facet - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify moved to different facet - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); - // assertTrue( - // targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(72) - // void testMoveMultipleAttachments() throws IOException { - // System.out.println("Test (72): Move multiple attachments at once between chapters"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // // Create multiple temp files - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test72_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List sourceAttachmentIds = new ArrayList<>(); - // File[] files = {tempPdf, tempTxt}; - // String[] mimeTypes = {"application/pdf", "text/plain"}; - - // for (int i = 0; i < files.length; i++) { - // postData.put("mimeType", mimeTypes[i]); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get object IDs - // List moveObjectIds = new ArrayList<>(); - // String sourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (sourceFolderId == null) { - // sourceFolderId = metadata.get("folderId").toString(); - // } - // } - - // // Create target - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Save target before move - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Move all at once - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify all moved - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadata.size(), - // "All attachments should be moved to target"); - - // List> sourceMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(73) - // void testMoveAttachmentsAllFacets() throws IOException { - // System.out.println("Test (73): Move attachments from all facets between chapters"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // // Create attachment in each facet - // for (int i = 0; i < facet.length; i++) { - // String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); - // File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Move from each facet - // for (int i = 0; i < facet.length; i++) { - // List> sourceMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // if (sourceMetadata.isEmpty()) { - // continue; - // } - - // String attachmentId = (String) sourceMetadata.get(0).get("ID"); - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String sourceFolderId = metadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - // } - - // // Verify all facets have attachments in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - // assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + - // facet[i]); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(74) - // void testChainMoveAttachments() throws IOException { - // System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || target1BookID.equals("Could not create entity") - // || target2BookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String target1ChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); - // String target2ChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), - // ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); - - // // First move: Source -> Target1 - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move to target1 - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // target1ChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify in target1 - // List> target1Metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - // assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); - - // // Second move: Target1 -> Target2 - // String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); - // Map target1AttMetadata = - // api.fetchMetadata( - // appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); - // String target1ObjectId = target1AttMetadata.get("objectId").toString(); - // String target1FolderId = target1AttMetadata.get("folderId").toString(); - - // moveObjectIds.clear(); - // moveObjectIds.add(target1ObjectId); - - // // Move to target2 - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // target2ChapterID, - // target1FolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify final state - // List> target2Metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); - // assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); - - // target1Metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - // assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); - - // List> sourceFinalMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, target1BookID); - // api.deleteEntity(appUrl, bookEntityName, target2BookID); - // } - - // @Test - // @Order(75) - // void testMoveAttachmentsWithoutSDMRole() throws IOException { - // System.out.println("Test (75): Move attachments fails without SDM role"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = - // File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get object ID and folder ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String sourceFolderId = metadata.get("folderId").toString(); - - // // Create target with no role user - // String targetBookID = - // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (targetBookID.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // String targetChapterID = - // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, - // targetBookID); - - // // Save target before move - // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // boolean moveFailed = false; - // String errorMessage = null; - - // try { - // Map moveResult = - // apiNoRoles.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null || moveResult.containsKey("error")) { - // moveFailed = true; - // errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; - // } - // } catch (Exception e) { - // moveFailed = true; - // errorMessage = e.getMessage(); - // } - - // assertTrue(moveFailed, "Move should fail without SDM role"); - // System.out.println("Move correctly failed without SDM role: " + errorMessage); - - // // Verify source still has attachment - // List> sourceMetadataAfter = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(76) - // void testRenameChapterAttachmentWithExtensionChange() throws IOException { - // System.out.println( - // "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all - // facets - should return extension change warning"); - - // // Step 1: Create a new book and chapter - // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (newBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - // String newChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - // if (newChapterID.equals("Could not create entity")) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not create chapter"); - // } - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book: " + saveResponse); - // } - - // // Step 2: Upload a PDF attachment to each facet in the chapter - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", newChapterID); - // 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, bookEntityName, srvpath, newBookID); - // if (!"Entity in draft mode".equals(editResponse)) { - // fail("Could not put book in draft mode for PDF upload"); - // } - - // String[] facetAttachmentIDs = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // facetAttachmentIDs[i] = - // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - // if (facetAttachmentIDs[i] == null) { - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - // } - // } - - // // Step 3: Save the book - // String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // if (!savedAfterUpload.equals("Saved")) { - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not save book after PDF upload: " + savedAfterUpload); - // } - - // // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt - // for (int i = 0; i < facet.length; i++) { - // String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // if (!"Entity in draft mode".equals(editDraftResponse)) { - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not put book in draft mode for rename on facet: " + facet[i]); - // } - - // String renameResponse = - // api.renameAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // newChapterID, - // facetAttachmentIDs[i], - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - // } - - // // Step 6: Save and validate the extension change warning message - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } - - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); - // } - - // // Clean up - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // } - - // @Test - // @Order(77) - // void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { - // System.out.println( - // "Test (77) : Upload chapter attachment in draft, rename changing extension before save - // across all facets - should return extension change warning"); - - // for (int i = 0; i < facet.length; i++) { - // // Step 1: Create a new book and chapter draft (do NOT save) - // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (newBookID.equals("Could not create entity")) { - // fail("Could not create book for facet: " + facet[i]); - // } - // String newChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - // if (newChapterID.equals("Could not create entity")) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not create chapter for facet: " + facet[i]); - // } - - // // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft - // (unsaved) - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", newChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String facetAttachmentID = - // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - // if (facetAttachmentID == null) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - // } - - // // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not - // saved - // String renameResponse = - // api.renameAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // newChapterID, - // facetAttachmentID, - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - // } - - // // Step 4: Save the book — should receive extension change warning, not "Saved" - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } - - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); - - // // Clean up - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // } - // } + @Test + @Order(7) + void testRenameChapterAttachments() { + System.out.println("Test (7) : Rename single attachment, reference, and footnote inchapter"); + Boolean testStatus = true; + + try { + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + if ("Entity in draft mode".equals(response)) { + String[] name = {"sample123", "reference123", "footnote123"}; + for (int i = 0; i < facet.length; i++) { + // Read the facet to ensure it exists + response = + api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); + if (!"Renamed".equals(response)) { + testStatus = false; + System.out.println(facet[i] + " was not renamed: " + response); + } + } + // Save book draft if everything is renamed + if (testStatus) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if (!"Saved".equals(response)) { + testStatus = false; + System.out.println("Book draft was not saved: " + response); + } + } else { + // Attempt save despite potential rename failures + api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + } + } else { + testStatus = false; + System.out.println("Book was not put into draft mode: " + response); + } + } catch (Exception e) { + testStatus = false; + System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); + } + + if (!testStatus) { + fail("There was an error during the rename test process for chapter."); + } + } + + @Test + @Order(8) + void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { + System.out.println("Test (8): Create chapter attachments with unsupported characters"); + 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("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, bookEntityName, srvpath, bookID); + if (!"Entity in draft mode".equals(response)) { + fail("Book not in draft mode: " + response); + return; + } + + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", chapterID); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); + + if (!"Attachment created".equals(createResponse.get(0))) { + fail("Could not create attachment in chapter facet: " + facet[i]); + return; + } + + String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE + response = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); + + System.out.println("Rename response for chapter " + facet[i] + ": " + response); + } + + // Save should fail + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + // ---------------- PARSE JSON ---------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + String message = root.path("error").path("message").asText(); + + // ---------------- NORMALIZE MESSAGE ---------------- + // 1. Normalize smart quotes + // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared + message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); + + // ---------------- EXPECTED MESSAGE (EXACT) ---------------- + String expectedMessage = + "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" + + "Table: attachments\n" + + "Page: IntegrationTestEntity"; + + if (message.equals(expectedMessage)) { + + for (int i = 0; i < facet.length; i++) { + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if ("Saved".equals(response)) { + testStatus = true; + } + } + + if (!testStatus) { + fail("Test for unsupported characters in chapter attachments failed"); + } + } + + @Test + @Order(9) + void testRenameSingleDuplicateInChapter() throws IOException { + System.out.println( + "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); + Boolean testStatus = false; + int counter = 0; + + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Edit entity response: " + response); + + if ("Entity in draft mode".equals(response)) { + // To create a duplicate within the same facet, we need to rename ID2[i] to + // the same name as an existing file in that facet. After test 7, the existing files are: + // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) + // We rename ID2[i] (sample123.txt from test 8) to these names which already exist + String[] duplicateNames = {"sample123", "reference123", "footnote123"}; + String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; + + // Try to rename to duplicate file names (names that already exist in each facet) + for (int i = 0; i < facet.length; i++) { + response = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); + System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); + if ("Renamed".equals(response)) { + counter++; + } + } + System.out.println("Renamed count: " + counter); + + if (counter == facet.length) { + // Try to save - should fail with duplicate error + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Save response (expecting error): " + response); + + // Parse JSON response to check for duplicate error + ObjectMapper mapper = new ObjectMapper(); + try { + JsonNode root = mapper.readTree(response); + String message = root.path("error").path("message").asText(); + + if (message.contains("already exists")) { + System.out.println("Duplicate error detected as expected: " + message); + counter = 0; + // Rename with valid different names + for (int i = 0; i < facet.length; i++) { + response = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); + System.out.println("Rename " + facet[i] + " to valid name: " + response); + if ("Renamed".equals(response)) { + counter++; + } + } + + if (counter == facet.length) { + // Save should now succeed + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Final save response: " + response); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } else { + System.out.println("Unexpected error message: " + message); + } + } catch (Exception e) { + // Response might not be JSON if save succeeded (shouldn't happen with duplicates) + System.out.println("Response was not JSON error: " + response); + // If save succeeded unexpectedly, we still need to ensure book is saved + if ("Saved".equals(response)) { + System.out.println( + "Save succeeded unexpectedly - duplicates might be in different facets"); + } + } + } + } else { + System.out.println("Book was not put into draft mode: " + response); + } + + if (!testStatus) { + fail("Duplicate rename test failed for chapter"); + } + } + + @Test + @Order(10) + void testRenameToValidateNamesInChapter() throws IOException { + System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); + Boolean testStatus = false; + + // Create a new book and chapter for this test + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + bookID3 = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); + if (!"Could not create entity".equals(chapterResponse)) { + chapterID3 = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID3); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); + } + + String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; + + boolean allRenamed = true; + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); + if (!"Renamed".equals(response1)) { + allRenamed = false; + System.out.println( + "Failed to rename " + + facet[i] + + " to valid name " + + validNames[i] + + ": " + + response1); + } + } + + if (allRenamed) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } + } + + if (!testStatus) { + fail("Could not rename chapter attachments to valid names"); + } + } + + @Test + @Order(11) + void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { + System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); + boolean testStatus = true; + + try { + String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Edit entity response: " + response); + + if (response.equals("Entity in draft mode")) { + String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; + for (int i = 0; i < facet.length; i++) { + response = + apiNoRoles.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); + System.out.println("Rename response for " + facet[i] + ": " + response); + if (!"Renamed".equals(response)) { + testStatus = false; + } + } + + if (testStatus) { + // Save should fail with permission error + response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Save response (expecting permission error): " + response); + + // The expected error should indicate no permissions to update + String expected = + "[{\"code\":\"\",\"message\":\"Could not update the following files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + + // Check if response contains permission error + if (!response.equals(expected) + && !response.contains("do not have the required permissions")) { + System.out.println("Expected permission error but got: " + response); + testStatus = false; + } else { + System.out.println("Got expected permission error"); + } + } else { + // Some renames failed - save to release draft + apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + } + } else { + System.out.println("Could not edit entity: " + response); + testStatus = false; + } + } catch (Exception e) { + System.out.println("Exception: " + e.getMessage()); + testStatus = false; + } + + if (!testStatus) { + fail("Chapter attachment got renamed without SDM roles."); + } + } + + @Test + @Order(12) + void testDeleteSingleChapterAttachment() throws IOException { + System.out.println( + "Test (12) : Delete single attachment, reference, and footnote from chapter"); + Boolean testStatus = false; + int deleteCounter = 0; + + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + if (response.equals("Deleted")) deleteCounter++; + } + if (deleteCounter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if (response.equals("Saved")) { + int verifyCounter = 0; + for (int i = 0; i < facet.length; i++) { + response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + if (response.equals("Could not read Attachment")) verifyCounter++; + } + if (verifyCounter == facet.length) { + testStatus = true; + } else { + fail( + "Could not verify all deleted chapter facets. Verified: " + + verifyCounter + + "/" + + facet.length); + } + } else { + fail("Could not save book after deleting chapter attachments"); + } + } else { + fail( + "Could not delete all chapter attachments. Deleted: " + + deleteCounter + + "/" + + facet.length); + } + } else { + fail("Could not edit book to draft mode"); + } + + if (!testStatus) { + fail("Test failed to delete chapter attachments"); + } + } + + @Test + @Order(13) + void testUploadBlockedMimeTypeToChapter() throws IOException { + System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); + Boolean testStatus = false; + + // Create new book and chapter + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + bookID4 = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); + if (!"Could not create entity".equals(chapterResponse)) { + chapterID4 = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = + new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID4); + postData.put("mimeType", "application/rtf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + boolean allBlocked = true; + for (int i = 0; i < facet.length; i++) { + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID4, 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)) { + allBlocked = false; + System.out.println( + "Chapter facet " + + facet[i] + + " incorrectly accepted blocked mimeType: " + + actualResponse); + } + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); + if ("Saved".equals(response) && allBlocked) { + testStatus = true; + } + } + } + + if (!testStatus) { + fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); + } + } + + @Test + @Order(14) + void testDeleteBookAndChapter() { + System.out.println("Test (14) : Delete book (and its chapters)"); + Boolean testStatus = false; + // Delete books (chapters are deleted automatically as they're composition) + String response = api.deleteEntity(appUrl, bookEntityName, bookID); + String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); + String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); + String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); + if (response.equals("Entity Deleted") + && response2.equals("Entity Deleted") + && response3.equals("Entity Deleted") + && response4.equals("Entity Deleted")) testStatus = true; + if (!testStatus) fail("Could not delete books"); + } + + @Test + @Order(15) + void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { + System.out.println( + "Test (15) : Rename & Update secondary property in chapter before book is saved"); + System.out.println("Creating book and chapter"); + + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (!response.equals("Could not create entity")) { + bookID5 = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); + if (!chapterResponse.equals("Could not create entity")) { + chapterID5 = chapterResponse; + + System.out.println("Creating attachment, reference, and footnote in chapter"); + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID5); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + boolean allCreated = true; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); + if (tempID[i] == null || tempID[i].isEmpty()) { + System.out.println("Failed to create attachment for facet: " + facet[i]); + allCreated = false; + } + } + + System.out.println("Attachments, References, and Footnotes created in chapter"); + System.out.println( + "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + tempID[2]); + + if (!allCreated) { + fail("Could not create all attachments for test 15"); + } + + // Reset counter for this test + counter = 0; + + // Use valid dropdown value for customProperty1 + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + for (int i = 0; i < facet.length; i++) { + System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + System.out.println("Rename response for " + facet[i] + ": " + response1); + + // Update customProperty1 (String - dropdown value) + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); + + // Update customProperty2 (Integer) + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + + // Update customProperty5 (DateTime) - using customProperty5 like Books test + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + + // Update customProperty6 (Boolean) + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + // Check all updates succeeded + if ("Renamed".equals(response1) + && "Updated".equals(updateSecondaryPropertyResponse1) + && "Updated".equals(updateSecondaryPropertyResponse2) + && "Updated".equals(updateSecondaryPropertyResponse3) + && "Updated".equals(updateSecondaryPropertyResponse4)) { + counter++; + } else { + System.out.println( + "Update failed for " + + facet[i] + + ": rename=" + + response1 + + ", dropdown=" + + updateSecondaryPropertyResponse1 + + ", int=" + + updateSecondaryPropertyResponse2 + + ", datetime=" + + updateSecondaryPropertyResponse3 + + ", bool=" + + updateSecondaryPropertyResponse4); + } + } + + System.out.println("Counter after all facets: " + counter); + if (counter == facet.length) { + // Save the book (not the chapter) + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + System.out.println("Save response: " + response); + if ("Saved".equals(response)) { + testStatus = true; + } + } else { + System.out.println( + "Counter is less than " + facet.length + ", not saving. Counter: " + counter); + } + } + } + + if (!testStatus) { + fail( + "Could not update secondary properties in chapter before book save. Counter: " + counter); + } + } + + @Test + @Order(16) + void testUploadNAttachmentsToChapter() throws IOException { + System.out.println("Test (16) : Upload N attachments to chapter"); + Boolean testStatus = false; + counter = 0; + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); + + for (int j = 0; j < 5; j++) { + // Create temp file with unique name per iteration + File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); + Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID5); + 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, bookEntityName, srvpath, bookID5); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + String check = facetResponse.get(0); + if (check.equals("Attachment created")) { + counter++; + } else { + System.out.println( + "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + } + } + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if (!response.equals("Saved")) { + System.out.println( + "Failed to save book after creating attachments in chapter: " + response); + } + } else { + System.out.println("Could not edit book draft: " + response); + } + tempFile.delete(); + } + + if (counter == 15) { // 5 iterations * 3 facets + testStatus = true; + } + + if (!testStatus) { + fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); + } + } + + @Test + @Order(17) + void testDiscardDraftWithoutChapterAttachments() { + System.out.println("Test (17) : Discard book draft without chapter attachments"); + Boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + // Create chapter but don't add attachments + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft without chapter attachments was not discarded properly"); + } + } + + @Test + @Order(18) + void testDiscardDraftWithChapterAttachments() throws IOException { + System.out.println("Test (18) : Discard book draft with chapter attachments"); + Boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + // Create chapter + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + 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 chapter + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + String check = facetResponse.get(0); + if (!check.equals("Attachment created")) { + System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + } + } + + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft with chapter attachments was not discarded properly"); + } + } + + @Test + @Order(19) + void testUploadChapterAttachmentWithoutSDMRole() throws IOException { + System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); + Boolean testStatus = true; + + String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + try { + List createResponse = + apiNoRoles.createAttachment( + appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); + String check = createResponse.get(0); + + if (check.equals("Attachment created")) { + testStatus = false; + } + } catch (Exception e) { + // Expected to fail + testStatus = true; + } + + apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + } + } + + if (!testStatus) { + fail("Chapter attachment was uploaded without SDM roles"); + } + } + + @Test + @Order(20) + void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { + System.out.println( + "Test (20): Rename & Update secondary property in chapter after book is saved"); + Boolean testStatus = false; + counter = 0; // Reset counter for this test + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + System.out.println("Editing book, response: " + response); + + if (response.equals("Entity in draft mode")) { + // Use unique names that won't conflict with existing attachments + String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; + Integer secondaryPropertyInt = 42; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + System.out.println("Renaming and updating secondary properties for chapter attachment"); + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + // Get the first attachment ID from the chapter + try { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); + if (!metadata.isEmpty()) { + tempID[i] = (String) metadata.get(0).get("ID"); + } + } catch (IOException e) { + fail("Could not fetch metadata for chapter: " + e.getMessage()); + } + + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if (response.equals("Saved")) { + testStatus = true; + System.out.println("Renamed & updated Secondary properties for chapter attachment"); + } + } + } + if (!testStatus) fail("Could not update secondary properties in chapter after book is saved"); + } + + @Test + @Order(21) + void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { + System.out.println( + "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); + System.out.println("Creating book and chapter"); + Boolean testStatus = false; + int localCounter = 0; + int createCounter = 0; + + // Create new book and chapter for this test + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + if (tempID[i] != null) { + createCounter++; + } + } + + // Only proceed if all facets were created successfully + if (createCounter == facet.length) { + // Prepare test data + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + localCounter++; + } + } + + if (localCounter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + // Fetch metadata and verify values weren't updated due to invalid property + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + + // Parse JSON response and check for expected error messages + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved with expected invalid property errors"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for chapter attachment is unsuccessful"); + } + } else { + System.out.println( + "Not all facets updated successfully. localCounter: " + localCounter); + } + } else { + System.out.println( + "Not all facets created successfully. createCounter: " + createCounter); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, tempBookID); + } + } + if (!testStatus) + fail("Could not update invalid secondary property in chapter before book is saved"); + } + + @Test + @Order(22) + void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { + System.out.println( + "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); + System.out.println("Creating book and chapter"); + Boolean testStatus = false; + int localCounter = 0; + int createCounter = 0; + + // Create new book and chapter + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + if (tempID[i] != null) { + createCounter++; + } + } + + // Only proceed if all facets were created successfully + if (createCounter != facet.length) { + api.deleteEntity(appUrl, bookEntityName, tempBookID); + fail("Not all facets created successfully. createCounter: " + createCounter); + } + + // Save the book first + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (!response.equals("Saved")) { + api.deleteEntity(appUrl, bookEntityName, tempBookID); + fail("Could not save book initially"); + } + + // Now edit to update with invalid property + response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (response.equals("Entity in draft mode")) { + String name1 = "sample.pdf"; + Integer secondaryPropertyInt = 12; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testidinvalid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + localCounter++; + } + } + + if (localCounter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + + // Parse JSON response and check for expected error messages + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved with expected invalid property errors"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for chapter attachment is unsuccessful"); + } + } else { + System.out.println( + "Not all facets updated successfully. localCounter: " + localCounter); + } + } + api.deleteEntity(appUrl, bookEntityName, tempBookID); + } + } + if (!testStatus) + fail("Could not update invalid secondary property in chapter after book is saved"); + } + + @Test + @Order(23) + void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { + System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); + + boolean testStatus = false; + + // Reuse bookID5 and chapterID5 + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + + if (response.equals("Entity in draft mode")) { + ClassLoader classLoader = getClass().getClassLoader(); + + // Use temp files with unique names to avoid duplicate name errors + File originalPdf = + new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + File originalTxt = + new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); + + File file1 = File.createTempFile("test23_pdf_", ".pdf"); + File file2 = File.createTempFile("test23_txt_", ".txt"); + Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData1 = new HashMap<>(); + postData1.put("up__ID", chapterID5); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); + + Map postData2 = new HashMap<>(postData1); + postData2.put("up__ID", chapterID5); + postData2.put("mimeType", "text/plain"); + + boolean allCreated = true; + String[] tempID1 = new String[facet.length]; + String[] tempID2 = new String[facet.length]; + + for (int i = 0; i < facet.length; i++) { + List response1 = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); + List response2 = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); + + if (response1.get(0).equals("Attachment created") + && response2.get(0).equals("Attachment created")) { + tempID1[i] = response1.get(1); // to keep one + tempID2[i] = response2.get(1); // will delete this one + } else { + System.out.println("Failed to create attachments for facet " + facet[i]); + System.out.println("Response 1: " + response1.get(0)); + System.out.println("Response 2: " + response2.get(0)); + allCreated = false; + break; + } + + String deleteResponse = + api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); + if (!"Deleted".equals(deleteResponse)) { + allCreated = false; + break; + } + } + + file1.delete(); + file2.delete(); + + if (allCreated) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } else { + System.out.println("Could not edit book: " + response); + } + + if (!testStatus) { + fail("Failed to upload multiple chapter facet entries, delete one per facet and save book"); + } + } + + @Test + @Order(24) + void testUpdateChapterEntityDraft() throws IOException { + System.out.println("Test (24): Update chapter in book draft with new facet content"); + boolean testStatus = false; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + // Use unique temp file name to avoid duplicates + File tempFile = File.createTempFile("test24_sample_", ".pdf"); + Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID5); + 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, bookEntityName, srvpath, bookID5); + if (response.equals("Entity in draft mode")) { + boolean allCreated = true; + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + String check = facetResponse.get(0); + if (!check.equals("Attachment created")) { + allCreated = false; + System.out.println( + "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + } + } + + if (allCreated) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } else { + System.out.println("Could not edit book: " + response); + } + + tempFile.delete(); + + if (!testStatus) { + fail("Failed to update chapter entity draft with new attachments"); + } + } + + @Test + @Order(25) + void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + throws IOException { + System.out.println( + "Test (25): Rename & Update secondary properties for multiple chapter attachments after book is saved"); + System.out.println("Creating book and chapter with multiple attachments"); + + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!"Could not create entity".equals(chapterResponse)) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create PDF attachments + postData.put("mimeType", "application/pdf"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + String[] pdfID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + pdfID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create TXT attachments + postData.put("mimeType", "application/txt"); + file = new File(classLoader.getResource("sample.txt").getFile()); + String[] txtID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + txtID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create EXE attachments + postData.put("mimeType", "application/exe"); + file = new File(classLoader.getResource("sample.exe").getFile()); + String[] exeID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + exeID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Save book first + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (!"Saved".equals(response)) { + fail("Could not save book initially"); + } + + // Edit book to update chapter attachments + response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (response.equals("Entity in draft mode")) { + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; + + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // Update PDF properties + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String renameResp = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + String upd3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + String upd4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + + if ("Renamed".equals(renameResp) + && "Updated".equals(upd1) + && "Updated".equals(upd2) + && "Updated".equals(upd3) + && "Updated".equals(upd4)) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // Update TXT properties (only boolean) + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + String upd = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + if ("Updated".equals(upd)) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + // Update EXE properties (dropdown and int) + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValueExe = integrationTestUtils.getDropDownValue(); + String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + for (int i = 0; i < facet.length; i++) { + RequestBody bodyDropdownExe = + RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + RequestBody bodyIntExe = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + + if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (response.equals("Saved")) { + System.out.println("Book saved"); + testStatus = true; + System.out.println("Renamed & updated Secondary properties for chapter attachments"); + } + } + } + api.deleteEntity(appUrl, bookEntityName, tempBookID); + } + } + if (!testStatus) { + fail("Could not update secondary property in chapter after book is saved"); + } + } + + @Test + @Order(26) + void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() + throws IOException { + System.out.println( + "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter facets before book is saved"); + System.out.println("Creating book and chapter"); + + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (!"Could not create entity".equals(response)) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!"Could not create entity".equals(chapterResponse)) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create PDF attachments + postData.put("mimeType", "application/pdf"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + String[] pdfID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + pdfID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create TXT attachments + postData.put("mimeType", "application/txt"); + file = new File(classLoader.getResource("sample.txt").getFile()); + String[] txtID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + txtID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create EXE attachments + postData.put("mimeType", "application/exe"); + file = new File(classLoader.getResource("sample.exe").getFile()); + String[] exeID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + exeID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; + + String name1 = "sample1234.pdf"; + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + + // Update PDF properties + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String renameResp = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + String upd3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + String upd4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + String updInvalid = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], invalidPropertyPDF); + + if ("Renamed".equals(renameResp) + && "Updated".equals(upd1) + && "Updated".equals(upd2) + && "Updated".equals(upd3) + && "Updated".equals(upd4) + && "Updated".equals(updInvalid)) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // Update TXT properties + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + String upd = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + if ("Updated".equals(upd)) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + // Update EXE properties + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValueExe = integrationTestUtils.getDropDownValue(); + String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + for (int i = 0; i < facet.length; i++) { + RequestBody bodyDropdownExe = + RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + RequestBody bodyIntExe = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + + if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + + // Verify PDF metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); + assertEquals(expectedNames[0], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertNull(metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } + + // Verify TXT metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); + assertEquals(expectedNames[1], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertTrue((Boolean) metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } + + // Verify EXE metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); + assertEquals(expectedNames[2], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); + assertEquals(1234, metadata.get("customProperty2")); + } + + // Parse JSON response and check for expected error messages + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved with expected invalid property errors"); + testStatus = true; + System.out.println( + "Rename & update unsuccessful for invalid properties and successful for valid attachments"); + } + } + } + } + + if (!testStatus) { + fail("Could not update secondary property before book is saved"); + } + } + + @Test + @Order(27) + void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + throws IOException { + System.out.println( + "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter attachments after book is saved"); + + // Reuse bookID5 and chapterID5 + System.out.println("Editing book with bookID5: " + bookID5); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + System.out.println("Edit entity response: " + response); + + if (response.equals("Entity in draft mode")) { + // Fetch existing attachments from the chapter + List> attachmentsMeta = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); + List> referencesMeta = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); + List> footnotesMeta = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); + + System.out.println("Attachments count: " + attachmentsMeta.size()); + System.out.println("References count: " + referencesMeta.size()); + System.out.println("Footnotes count: " + footnotesMeta.size()); + + if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) { + String[] pdfID = new String[facet.length]; + String[] txtID = new String[facet.length]; + String[] exeID = new String[facet.length]; + + pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); + pdfID[1] = (String) referencesMeta.get(0).get("ID"); + pdfID[2] = (String) footnotesMeta.get(0).get("ID"); + + txtID[0] = (String) attachmentsMeta.get(1).get("ID"); + txtID[1] = (String) referencesMeta.get(1).get("ID"); + txtID[2] = (String) footnotesMeta.get(1).get("ID"); + + exeID[0] = (String) attachmentsMeta.get(2).get("ID"); + exeID[1] = (String) referencesMeta.get(2).get("ID"); + exeID[2] = (String) footnotesMeta.get(2).get("ID"); + + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; + + String name1 = "sample.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // PDF + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); + + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); + + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); + + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); + + String updateSecondaryPropertyResponse5 = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated") + && updateSecondaryPropertyResponse5.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); + String updateSecondaryPropertyResponseTXT1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + Integer secondaryPropertyInt3 = 12; + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + for (int i = 0; i < facet.length; i++) { + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); + + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); + + if (updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // Note: Don't verify specific filenames since previous tests may have changed them + System.out.println("Save response: " + response); + + // Parse JSON response to check for invalid secondary property errors in all three tables + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved"); + testStatus = true; + System.out.println( + "Rename & update unsuccessful for invalid Secondary properties and successful for valid property attachments"); + } else { + System.out.println("Save response did not match expected: " + response); + } + } else { + System.out.println("Not enough attachments in facets - need at least 3 per facet"); + } + } + } else { + System.out.println( + "Could not edit book - it may be stuck in draft mode from a previous test"); + } + if (!testStatus) { + fail("Could not update secondary property before book is saved"); + } + } + + // Tests 28 and 29 removed - chapters have no attachment limit + + // Tests 28-29 skipped - chapters have no attachment limit + + @Test + @Order(30) + void testDiscardBookDraftWithoutChapterAttachments() { + System.out.println("Test (30) : Discard book draft without adding chapter attachments"); + Boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if (response.equals("Entity Draft Deleted")) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft with chapter was not discarded properly"); + } + } + + @Test + @Order(31) + void testDiscardBookDraftWithChapterAttachments() throws IOException { + System.out.println("Test (31): Discard book draft with chapter attachments"); + boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!"Could not create entity".equals(chapterResponse)) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = + new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + 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 < facet.length; i++) { + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + if ("Attachment created".equals(createResponse.get(0))) { + System.out.println("Attachment created in chapter facet: " + facet[i]); + } else { + System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + } + } + + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft with chapter attachments was not discarded properly"); + } + } + + // Tests 32-34 covered in tests 19, 23, 24 // Tests 37-41 skipped - copy with notes/secondary + // properties not applicable + + @Test + @Order(42) + void testCreateLinkSuccessInChapter() throws IOException { + System.out.println("Test (42): Create link in chapter"); + List attachments = new ArrayList<>(); + + // Create book and chapter for link testing + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create book"); + } + String createLinkBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + if (chapterResponse.equals("Could not create entity")) { + fail("Could not create chapter"); + } + String createLinkChapterID = chapterResponse; + + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + for (String facetName : facet) { + String createLinkResponse1 = + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + String createLinkResponse2 = + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", linkUrl); + if (!createLinkResponse1.equals("Link created successfully") + || !createLinkResponse2.equals("Link created successfully")) { + fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); + } + } + + String saveEntityResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save book"); + } + + for (String facetName : facet) { + attachments = + api + .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) + .stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String openAttachmentResponse; + for (String attachment : attachments) { + openAttachmentResponse = + api.openAttachment( + appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open created link in chapter facet : " + facetName); + } + } + } + api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + } + + @Test + @Order(43) + void testCreateLinkDifferentChapter() throws IOException { + System.out.println("Test (43): Create link with same name in different chapter"); + + // Create new book and chapter + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (response.equals("Could not edit entity")) { + fail("Could not create book"); + } + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (chapterResponse.equals("Could not create entity")) { + fail("Could not create chapter"); + } + String tempChapterID = chapterResponse; + + String linkName = "sample"; + String linkUrl = "https://example.com"; + for (String facetName : facet) { + String createResponse = + api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); + if (!createResponse.equals("Link created successfully")) { + fail("Could not create link in different chapter with same name"); + } + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (!response.equals("Saved")) { + fail("Could not save book"); + } + + response = api.deleteEntity(appUrl, bookEntityName, tempBookID); + if (!response.equals("Entity Deleted")) { + fail("Could not delete book"); + } + } + + @Test + @Order(44) + void testCreateLinkFailureInChapter() throws IOException { + System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if ("Could not create entity".equals(response)) { + fail("Could not create book"); + } + String createLinkBookID = response; + + response = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + if ("Could not create entity".equals(response)) { + fail("Could not create chapter"); + } + String createLinkChapterID = response; + + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + + ObjectMapper mapper = new ObjectMapper(); + + for (String facetName : facet) { + + // Create initial link for this facet first (so duplicate test works) + response = + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + if (!"Link created successfully".equals(response)) { + fail("Could not create initial link for facet: " + facetName); + } + + /* ---------- INVALID URL ---------- */ + try { + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); + fail("Expected invalid URL error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + assertEquals("400018", error.path("code").asText()); + assertTrue( + error.path("message").asText().contains("expected pattern"), + "Unexpected message: " + error.path("message").asText()); + } + + /* ---------- INVALID NAME ---------- */ + try { + api.createLink( + appUrl, + chapterEntityName, + facetName, + createLinkChapterID, + "sample//", + "https://example.com"); + fail("Expected invalid name error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); + + assertEquals("500", error.path("code").asText()); + assertTrue( + message.contains("contains unsupported characters") + && message.contains("Rename and try again"), + "Unexpected message: " + message); + } + + /* ---------- EMPTY NAME & URL ---------- */ + try { + api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); + fail("Expected missing value error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + assertEquals("409008", error.path("code").asText()); + assertEquals("Provide the missing value.", error.path("message").asText()); + } + + /* ---------- DUPLICATE NAME ---------- */ + try { + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + fail("Expected duplicate name error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + assertEquals("500", error.path("code").asText()); + assertEquals( + "An object named \"sample\" already exists. Rename the object and try again.", + error.path("message").asText()); + } + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + if (!"Saved".equals(response)) { + fail("Could not save book"); + } + + response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + if (!"Entity Deleted".equals(response)) { + fail("Could not delete book"); + } + } + + @Test + @Order(45) + void testCreateLinkNoSDMRolesInChapter() throws IOException { + System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); + + String createLinkBookNoRoles = + apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (createLinkBookNoRoles.equals("Could not edit entity")) { + fail("Could not create book"); + } + + String createLinkChapterNoRoles = + apiNoRoles.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); + if (createLinkChapterNoRoles.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + for (String facetName : facet) { + String linkName = "sample27"; + String linkUrl = "https://example.com"; + try { + apiNoRoles.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); + if (!response.equals("Saved")) { + fail("Could not save book"); + } + + response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); + if (!response.equals("Entity Deleted")) { + fail("Could not delete book"); + } + } + + @Test + @Order(46) + void testDeleteLinkInChapter() throws IOException { + System.out.println("Test (46): Delete link in chapter"); + List> attachments = new ArrayList<>(); + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create book"); + } + String deleteLinkBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); + if (chapterResponse.equals("Could not create entity")) { + fail("Could not create chapter"); + } + String deleteLinkChapterID = chapterResponse; + + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink( + appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for chapter facet : " + facetName); + } + } + + String saveEntityResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save book"); + } + + for (String facetName : facet) { + attachments.add( + api + .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + .stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } + + String editEntityResponse = + api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + int index = 0; + for (String facetName : facet) { + String deleteLinkResponse = + api.deleteAttachment( + appUrl, + chapterEntityName, + facetName, + deleteLinkChapterID, + attachments.get(index).get(0)); + System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + if (!deleteLinkResponse.equals("Deleted")) { + fail("Could not delete created link"); + } + index += 1; + } + + saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save book"); + } + + index = 0; + attachments.clear(); + for (String facetName : facet) { + attachments.add( + api + .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + .stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + System.out.println( + "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + if (attachments.get(index).size() != 0) { + fail("Link wasn't deleted"); + } + index += 1; + } + + response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); + if (!response.equals("Entity Deleted")) { + fail("Could not delete book"); + } + } + + @Test + @Order(35) + void testCopyAttachmentsToNewChapterInSameBook() throws IOException { + System.out.println( + "Test (35): Copy attachments from one chapter to another new chapter in the same book"); + + // Create source book and chapter with attachments + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Load original files for copying content + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(new ArrayList<>()); + } + + // Create attachments in all facets - each upload needs a unique filename + int fileCounter = 0; + for (int i = 0; i < facet.length; i++) { + boolean useTxt = (i == 1); // Use txt for references facet + postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + File originalFile = useTxt ? originalTxt : originalPdf; + String extension = useTxt ? ".txt" : ".pdf"; + + for (int j = 0; j < 2; j++) { // Create 2 attachments per facet + // Create unique temp file for EACH upload to avoid duplicate filename errors + fileCounter++; + File tempFile = + File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + System.out.println("Created attachment ID: " + createResponse.get(1)); + } else { + System.out.println("Failed to create attachment: " + createResponse.get(0)); + fail("Could not create attachment in facet: " + facet[i]); + } + } + } + + // Fetch object IDs from source attachments + List objectIds = new ArrayList<>(); + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + if (metadata.containsKey("objectId")) { + objectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + } + + // Save the source book + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Create target chapter in the SAME book + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source book for adding target chapter"); + } + + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (targetChapterID.equals("Could not create entity")) { + fail("Could not create target chapter in same book"); + } + + // Copy attachments to target chapter + int objectIdIndex = 0; + for (String facetName : facet) { + List facetObjectIds = + objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, facetObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + } + + // Fetch and wait for copied attachments + List> copiedMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + for (Map meta : copiedMetadata) { + String copiedId = (String) meta.get("ID"); + } + objectIdIndex += 2; + } + + // Save the book with new chapter + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book after copying attachments"); + } + + // Verify attachments were copied - read them + for (String facetName : facet) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.size() != 2) { + fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); + } + for (Map meta : targetMetadata) { + String attachmentId = (String) meta.get("ID"); + String readResponse = + api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment in facet: " + facetName); + } + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + } + + @Test + @Order(36) + void testCopyAttachmentsToChapterInDifferentBook() throws IOException { + System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); + + // Create Book1 with source chapter and attachments + copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create Book2 with target chapter + copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Load original files for copying content + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(new ArrayList<>()); + } + + // Create attachments in all facets of source chapter - each upload needs unique filename + int fileCounter = 0; + for (int i = 0; i < facet.length; i++) { + boolean useTxt = (i == 1); // Use txt for references facet + postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + File originalFile = useTxt ? originalTxt : originalPdf; + String extension = useTxt ? ".txt" : ".pdf"; + + for (int j = 0; j < 2; j++) { + // Create unique temp file for EACH upload to avoid duplicate filename errors + fileCounter++; + File tempFile = + File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + System.out.println("Created attachment ID: " + createResponse.get(1)); + } else { + System.out.println("Failed to create attachment: " + createResponse.get(0)); + fail("Could not create attachment in facet: " + facet[i]); + } + } + } + + // Fetch object IDs + sourceObjectIds.clear(); + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + } + + // Save Book1 + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Copy attachments from Book1's chapter to Book2's chapter + if (sourceObjectIds.size() == 6) { + int objectIdIndex = 0; + for (String facetName : facet) { + List facetObjectIds = + sourceObjectIds.subList( + objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, facetObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + } + + objectIdIndex += 2; + } + + // Save Book2 + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book after copying attachments"); + } + + // Verify attachments were copied + for (String facetName : facet) { + List> targetMetadata = + api.fetchEntityMetadata( + appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); + if (targetMetadata.size() != 2) { + fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); + } + for (Map meta : targetMetadata) { + String attachmentId = (String) meta.get("ID"); + String readResponse = + api.readAttachment( + appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, attachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment in facet: " + facetName); + } + } + } + + // Cleanup - delete both books after verification + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + } else { + fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); + } + } + + @Test + @Order(37) + void testCopyAttachmentsWithNotePreserved() throws IOException { + System.out.println("Test (37): Copy attachments with note field preserved"); + + // Create source book and chapter + copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create attachments with notes in source chapter + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] sourceAttachmentIds = new String[facet.length]; + String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); + + for (int i = 0; i < facet.length; i++) { + // Create unique temp file for each facet + File tempFile = + File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + sourceAttachmentIds[i] = createResponse.get(1); + + // Update note field using RequestBody + String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + String noteResponse = + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + noteBody); + if (!noteResponse.equals("Updated")) { + fail("Could not update note for attachment in facet: " + facet[i]); + } + System.out.println("Note updated for facet: " + facet[i]); + } + + // Save source book + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Verify notes were saved in source + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + if (!testNote.equals(metadata.get("note"))) { + fail("Note not saved correctly in source for facet: " + facet[i]); + } + } + + // Create target book and chapter + copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Get object IDs and copy attachments + for (int i = 0; i < facet.length; i++) { + Map sourceMetadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + String objectId = sourceMetadata.get("objectId").toString(); + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + System.out.println("Attachment copied to facet: " + facet[i]); + } + + // Save target book + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify notes were preserved in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); + if (targetMetadata.isEmpty()) { + fail("No attachments found in target facet: " + facet[i]); + } + Map copiedAttachment = targetMetadata.get(0); + String copiedNote = (String) copiedAttachment.get("note"); + if (!testNote.equals(copiedNote)) { + fail( + "Note not preserved after copy in facet: " + + facet[i] + + ". Expected: " + + testNote + + ", Got: " + + copiedNote); + } + System.out.println("Note preserved in target facet: " + facet[i]); + } + + System.out.println("Test 37 passed - notes preserved during copy"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + copyAttachmentSourceBook = null; + copyAttachmentTargetBook = null; + } + + @Test + @Order(38) + void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { + System.out.println("Test (38): Copy attachments with secondary properties preserved"); + + // Use entities from test 37 or create new ones if needed + boolean sourceBookJustCreated = false; + boolean targetBookJustCreated = false; + + if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + copyAttachmentSourceBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + sourceBookJustCreated = true; + } + + if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + copyAttachmentTargetBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + targetBookJustCreated = true; + } + + // If source book was just created, save it first before we can edit it + if (sourceBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created source book"); + } + } + + // If target book was just created, save it first + if (targetBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created target book"); + } + } + + // Edit source book + String editResponse = + api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source book"); + } + + // Create new attachments with secondary properties + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] sourceAttachmentIds = new String[facet.length]; + Boolean testBooleanProp = true; + Integer testIntegerProp = 12345; + + for (int i = 0; i < facet.length; i++) { + // Create unique temp file + File tempFile = + File.createTempFile( + "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + sourceAttachmentIds[i] = createResponse.get(1); + + // Update secondary properties using RequestBody (customProperty6 - Boolean, customProperty2 - + // Integer) + String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + String boolResponse = + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + boolBody); + if (!boolResponse.equals("Updated")) { + System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); + } + + String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + String intResponse = + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + intBody); + if (!intResponse.equals("Updated")) { + System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); + } + + System.out.println("Secondary properties updated for facet: " + facet[i]); + } + + // Save source book + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Edit target book + editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + // Copy attachments to target + for (int i = 0; i < facet.length; i++) { + Map sourceMetadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + String objectId = sourceMetadata.get("objectId").toString(); + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); + } + + // Save target book + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify secondary properties were preserved in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); + + // Find the attachment we just copied (most recent one) + boolean found = false; + for (Map attachment : targetMetadata) { + Object boolProp = attachment.get("customProperty6"); + Object intProp = attachment.get("customProperty2"); + + if (boolProp != null && intProp != null) { + if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { + found = true; + System.out.println("Secondary properties preserved in target facet: " + facet[i]); + break; + } + } + } + if (!found) { + System.out.println( + "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); + } + } + + System.out.println("Test 38 passed - secondary properties checked during copy"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + copyAttachmentSourceBook = null; + copyAttachmentTargetBook = null; + } + + @Test + @Order(39) + void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { + System.out.println( + "Test (39): Copy attachments with both note and secondary properties preserved"); + + // Use entities from previous tests or create new ones + boolean sourceBookJustCreated = false; + boolean targetBookJustCreated = false; + + if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + copyAttachmentSourceBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + sourceBookJustCreated = true; + } + + if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + copyAttachmentTargetBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + targetBookJustCreated = true; + } + + // If source book was just created, save it first before we can edit it + if (sourceBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created source book"); + } + } + + // If target book was just created, save it first + if (targetBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created target book"); + } + } + + // Edit source book + String editResponse = + api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source book"); + } + + // Create new attachments with both note and secondary properties + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] sourceAttachmentIds = new String[facet.length]; + String testNote = "Combined test note - " + System.currentTimeMillis(); + Boolean testBooleanProp = true; + Integer testIntegerProp = 99999; + + for (int i = 0; i < facet.length; i++) { + // Create unique temp file + File tempFile = + File.createTempFile( + "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + sourceAttachmentIds[i] = createResponse.get(1); + + // Update note using RequestBody + String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + noteBody); + + // Update secondary properties using RequestBody + String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + boolBody); + + String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + intBody); + + System.out.println("Note and secondary properties updated for facet: " + facet[i]); + } + + // Save source book + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Edit target book + editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + // Copy attachments to target + for (int i = 0; i < facet.length; i++) { + Map sourceMetadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + String objectId = sourceMetadata.get("objectId").toString(); + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + System.out.println("Attachment with note and properties copied to facet: " + facet[i]); + } + + // Save target book + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify note and secondary properties were preserved in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); + + boolean noteFound = false; + boolean propsFound = false; + + for (Map attachment : targetMetadata) { + String copiedNote = (String) attachment.get("note"); + Object boolProp = attachment.get("customProperty6"); + Object intProp = attachment.get("customProperty2"); + + if (testNote.equals(copiedNote)) { + noteFound = true; + System.out.println("Note preserved in target facet: " + facet[i]); + } + + if (boolProp != null && intProp != null) { + if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { + propsFound = true; + System.out.println("Secondary properties preserved in target facet: " + facet[i]); + } + } + } + + if (!noteFound) { + System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); + } + if (!propsFound) { + System.out.println( + "Warning: Secondary properties may not be preserved in facet: " + facet[i]); + } + } + + // Cleanup - delete both books + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + + // Reset static variables + copyAttachmentSourceBook = null; + copyAttachmentTargetBook = null; + copyAttachmentSourceChapter = null; + copyAttachmentTargetChapter = null; + + System.out.println("Test 39 passed - both note and secondary properties checked during copy"); + } + + @Test + @Order(40) + void testCopyAttachmentsWithInvalidObjectId() throws IOException { + System.out.println("Test (40): Copy attachments with invalid object ID should fail"); + + // Create independent test entities (don't rely on previous tests) + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create test book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create test chapter"); + } + + // Save the book first so it's not in draft mode + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save test book"); + } + + // Now edit it to test copy with invalid object IDs + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit test book"); + } + + // Try to copy with invalid object ID + for (String facetName : facet) { + try { + List invalidObjectIds = new ArrayList<>(); + invalidObjectIds.add("invalidObjectId123"); + invalidObjectIds.add("anotherInvalidId456"); + api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, invalidObjectIds); + fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); + } catch (IOException e) { + // Expected - copy should fail with invalid object ID + System.out.println( + "Expected error received for invalid object ID in facet " + + facetName + + ": " + + e.getMessage()); + } + } + + // Save and cleanup + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + + // Also cleanup test 36 entities if they exist + if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { + try { + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + } catch (Exception e) { + // Ignore - may already be deleted + } + } + if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { + try { + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + } catch (Exception e) { + // Ignore - may already be deleted + } + } + } + + @Test + @Order(41) + void testCopyAttachmentsToExistingChapter() throws IOException { + System.out.println( + "Test (41): Copy attachments to an existing chapter that already has attachments"); + + // Create Book1 with source chapter + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create Book2 with target chapter that has existing attachments + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (targetBookID.equals("Could not create entity")) { + fail("Could not create target book"); + } + + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + if (targetChapterID.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Create temp files with unique names to avoid duplicate filename errors + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test41_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create attachment in source chapter + List sourceObjIds = new ArrayList<>(); + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create source attachment"); + } + String attachmentId = createResponse.get(1); + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + sourceObjIds.add(metadata.get("objectId").toString()); + } + + // Create existing attachment in target chapter + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", targetChapterID); + postData.put("mimeType", "text/plain"); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create existing target attachment"); + } + String attachmentId = createResponse.get(1); + } + + // Save both books + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Edit target book and copy attachments + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + // Copy from source to target (target already has 1 attachment per facet) + for (int i = 0; i < facet.length; i++) { + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjIds.get(i)); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, objectIdsToCopy); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + } + + // Save target book + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) + for (String facetName : facet) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.size() != 2) { + fail( + "Expected 2 attachments in facet " + + facetName + + " (1 existing + 1 copied), found " + + targetMetadata.size()); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + // ============= LINK RENAME TESTS (47-49) ============= + + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (47): Rename link in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links in all facets + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit and rename links + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + String renameResponse = + api.renameAttachment( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); + if (!renameResponse.equals("Renamed")) { + fail("Could not rename link in facet: " + facetName); + } + } + + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book after renaming links"); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(48) + void testRenameLinkDuplicate() throws IOException { + System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create two links in all facets + for (String facetName : facet) { + String createLinkResponse1 = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "link1", + "https://www.example1.com"); + String createLinkResponse2 = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "link2", + "https://www.example2.com"); + if (!createLinkResponse1.equals("Link created successfully") + || !createLinkResponse2.equals("Link created successfully")) { + fail("Could not create links in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit and try to rename link2 to link1 (duplicate) + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.size() < 2) { + fail("Expected 2 links in facet: " + facetName); + } + + // Find link2 and rename to link1 + for (Map attachment : attachments) { + if ("link2".equals(attachment.get("fileName"))) { + String linkId = (String) attachment.get("ID"); + api.renameAttachment( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); + break; + } + } + } + + // Save should fail with duplicate error + String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + ObjectMapper mapper = new ObjectMapper(); + try { + JsonNode errorJson = mapper.readTree(saveError); + String errorMessage = errorJson.path("error").path("message").asText(); + if (!errorMessage.contains("already exists")) { + fail("Expected duplicate error but got: " + saveError); + } + } catch (Exception e) { + if (!saveError.contains("already exists")) { + fail("Expected duplicate error but got: " + saveError); + } + } + + // Cleanup + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(49) + void testRenameLinkUnsupportedCharacters() throws IOException { + System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links in all facets + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit and rename with unsupported characters + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + api.renameAttachment( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); + } + + // Save should fail with unsupported characters error + String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveError.contains("unsupported characters")) { + fail("Expected unsupported characters error but got: " + saveError); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + // ============= LINK EDIT TESTS (50-53) ============= + + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (50): Edit existing link URL in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links in all facets + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit links + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + String editLinkResponse = + api.editLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + linkId, + "https://www.editedexample.com"); + if (!editLinkResponse.equals("Link edited successfully")) { + fail("Could not edit link in facet: " + facetName); + } + } + + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book after editing links"); + } + + // Verify links open successfully + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + for (Map attachment : attachments) { + String linkId = (String) attachment.get("ID"); + String openResponse = + api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); + if (!openResponse.equals("Attachment opened successfully")) { + fail("Could not open edited link in facet: " + facetName); + } + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (51): Edit link with invalid URL fails in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + try { + api.editLink( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://editedexample"); + fail("Edit link should have failed with invalid URL in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected error received for invalid URL in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(52) + void testEditLinkFailureEmptyURL() throws IOException { + System.out.println("Test (52): Edit link with empty URL fails in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + try { + api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); + fail("Edit link should have failed with empty URL in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected error received for empty URL in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + try { + apiNoRoles.editLink( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://www.edited.com"); + fail("Edit link should have failed without SDM roles in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected permission error received in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + // ============= COPY LINK TESTS (54-58) ============= + + @Test + @Order(54) + void testCopyLinkSuccessNewChapter() throws IOException { + System.out.println("Test (54): Copy link from one chapter to another new chapter"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + if (sourceChapterID.equals("Could not create entity") + || targetChapterID.equals("Could not create entity")) { + fail("Could not create chapters"); + } + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + // Create links in source chapter + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch object IDs + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + // Copy links to target chapter + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link for facet " + facetName + ": " + copyResponse); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify link type and URL + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.isEmpty()) { + fail("No links found in target chapter for facet: " + facetName); + } + + Map copiedLink = targetMetadata.get(0); + String receivedUrl = (String) copiedLink.get("linkUrl"); + assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); + + objectIdIndex++; + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(55) + void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { + System.out.println("Test (55): Copy invalid link object ID to chapter fails"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + for (String facetName : facet) { + try { + List invalidObjectIds = new ArrayList<>(); + invalidObjectIds.add("incorrectObjectId"); + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, invalidObjectIds); + fail("Copy should have thrown error for invalid object ID in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected error received for invalid object ID in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(56) + void testCopyLinkToExistingChapter() throws IOException { + System.out.println("Test (56): Copy link to existing chapter that has attachments"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + // Create links in source chapter + for (int i = 0; i < facet.length; i++) { + String linkName = "sourceLink" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in source chapter for facet: " + facet[i]); + } + } + + // Create existing links in target chapter + for (int i = 0; i < facet.length; i++) { + String linkName = "existingLink" + i; + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facet[i], + targetChapterID, + linkName, + "https://www.existing.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create existing link in target chapter for facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch source object IDs + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + // Copy links to target chapter + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link for facet " + facetName); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify target has 2 links (existing + copied) + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.size() != 2) { + fail( + "Expected 2 links in target chapter facet " + + facetName + + ", found " + + targetMetadata.size()); + } + + objectIdIndex++; + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(57) + void testCopyLinkNoSDMRoles() throws IOException { + System.out.println("Test (57): Copy link fails due to no SDM roles"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch object IDs + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + // Try to copy with no SDM roles + int objectIdIndex = 0; + for (String facetName : facet) { + try { + // Use normal api to put book in draft mode + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // Use apiNoRoles to attempt copy (should fail) + apiNoRoles.copyAttachment( + appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + fail("Copy should have failed without SDM roles in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected permission error in facet " + facetName); + // Discard draft to clean up for next iteration + api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); + } + objectIdIndex++; + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(58) + void testCopyLinkFromDraftChapter() throws IOException { + System.out.println("Test (58): Copy link from draft chapter to another chapter"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + // Create links in source chapter (NOT saved yet - draft mode) + for (int i = 0; i < facet.length; i++) { + String linkName = "draftLink" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + + // Save target book only + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch object IDs from draft + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + if (linkObjectIds.size() != facet.length) { + fail("Could not fetch all object IDs from draft"); + } + + // Copy links from draft to target + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link from draft for facet " + facetName); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify link was copied + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.isEmpty()) { + fail("No links found in target chapter for facet: " + facetName); + } + + objectIdIndex++; + } + + // Cleanup + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= + + @Test + @Order(59) + void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { + System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + if (sourceChapterID.equals("Could not create entity") + || targetChapterID.equals("Could not create entity")) { + fail("Could not create chapters"); + } + + // Create temp files with unique names + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test59_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List sourceObjectIds = new ArrayList<>(); + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(new ArrayList<>()); + } + + // Create attachments in source chapter (still in draft) + for (int i = 0; i < facet.length; i++) { + postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); + File file = i == 1 ? tempTxt : tempPdf; + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment in facet: " + facet[i]); + } + } + + // Fetch object IDs from draft + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + } + + // Save target book only + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Copy attachments from draft to target + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment from draft for facet " + facetName); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify attachment was copied + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.isEmpty()) { + fail("No attachments found in target chapter for facet: " + facetName); + } + + // Read attachment to verify + String attachmentId = (String) targetMetadata.get(0).get("ID"); + String readResponse = + api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment in facet: " + facetName); + } + + objectIdIndex++; + } + + // Cleanup + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + // ============= CHANGELOG TESTS (60-64) ============= + + @Test + @Order(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println("Test (60): View changelog for newly created attachment in chapter"); + + for (int i = 0; i < facet.length; i++) { + String facetName = facet[i]; + + // Create book and chapter + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = + File.createTempFile( + "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } + + String attachmentId = createResponse.get(1); + + // Fetch changelog + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); + + // Cleanup + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + } + } + + @Test + @Order(61) + void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { + System.out.println("Test (61): Changelog after modifying note and custom property in chapter"); + + for (int i = 0; i < facet.length; i++) { + String facetName = facet[i]; + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = + File.createTempFile( + "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Update note + String notesValue = "Test note for changelog verification"; + RequestBody updateNotesBody = + RequestBody.create( + MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); + + // Update custom property + RequestBody bodyInt = + RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 12345}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); + + // Save + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit to fetch changelog + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + // Fetch changelog + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + int numItems = (int) changelogResponse.get("numItems"); + assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + } + + @Test + @Order(62) + void testChangelogAfterRenamingAttachment() throws IOException { + System.out.println("Test (62): Changelog after renaming attachment in chapter"); + + for (int i = 0; i < facet.length; i++) { + String facetName = facet[i]; + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = + File.createTempFile( + "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Rename attachment + String renameResponse = + api.renameAttachment( + appUrl, + chapterEntityName, + facetName, + testChapterID, + attachmentId, + "renamed_file.txt"); + if (!renameResponse.equals("Renamed")) { + fail("Could not rename attachment"); + } + + // Save + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit to fetch changelog + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + // Fetch changelog + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + int numItems = (int) changelogResponse.get("numItems"); + assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + } + + @Test + @Order(63) + void testChangelogForCopiedAttachment() throws IOException { + System.out.println("Test (63): Changelog for copied attachment in chapter"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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 in source + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Save both books + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Get object ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + + // Copy to target + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment"); + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch changelog for copied attachment + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); + + editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + Map changelogResponse = + api.fetchChangelog( + appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + int numItems = (int) changelogResponse.get("numItems"); + assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(64) + void testChangelogForNewChapter() throws IOException { + System.out.println("Test (64): Changelog for attachment in newly created chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Fetch changelog before saving + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + assertEquals( + 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); + + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); + + // Cleanup + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + } + + // ============= MOVE ATTACHMENT TESTS (65-75) ============= + + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println("Test (65): Move attachments from source chapter to target chapter"); + + for (int i = 0; i < facet.length; i++) { + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create temp files + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempPdf.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), + tempTxt.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List sourceAttachmentIds = new ArrayList<>(); + File[] files = {tempPdf, tempTxt}; + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source chapter"); + } + } + + // Save source book + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Get object IDs and folder ID + List moveObjectIds = new ArrayList<>(); + String sourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (sourceFolderId == null && metadata.containsKey("folderId")) { + sourceFolderId = metadata.get("folderId").toString(); + } + } + } + + // Create target book and chapter + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (targetBookID.equals("Could not create entity")) { + fail("Could not create target book"); + } + + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + if (targetChapterID.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Save target book before moving attachments (moveAttachments requires Active entity) + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book before move"); + } + + // Move attachments to Active entity + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[i], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } + + // Verify + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + assertEquals( + sourceAttachmentIds.size(), + targetMetadata.size(), + "Target should have all attachments after move"); + + List> sourceMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, targetBookID); + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + } + } + + @Test + @Order(66) + void testMoveAttachmentsToChapterWithDuplicate() throws IOException { + System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Create attachment in source with specific name + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, originalPdf); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create source attachment"); + } + String sourceAttachmentId = createResponse.get(1); + + // Create attachment in target with SAME name (duplicate) + postData.put("up__ID", targetChapterID); + createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, originalPdf); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create target attachment"); + } + + // Save both + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Get source object ID and folder ID + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, sourceAttachmentId); + String objectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move to saved target + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Move should handle duplicate - attachment stays in source + + // Verify source still has attachment (duplicate not moved) + List> sourceMetadataAfter = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertTrue( + sourceMetadataAfter.size() >= 1, + "Source should still have attachment when duplicate exists in target"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(67) + void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { + System.out.println("Test (67): Move attachments with notes and secondary properties"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + // Add note and secondary property + String testNote = "Test note for move"; + RequestBody noteBody = + RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + "\"}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); + + RequestBody propBody = + RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); + + // Save source + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get object ID and folder ID + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // Create target + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null"); + } + + // Verify note was preserved + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + if (!targetMetadata.isEmpty()) { + String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); + Map movedMetadata = + api.fetchMetadata( + appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); + + // Note should be preserved + if (movedMetadata.containsKey("note")) { + assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(68) + void testMoveAttachmentsPartialFailure() throws IOException { + System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get real object ID and folder ID + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String realObjectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // Create target + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Try to move with mix of valid and invalid object IDs + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(realObjectId); + moveObjectIds.add("invalidObjectId123"); + + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Should handle partial failure + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(69) + void testMoveAttachmentsEmptyList() throws IOException { + System.out.println("Test (69): Move attachments with empty object ID list"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Try to move with empty list + List emptyObjectIds = new ArrayList<>(); + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + + try { + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + "someFolderId", + emptyObjectIds, + targetFacet, + sourceFacet); + // Should either fail or do nothing + } catch (Exception e) { + System.out.println("Expected: Move with empty list handled: " + e.getMessage()); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(70) + void testMoveAttachmentsToSameChapter() throws IOException { + System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + 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"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Get object ID and folder ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String folderId = metadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move to same chapter + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + testChapterID, + folderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Should handle gracefully - attachment stays in place + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Verify attachment still exists + List> metadataAfter = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); + assertEquals(1, metadataAfter.size(), "Attachment should still exist"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(71) + void testMoveAttachmentsBetweenFacets() throws IOException { + System.out.println("Test (71): Move attachments between different facets in chapters"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create in attachments facet + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Get object ID and folder ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String sourceFolderId = metadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move from attachments to references facet + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[1], // references facet + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify moved to different facet + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); + assertTrue( + targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(72) + void testMoveMultipleAttachments() throws IOException { + System.out.println("Test (72): Move multiple attachments at once between chapters"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // Create multiple temp files + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test72_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List sourceAttachmentIds = new ArrayList<>(); + File[] files = {tempPdf, tempTxt}; + String[] mimeTypes = {"application/pdf", "text/plain"}; + + for (int i = 0; i < files.length; i++) { + postData.put("mimeType", mimeTypes[i]); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get object IDs + List moveObjectIds = new ArrayList<>(); + String sourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + moveObjectIds.add(metadata.get("objectId").toString()); + if (sourceFolderId == null) { + sourceFolderId = metadata.get("folderId").toString(); + } + } + + // Create target + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Move all at once + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify all moved + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + assertEquals( + sourceAttachmentIds.size(), + targetMetadata.size(), + "All attachments should be moved to target"); + + List> sourceMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(73) + void testMoveAttachmentsAllFacets() throws IOException { + System.out.println("Test (73): Move attachments from all facets between chapters"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Create attachment in each facet + for (int i = 0; i < facet.length; i++) { + String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); + File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Move from each facet + for (int i = 0; i < facet.length; i++) { + List> sourceMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + if (sourceMetadata.isEmpty()) { + continue; + } + + String attachmentId = (String) sourceMetadata.get(0).get("ID"); + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String sourceFolderId = metadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + api.moveAttachment( + appUrl, + chapterEntityName, + facet[i], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + } + + // Verify all facets have attachments in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + facet[i]); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(74) + void testChainMoveAttachments() throws IOException { + System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || target1BookID.equals("Could not create entity") + || target2BookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String target1ChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); + String target2ChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); + + // First move: Source -> Target1 + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move to target1 + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + target1ChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify in target1 + List> target1Metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); + + // Second move: Target1 -> Target2 + String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); + Map target1AttMetadata = + api.fetchMetadata( + appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); + String target1ObjectId = target1AttMetadata.get("objectId").toString(); + String target1FolderId = target1AttMetadata.get("folderId").toString(); + + moveObjectIds.clear(); + moveObjectIds.add(target1ObjectId); + + // Move to target2 + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + target2ChapterID, + target1FolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify final state + List> target2Metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); + assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); + + target1Metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); + + List> sourceFinalMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, target1BookID); + api.deleteEntity(appUrl, bookEntityName, target2BookID); + } + + @Test + @Order(75) + void testMoveAttachmentsWithoutSDMRole() throws IOException { + System.out.println("Test (75): Move attachments fails without SDM role"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = + File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get object ID and folder ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String sourceFolderId = metadata.get("folderId").toString(); + + // Create target with no role user + String targetBookID = + apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (targetBookID.equals("Could not create entity")) { + fail("Could not create target book"); + } + + String targetChapterID = + apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + boolean moveFailed = false; + String errorMessage = null; + + try { + Map moveResult = + apiNoRoles.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null || moveResult.containsKey("error")) { + moveFailed = true; + errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; + } + } catch (Exception e) { + moveFailed = true; + errorMessage = e.getMessage(); + } + + assertTrue(moveFailed, "Move should fail without SDM role"); + System.out.println("Move correctly failed without SDM role: " + errorMessage); + + // Verify source still has attachment + List> sourceMetadataAfter = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(76) + void testRenameChapterAttachmentWithExtensionChange() throws IOException { + System.out.println( + "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); + + // Step 1: Create a new book and chapter + String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (newBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + String newChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + if (newChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not create chapter"); + } + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book: " + saveResponse); + } + + // Step 2: Upload a PDF attachment to each facet in the chapter + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", newChapterID); + 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, bookEntityName, srvpath, newBookID); + if (!"Entity in draft mode".equals(editResponse)) { + fail("Could not put book in draft mode for PDF upload"); + } + + String[] facetAttachmentIDs = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + facetAttachmentIDs[i] = + CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + if (facetAttachmentIDs[i] == null) { + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + } + } + + // Step 3: Save the book + String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + if (!savedAfterUpload.equals("Saved")) { + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not save book after PDF upload: " + savedAfterUpload); + } + + // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt + for (int i = 0; i < facet.length; i++) { + String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + if (!"Entity in draft mode".equals(editDraftResponse)) { + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not put book in draft mode for rename on facet: " + facet[i]); + } + + String renameResponse = + api.renameAttachment( + appUrl, + chapterEntityName, + facet[i], + newChapterID, + facetAttachmentIDs[i], + "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + } + + // Step 6: Save and validate the extension change warning message + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } + + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + } + + // Clean up + api.deleteEntity(appUrl, bookEntityName, newBookID); + } + + @Test + @Order(77) + void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { + System.out.println( + "Test (77) : Upload chapter attachment in draft, rename changing extension before save across all facets - should return extension change warning"); + + for (int i = 0; i < facet.length; i++) { + // Step 1: Create a new book and chapter draft (do NOT save) + String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (newBookID.equals("Could not create entity")) { + fail("Could not create book for facet: " + facet[i]); + } + String newChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + if (newChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not create chapter for facet: " + facet[i]); + } + + // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft (unsaved) + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", newChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String facetAttachmentID = + CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + if (facetAttachmentID == null) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + } + + // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not saved + String renameResponse = + api.renameAttachment( + appUrl, + chapterEntityName, + facet[i], + newChapterID, + facetAttachmentID, + "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + } + + // Step 4: Save the book — should receive extension change warning, not "Saved" + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } + + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + + // Clean up + api.deleteEntity(appUrl, bookEntityName, newBookID); + } + } @Test @Order(78) 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 0b98fc280..b231aa985 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 @@ -383,6891 +383,6678 @@ void testUploadSingleTXT() throws IOException { } } - // @Test - // @Order(5) - // void testUploadSingleEXE() throws IOException { - // System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); - // Boolean testStatus = false; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.exe").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables - // 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, entityID); - // if (response.equals("Entity in draft mode")) { - // // Creation of attachment, reference and footnote - // for (int i = 0; i < facet.length; i++) { - // ID3[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID, postData, file); - // } - // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); - // } - // if (!testStatus) { - // fail("Could not upload sample.exe " + response); - // } - // } + @Test + @Order(5) + void testUploadSingleEXE() throws IOException { + System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); + Boolean testStatus = false; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.exe").getFile()); - // @Test - // @Order(6) - // void testUploadPDFDuplicate() throws IOException { - // System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // 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, entityID); - // if ("Entity in draft mode".equals(response)) { - // Boolean allFacetsFailedCorrectly = true; - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, - // file); - // allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); - // } - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!allFacetsFailedCorrectly) { - // fail("One or more facets were incorrectly accepted as new."); - // } - // } else { - // fail("Entity could not be edited to draft mode."); - // } - // } + Map postData = new HashMap<>(); + postData.put("up__ID", entityID); + postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // @Test - // @Order(7) - // void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { - // System.out.println( - // "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and - // footnote"); - // Boolean testStatus = false; - // // Create a new entity draft - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID2 = response; - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - - // if ("Saved".equals(response)) { - // response = api.checkEntity(appUrl, entityName, entityID2); - // if ("Entity exists".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Could not create entity"); - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Entity in draft mode")) { + // Creation of attachment, reference and footnote + for (int i = 0; i < facet.length; i++) { + ID3[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID, postData, file); + } + testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); + } + if (!testStatus) { + fail("Could not upload sample.exe " + response); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID2); - // 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 draft mode - // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - // if ("Entity in draft mode".equals(response)) { - // // Create attachment, reference, and footnote - // for (int i = 0; i < facet.length; i++) { - // ID4[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID2, postData, file); - // } - // // Verify and save - // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); - // } - // if (!testStatus) { - // fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); - // } - // } + @Test + @Order(6) + void testUploadPDFDuplicate() throws IOException { + System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID); + 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(8) - // void testRenameEntities() { - // System.out.println("Test (8) : Rename single attachment, reference, and footnote"); - // Boolean testStatus = true; - - // try { - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - - // if ("Entity in draft mode".equals(response)) { - // String[] name = {"sample123", "reference123", "footnote123"}; - // for (int i = 0; i < facet.length; i++) { - // // Read the facet to ensure it exists - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], - // name[i]); - // if (!"Renamed".equals(response)) { - // testStatus = false; - // System.out.println(facet[i] + " was not renamed: " + response); - // } - // } - // // Save entity draft if everything is renamed - // if (testStatus) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!"Saved".equals(response)) { - // testStatus = false; - // System.out.println("Entity draft was not saved: " + response); - // } - // } else { - // // Attempt save despite potential rename failures - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } else { - // testStatus = false; - // System.out.println("Entity was not put into draft mode: " + response); - // } - // } catch (Exception e) { - // testStatus = false; - // System.out.println("Exception during renaming entities: " + e.getMessage()); - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Entity in draft mode".equals(response)) { + Boolean allFacetsFailedCorrectly = true; + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, file); + allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); + } + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (!allFacetsFailedCorrectly) { + fail("One or more facets were incorrectly accepted as new."); + } + } else { + fail("Entity could not be edited to draft mode."); + } + } - // if (!testStatus) { - // fail("There was an error during the rename test process."); - // } - // } + @Test + @Order(7) + void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { + System.out.println( + "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and footnote"); + Boolean testStatus = false; + // Create a new entity draft + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID2 = response; + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // @Test - // @Order(9) - // void testCreateEntitiesWithUnsupportedCharacter() throws IOException { - // System.out.println("Test (9): Create attachments with unsupported characters"); - // boolean testStatus = false; + if ("Saved".equals(response)) { + response = api.checkEntity(appUrl, entityName, entityID2); + if ("Entity exists".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Could not create entity"); + } - // 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("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, entityID); - // if (!"Entity in draft mode".equals(response)) { - // fail("Entity not in draft mode: " + response); - // return; - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", entityID); - // List createResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, - // tempFile); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID2); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // String check = createResponse.get(0); - // if (!"Attachment created".equals(check)) { - // System.out.println("Failed to create attachment for facet: " + facet[i]); - // continue; - // } + // Edit entity to draft mode + response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + if ("Entity in draft mode".equals(response)) { + // Create attachment, reference, and footnote + for (int i = 0; i < facet.length; i++) { + ID4[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID2, postData, file); + } + // Verify and save + testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); + } + if (!testStatus) { + fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); + } + } - // String restrictedName = "a/\\bc.pdf"; - // response = - // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); - // } + @Test + @Order(8) + void testRenameEntities() { + System.out.println("Test (8) : Rename single attachment, reference, and footnote"); + Boolean testStatus = true; - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" - // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // if (response.equals(expected)) { - // api.deleteEntityDraft(appUrl, entityName, entityID); - // testStatus = true; - // } + try { + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!testStatus) { - // fail("Facets renamed with restricted characters were not correctly rejected."); - // } - // } + if ("Entity in draft mode".equals(response)) { + String[] name = {"sample123", "reference123", "footnote123"}; + for (int i = 0; i < facet.length; i++) { + // Read the facet to ensure it exists + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); + if (!"Renamed".equals(response)) { + testStatus = false; + System.out.println(facet[i] + " was not renamed: " + response); + } + } + // Save entity draft if everything is renamed + if (testStatus) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Saved".equals(response)) { + testStatus = false; + System.out.println("Entity draft was not saved: " + response); + } + } else { + // Attempt save despite potential rename failures + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } else { + testStatus = false; + System.out.println("Entity was not put into draft mode: " + response); + } + } catch (Exception e) { + testStatus = false; + System.out.println("Exception during renaming entities: " + e.getMessage()); + } - // @Test - // @Order(10) - // void testRenameEntitiesWithUnsupportedCharacter() { - // System.out.println("Test (10) : Rename attachments with unsupported characters"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - // if (response.equals("Renamed")) counter++; - // } - // if (counter >= 2) { - // counter = -1; // Reset counter for the next check - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains - // unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" - // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // if (response.equals(expected)) { - // for (int i = 0; i < facet.length; i++) { - // response = - // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], - // "sample.pdf"); - // } - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment was renamed with unsupported characters"); - // } - // } + if (!testStatus) { + fail("There was an error during the rename test process."); + } + } - // @Test - // @Order(11) - // void testRenameMultipleEntityComponents() { - // System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); - // boolean testStatus = true; - - // String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!"Entity in draft mode".equals(draftResponse)) { - // fail("Entity is not in draft mode."); - // return; - // } - // String[] name = {"sample1234", "reference1234", "footnote1234"}; - // String[] name2 = {"sample12345", "reference12345", "footnote12345"}; - // for (int i = 0; i < facet.length; i++) { - // // Read the facet to ensure it exists - // testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); - // testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); - // } - // // Save the draft if all renames succeeded - // if (testStatus) { - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!"Saved".equals(saveResponse)) { - // fail("Entity draft was not saved after renaming."); - // } - // } else { - // // Save draft even if renaming failed to preserve state - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // fail("One or more components were not renamed."); - // } - // } + @Test + @Order(9) + void testCreateEntitiesWithUnsupportedCharacter() throws IOException { + System.out.println("Test (9): Create attachments with unsupported characters"); + boolean testStatus = false; - // @Test - // @Order(12) - // void testRenameSingleDuplicate() { - // System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String[] name = {"sample1234", "reference1234", "footnote1234"}; - // String[] name2 = {"sample123456", "reference123456", "footnote123456"}; - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - // if (response.equals("Renamed")) counter++; - // } - // if (counter >= 2) { - // counter = -1; // Reset counter for the next check - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // String.format( - // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already - // exists. Rename the object and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named - // \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An - // object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: - // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", - // name[1], name[0], name[2]); - // if (response.equals(expected)) { - // for (int i = 0; i < facet.length; i++) { - // // Attempt to rename again with a different name - // response = - // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); - // if (response.equals("Renamed")) counter++; - // } - // } - // if (counter >= 2) { - // // If all renames were successful, save the draft - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } else { - // testStatus = false; - // fail("Attachment was renamed"); - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // @Test - // @Order(13) - // void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { - // System.out.println( - // "Test (13) : Rename multiple files out of which one file name contains unsupported - // characters"); - // boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String[] names = {"summary_1234", "reference_4567", "note/invalid"}; - - // if (response.equals("Entity in draft mode")) { - // int successCount = 0; - // for (int i = 0; i < facet.length; i++) { - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], - // names[i]); - // if (response.equals("Renamed")) successCount++; - // } + File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - // if (successCount >= 2) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // response = - // api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) testStatus = true; - // } - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } + Map postData = new HashMap<>(); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // if (!testStatus) { - // fail("Attachment was renamed with unsupported characters"); - // } - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Entity in draft mode".equals(response)) { + fail("Entity not in draft mode: " + response); + return; + } - // @Test - // @Order(14) - // void testRenameToValidateNames() throws IOException { - // System.out.println("Test (14) : Rename attachments to validate names"); - // String[] generatedIDs = new String[3]; - // String[] duplicateIDs = new String[1]; - // boolean testStatus = false, allRenamedSuccessfully = true; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID3 = response; + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", entityID); + List createResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, tempFile); + + String check = createResponse.get(0); + if (!"Attachment created".equals(check)) { + System.out.println("Failed to create attachment for facet: " + facet[i]); + continue; + } - // String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; - // String duplicateName = "duplicateName.pdf"; + String restrictedName = "a/\\bc.pdf"; + response = + api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); + } - // 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"); + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // // Creation of attachment, reference and footnote - // for (int i = 0; i < facet.length; i++) { - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - // generatedIDs[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // response = - // api.renameAttachment( - // appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); - // allRenamedSuccessfully &= "Renamed".equals(response); - // } - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // // Creating duplicate name for last facet - // duplicateIDs[0] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[2], entityID3, postData, file); - // String response2 = - // api.renameAttachment( - // appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); - - // if (allRenamedSuccessfully && "Renamed".equals(response2)) { - // 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: references\\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: footnotes\\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 expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + if (response.equals(expected)) { + api.deleteEntityDraft(appUrl, entityName, entityID); + testStatus = true; + } - // @Test - // @Order(15) - // void testRenameEntitiesWithoutSDMRole() throws IOException { - // System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); - // boolean testStatus = true; - // try { - // String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if ("Entity in draft mode".equals(apiResponse)) { - // String[] name = {"sample456", "reference456", "footnote456"}; - // for (int i = 0; i < facet.length; i++) { - // apiResponse = - // apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], - // name[i]); - // if (!"Renamed".equals(apiResponse)) { - // testStatus = false; - // } - // } - // if (testStatus) { - // apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "[{\"code\":\"\",\"message\":\"Could not update the following files. - // \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update - // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (!apiResponse.equals(expected)) { - // testStatus = false; - // } - // } else { - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // } catch (Exception e) { - // testStatus = false; - // } - // if (!testStatus) { - // fail("Attachment got renamed without SDM roles."); - // } - // } + if (!testStatus) { + fail("Facets renamed with restricted characters were not correctly rejected."); + } + } - // @Test - // @Order(16) - // void testDeleteSingleAttachment() throws IOException { - // System.out.println("Test (16) : Delete single attachment, reference, and footnote"); - // Boolean testStatus = false; - // counter = -1; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - // if (response.equals("Deleted")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // counter = -1; // Reset counter for the next check - // if (response.equals("Saved")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - // if (response.equals("Could not read Attachment")) counter++; - // } - // if (counter >= 2) testStatus = true; - // else fail("Could not read deleted facets"); - // } else { - // fail("Could not save entity after deletion"); - // } - // } - // } + @Test + @Order(10) + void testRenameEntitiesWithUnsupportedCharacter() { + System.out.println("Test (10) : Rename attachments with unsupported characters"); + Boolean testStatus = false; - // @Test - // @Order(17) - // void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { - // System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - // String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - // if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; - // } - // } - // if (counter >= 2) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // if (response.equals("Saved")) { - // for (int i = 0; i < facet.length; i++) { - // String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - // String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - // if (response1.equals("Could not read " + facet[i]) - // && response2.equals("Could not read " + facet[i])) { - // counter++; - // } - // } - // if (counter >= 2) testStatus = true; - // else fail("Could not read deleted facets"); - // } else fail("Could not save entity after deletion"); - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + if (response.equals("Renamed")) counter++; + } + if (counter >= 2) { + counter = -1; // Reset counter for the next check + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + if (response.equals(expected)) { + for (int i = 0; i < facet.length; i++) { + response = + api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], "sample.pdf"); + } + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment was renamed with unsupported characters"); + } + } - // @Test - // @Order(18) - // void testUploadBlockedMimeType() throws IOException { - // System.out.println("Test (18) : Upload blocked mimeType .rtf"); - // Boolean testStatus = false; + @Test + @Order(11) + void testRenameMultipleEntityComponents() { + System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); + boolean testStatus = true; + + String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Entity in draft mode".equals(draftResponse)) { + fail("Entity is not in draft mode."); + return; + } + String[] name = {"sample1234", "reference1234", "footnote1234"}; + String[] name2 = {"sample12345", "reference12345", "footnote12345"}; + for (int i = 0; i < facet.length; i++) { + // Read the facet to ensure it exists + testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); + testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); + } + // Save the draft if all renames succeeded + if (testStatus) { + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Saved".equals(saveResponse)) { + fail("Entity draft was not saved after renaming."); + } + } else { + // Save draft even if renaming failed to preserve state + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + fail("One or more components were not renamed."); + } + } - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID2 = response; + @Test + @Order(12) + void testRenameSingleDuplicate() { + System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); + Boolean testStatus = false; + + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String[] name = {"sample1234", "reference1234", "footnote1234"}; + String[] name2 = {"sample123456", "reference123456", "footnote123456"}; + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + if (response.equals("Renamed")) counter++; + } + if (counter >= 2) { + counter = -1; // Reset counter for the next check + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + String.format( + "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", + name[1], name[0], name[2]); + if (response.equals(expected)) { + for (int i = 0; i < facet.length; i++) { + // Attempt to rename again with a different name + response = + api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); + if (response.equals("Renamed")) counter++; + } + } + if (counter >= 2) { + // If all renames were successful, save the draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + testStatus = true; + } + } else { + testStatus = false; + fail("Attachment was renamed"); + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + @Test + @Order(13) + void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { + System.out.println( + "Test (13) : Rename multiple files out of which one file name contains unsupported characters"); + boolean testStatus = false; - // 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"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String[] names = {"summary_1234", "reference_4567", "note/invalid"}; - // boolean allBlocked = true; - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, - // file); + if (response.equals("Entity in draft mode")) { + int successCount = 0; + for (int i = 0; i < facet.length; i++) { + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], names[i]); + if (response.equals("Renamed")) successCount++; + } - // 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 (successCount >= 2) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + response = + api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); + 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 (!expectedJson.equals(actualResponse)) { - // allBlocked = false; - // System.out.println( - // "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); - // } - // } + if (!testStatus) { + fail("Attachment was renamed with unsupported characters"); + } + } - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if ("Saved".equals(response) && allBlocked) { - // testStatus = true; - // } - // } + @Test + @Order(14) + void testRenameToValidateNames() throws IOException { + System.out.println("Test (14) : Rename attachments to validate names"); + String[] generatedIDs = new String[3]; + String[] duplicateIDs = new String[1]; + boolean testStatus = false, allRenamedSuccessfully = true; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID3 = response; - // if (!testStatus) { - // fail("Attachment got uploaded with blocked .rtf MIME type"); - // } - // } + String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; + String duplicateName = "duplicateName.pdf"; - // @Test - // @Order(19) - // void testDeleteEntity() { - // System.out.println("Test (19) : Delete entity"); - // Boolean testStatus = false; - // String response = api.deleteEntity(appUrl, entityName, entityID); - // String response2 = api.deleteEntity(appUrl, entityName, entityID2); - // if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = - // true; - // if (!testStatus) fail("Could not delete entity"); - // } + 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(20) - // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - // System.out.println("Test (20) : Rename & Update secondary property before entity is saved"); - // System.out.println("Creating entity"); + // Creation of attachment, reference and footnote + for (int i = 0; i < facet.length; i++) { + File file = new File(classLoader.getResource("sample2.pdf").getFile()); + generatedIDs[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + response = + api.renameAttachment( + appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); + allRenamedSuccessfully &= "Renamed".equals(response); + } + File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Creating duplicate name for last facet + duplicateIDs[0] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[2], entityID3, postData, file); + String response2 = + api.renameAttachment( + appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); + + if (allRenamedSuccessfully && "Renamed".equals(response2)) { + 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: references\\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: footnotes\\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; + } + } - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @Test + @Order(15) + void testRenameEntitiesWithoutSDMRole() throws IOException { + System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); + boolean testStatus = true; + try { + String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Entity in draft mode".equals(apiResponse)) { + String[] name = {"sample456", "reference456", "footnote456"}; + for (int i = 0; i < facet.length; i++) { + apiResponse = + apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); + if (!"Renamed".equals(apiResponse)) { + testStatus = false; + } + } + if (testStatus) { + apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (!apiResponse.equals(expected)) { + testStatus = false; + } + } else { + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + } catch (Exception e) { + testStatus = false; + } + if (!testStatus) { + fail("Attachment got renamed without SDM roles."); + } + } - // if (!response.equals("Could not create entity")) { - // entityID3 = response; + @Test + @Order(16) + void testDeleteSingleAttachment() throws IOException { + System.out.println("Test (16) : Delete single attachment, reference, and footnote"); + Boolean testStatus = false; + counter = -1; - // System.out.println("Creating attachment, reference, and footnote"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + if (response.equals("Deleted")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + counter = -1; // Reset counter for the next check + if (response.equals("Saved")) { + for (int i = 0; i < facet.length; i++) { + response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + if (response.equals("Could not read Attachment")) counter++; + } + if (counter >= 2) testStatus = true; + else fail("Could not read deleted facets"); + } else { + fail("Could not save entity after deletion"); + } + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + @Test + @Order(17) + void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { + System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); + Boolean testStatus = false; - // 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"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; + } + } + if (counter >= 2) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + if (response.equals("Saved")) { + for (int i = 0; i < facet.length; i++) { + String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + if (response1.equals("Could not read " + facet[i]) + && response2.equals("Could not read " + facet[i])) { + counter++; + } + } + if (counter >= 2) testStatus = true; + else fail("Could not read deleted facets"); + } else fail("Could not save entity after deletion"); + } - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + @Test + @Order(18) + void testUploadBlockedMimeType() throws IOException { + System.out.println("Test (18) : Upload blocked mimeType .rtf"); + Boolean testStatus = false; - // System.out.println("Attachments, References, and Footnotes created"); - - // // Use valid dropdown value for customProperty1 - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; - - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - - // // Update customProperty1 (String - dropdown value) - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - - // // Update customProperty2 (Integer) - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - - // // Update customProperty5 (DateTime) - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - - // // Update customProperty6 (Boolean) - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // counter++; - // } - // } + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID2 = response; - // if (counter >= 2) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // } - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + 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"); - // @Test - // @Order(21) - // void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { - // System.out.println("Test (21): Rename & Update secondary property after entity is saved"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // System.out.println("Editing entity"); - - // if (response.equals("Entity in draft mode")) { - // // Sample secondary properties - // String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; - // Integer secondaryPropertyInt = 42; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // System.out.println("Renaming and updating secondary properties for attachment"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Saved")) { - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for attachment"); - // } - // // Clean up - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); - // } - // if (!testStatus) fail("Could not update secondary properties after entity is saved"); - // } + boolean allBlocked = true; + for (int i = 0; i < facet.length; i++) { + List createResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, file); - // @Test - // @Order(22) - // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - // System.out.println( - // "Test (22): 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 (response != "Could not create entity") { - // entityID3 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + 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.\"}}"; - // 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"); + if (!expectedJson.equals(actualResponse)) { + allBlocked = false; + System.out.println( + "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); + } + } + + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if ("Saved".equals(response) && allBlocked) { + testStatus = true; + } + } + + if (!testStatus) { + fail("Attachment got uploaded with blocked .rtf MIME type"); + } + } + + @Test + @Order(19) + void testDeleteEntity() { + System.out.println("Test (19) : Delete entity"); + Boolean testStatus = false; + String response = api.deleteEntity(appUrl, entityName, entityID); + String response2 = api.deleteEntity(appUrl, entityName, entityID2); + if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = true; + if (!testStatus) fail("Could not delete entity"); + } + + @Test + @Order(20) + void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + System.out.println("Test (20) : 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.equals("Could not create entity")) { + entityID3 = response; + + System.out.println("Creating attachment, reference, and footnote"); + + 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"); + + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + + System.out.println("Attachments, References, and Footnotes created"); + + // Use valid dropdown value for customProperty1 + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + + // Update customProperty1 (String - dropdown value) + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + + // Update customProperty2 (Integer) + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + + // Update customProperty5 (DateTime) + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + + // Update customProperty6 (Boolean) + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + counter++; + } + } + + if (counter >= 2) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + } + if (response.equals("Saved")) { + testStatus = true; + } + } + + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(21) + void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { + System.out.println("Test (21): Rename & Update secondary property after entity is saved"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + System.out.println("Editing entity"); + + if (response.equals("Entity in draft mode")) { + // Sample secondary properties + String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; + Integer secondaryPropertyInt = 42; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + System.out.println("Renaming and updating secondary properties for attachment"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Saved")) { + testStatus = true; + System.out.println("Renamed & updated Secondary properties for attachment"); + } + // Clean up + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); + } + if (!testStatus) fail("Could not update secondary properties after entity is saved"); + } + + @Test + @Order(22) + void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + System.out.println( + "Test (22): 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 (response != "Could not create entity") { + entityID3 = response; + 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"); + + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + // Prepare test data + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, or footnote"); + } + + @Test + @Order(23) + void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { + System.out.println( + "Test (23): 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.equals("Entity in draft mode")) { + String name1 = "sample.pdf"; + Integer secondaryPropertyInt = 12; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testidinvalid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for attachment, reference, footnote is unsuccessfull"); + } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (!deleteEntityResponse.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } + if (!testStatus) + fail( + "Could not update secondary property after entity is saved for attachment, reference, or footnote"); + } + + @Test + @Order(24) + void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (24): Rename & Update valid secondary properties for multiple facets 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"); + 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"); + + System.out.println("Creating attachment, reference, and footnote PDF"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + + System.out.println("Creating attachment, reference, and footnote TXT"); + file = new File(classLoader.getResource("sample.txt").getFile()); + postData.put("mimeType", "application/txt"); + for (int i = 0; i < facet.length; i++) { + ID2[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + + System.out.println("Creating attachment, reference, and footnote EXE"); + file = new File(classLoader.getResource("sample.exe").getFile()); + postData.put("mimeType", "application/exe"); + for (int i = 0; i < facet.length; i++) { + ID3[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + Boolean Updated1[] = new Boolean[3]; + Boolean Updated2[] = new Boolean[3]; + Boolean Updated3[] = new Boolean[3]; + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // PDF + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + // 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, facet[i], entityID3, ID2[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + for (int i = 0; i < facet.length; i++) { + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponseEXE3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); + + if (updateSecondaryPropertyResponseEXE1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated") + && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + 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"); + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } - // // Prepare test data - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, - // or footnote"); - // } + @Test + @Order(25) + void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { + System.out.println( + "Test (25): Rename & Update valid secondary properties for multiple facets after entity is saved"); + System.out.println("Editing entity"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Entity in draft mode")) { + Boolean Updated1[] = new Boolean[3]; + Boolean Updated2[] = new Boolean[3]; + Boolean Updated3[] = new Boolean[3]; + + String name1 = "sample1.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } - // @Test - // @Order(23) - // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { - // System.out.println( - // "Test (23): 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.equals("Entity in draft mode")) { - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt = 12; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testidinvalid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for attachment, reference, footnote is - // unsuccessfull"); - // } - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (!deleteEntityResponse.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } - // if (!testStatus) - // fail( - // "Could not update secondary property after entity is saved for attachment, reference, - // or footnote"); - // } + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + // 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, facet[i], entityID3, ID2[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } - // @Test - // @Order(24) - // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (24): Rename & Update valid secondary properties for multiple facets 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; + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + for (int i = 0; i < facet.length; i++) { + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponseEXE3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); + + if (updateSecondaryPropertyResponseEXE1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated") + && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } - // System.out.println("Entity created"); - // ClassLoader classLoader = getClass().getClassLoader(); + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + 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"); + } + } - // 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_beforeEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (26): Rename & Update invalid and valid secondary properties for multiple facets before entity is saved"); + System.out.println("Creating entity"); - // System.out.println("Creating attachment, reference, and footnote PDF"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // System.out.println("Creating attachment, reference, and footnote TXT"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // postData.put("mimeType", "application/txt"); - // for (int i = 0; i < facet.length; i++) { - // ID2[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + if (!"Could not create entity".equals(response)) { + entityID3 = response; + System.out.println("Entity created"); - // System.out.println("Creating attachment, reference, and footnote EXE"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // postData.put("mimeType", "application/exe"); - // for (int i = 0; i < facet.length; i++) { - // ID3[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } - // Boolean Updated1[] = new Boolean[3]; - // Boolean Updated2[] = new Boolean[3]; - // Boolean Updated3[] = new Boolean[3]; - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // // PDF - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID3); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // // 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, facet[i], entityID3, ID2[i], - // bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + // Create PDF attachments + postData.put("mimeType", "application/pdf"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // for (int i = 0; i < facet.length; i++) { - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponseEXE3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyDate); - - // if (updateSecondaryPropertyResponseEXE1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated") - // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // 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"); - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + // Create TXT attachments + postData.put("mimeType", "application/txt"); + file = new File(classLoader.getResource("sample.txt").getFile()); + for (int i = 0; i < facet.length; i++) { + ID2[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } - // @Test - // @Order(25) - // void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { - // System.out.println( - // "Test (25): Rename & Update valid secondary properties for multiple facets after entity - // is saved"); - // System.out.println("Editing entity"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Entity in draft mode")) { - // Boolean Updated1[] = new Boolean[3]; - // Boolean Updated2[] = new Boolean[3]; - // Boolean Updated3[] = new Boolean[3]; - - // String name1 = "sample1.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } + // Create EXE attachments + postData.put("mimeType", "application/exe"); + file = new File(classLoader.getResource("sample.exe").getFile()); + for (int i = 0; i < facet.length; i++) { + ID3[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // // 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, facet[i], entityID3, ID2[i], - // bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // for (int i = 0; i < facet.length; i++) { - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponseEXE3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyDate); - - // if (updateSecondaryPropertyResponseEXE1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated") - // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } + String name1 = "sample1234.pdf"; + String dropdownValue = + integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // 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"); - // } - // } + // Update PDF properties + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String renameResp = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + String upd2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + String upd3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + String upd4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + String updInvalid = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + if ("Renamed".equals(renameResp) + && "Updated".equals(upd1) + && "Updated".equals(upd2) + && "Updated".equals(upd3) + && "Updated".equals(upd4) + && "Updated".equals(updInvalid)) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } - // @Test - // @Order(26) - // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (26): Rename & Update invalid and valid secondary properties for multiple facets - // before entity is saved"); - // System.out.println("Creating entity"); + // Update TXT properties + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + String upd = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], bodyBool); + if ("Updated".equals(upd)) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // Update EXE properties + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValueExe = integrationTestUtils.getDropDownValue(); + String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - // if (!"Could not create entity".equals(response)) { - // entityID3 = response; - // System.out.println("Entity created"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyDropdownExe = + RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + RequestBody bodyIntExe = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); + String upd2 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); + + if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID3); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - // // Create PDF attachments - // postData.put("mimeType", "application/pdf"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - // // Create TXT attachments - // postData.put("mimeType", "application/txt"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID2[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + // Verify PDF metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals(expectedNames[0], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertNull(metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } - // // Create EXE attachments - // postData.put("mimeType", "application/exe"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID3[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + // Verify TXT metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + assertEquals(expectedNames[1], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertTrue((Boolean) metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample1234.pdf"; - // String dropdownValue = - // integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - - // // Update PDF properties - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String renameResp = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // String upd2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // String upd3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // String upd4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - // String updInvalid = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - // if ("Renamed".equals(renameResp) - // && "Updated".equals(upd1) - // && "Updated".equals(upd2) - // && "Updated".equals(upd3) - // && "Updated".equals(upd4) - // && "Updated".equals(updInvalid)) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } + // Verify EXE metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + assertEquals(expectedNames[2], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertEquals( + dropdownValueExe, + metadata.get("customProperty1_code")); // Adjust expected value if needed + assertEquals(1234, metadata.get("customProperty2")); + } - // // Update TXT properties - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - // String upd = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], - // bodyBool); - // if ("Updated".equals(upd)) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + 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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update unsuccessful for invalid properties and successful for valid attachments"); + } + } + } - // // Update EXE properties - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValueExe = integrationTestUtils.getDropDownValue(); - // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyDropdownExe = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - // RequestBody bodyIntExe = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); - - // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } - // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - - // // Verify PDF metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals(expectedNames[0], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertNull(metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } + @Test + @Order(27) + void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (27): 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.equals("Entity in draft mode")) { + Boolean Updated1[] = new Boolean[3]; + Boolean Updated2[] = new Boolean[3]; + Boolean Updated3[] = new Boolean[3]; + String name1 = "sample.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + String dropdownValue = integrationTestUtils.getDropDownValue(); + System.out.println("drop down value is: " + dropdownValue); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // PDF + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // Update secondary properties for String + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + // Update invalid secondary property + String updateSecondaryPropertyResponse5 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated") + && updateSecondaryPropertyResponse5.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + // 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, facet[i], entityID3, ID2[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } - // // Verify TXT metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - // assertEquals(expectedNames[1], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertTrue((Boolean) metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } + Integer secondaryPropertyInt3 = 12; + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + for (int i = 0; i < facet.length; i++) { + // Update secondary properties for String + System.out.println("drop down value is: " + dropdownValue1); + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); + + if (updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } - // // Verify EXE metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - // assertEquals(expectedNames[2], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertEquals( - // dropdownValueExe, - // metadata.get("customProperty1_code")); // Adjust expected value if needed - // assertEquals(1234, metadata.get("customProperty2")); - // } + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; + // for PDF + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals(name[0], FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + // for TXT + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + assertEquals(name[1], FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertFalse((Boolean) FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + // for EXE + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + assertEquals(name[2], FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); + assertEquals(12, FacetMetadata.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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessful for invalid properties and successful for valid - // attachments"); - // } - // } - // } + 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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); + } + 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 (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + @Test + @Order(28) + void testNAttachments_NewEntity() throws IOException { + System.out.println( + "Test (28): 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, facet[0], entityID4, srvpath, postData1, file); + if (createResponse1.get(0).equals("Attachment created")) { + ID[0] = createResponse1.get(1); + System.out.println("Attachment created"); + } - // @Test - // @Order(27) - // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (27): 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.equals("Entity in draft mode")) { - // Boolean Updated1[] = new Boolean[3]; - // Boolean Updated2[] = new Boolean[3]; - // Boolean Updated3[] = new Boolean[3]; - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // System.out.println("drop down value is: " + dropdownValue); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // // PDF - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // // Update secondary properties for String - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - // // Update invalid secondary property - // String updateSecondaryPropertyResponse5 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated") - // && updateSecondaryPropertyResponse5.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // // 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, facet[i], entityID3, ID2[i], - // bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + 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, facet[0], entityID4, srvpath, postData2, file); + if (createResponse2.get(0).equals("Attachment created")) { + ID2[0] = createResponse2.get(1); + System.out.println("Attachment created"); + } - // Integer secondaryPropertyInt3 = 12; - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // for (int i = 0; i < facet.length; i++) { - // // Update secondary properties for String - // System.out.println("drop down value is: " + dropdownValue1); - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyInt); - - // if (updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } + 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, facet[0], entityID4, srvpath, postData3, file); + if (createResponse3.get(0).equals("Attachment created")) { + ID[0] = createResponse3.get(1); + System.out.println("Attachment created"); + } - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; - // // for PDF - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals(name[0], FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - // // for TXT - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - // assertEquals(name[1], FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertFalse((Boolean) FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - // // for EXE - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - // assertEquals(name[2], FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); - // assertEquals(12, FacetMetadata.get("customProperty2")); - // } + 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, facet[0], entityID4, srvpath, postData3, file); + if (createResponse4.get(0).equals("Attachment created")) { + ID4[0] = createResponse4.get(1); + System.out.println("Attachment created"); + } - // 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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); - // } - // 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 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, facet[0], entityID4, srvpath, postData3, file); + if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { + testStatus = true; + ID5[0] = 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(28) - // void testNAttachments_NewEntity() throws IOException { - // System.out.println( - // "Test (28): 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; + @Test + @Order(29) + void testUploadNAttachments() throws IOException { + System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); - // System.out.println("Entity created"); + 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, facet[0], 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"); + } + } + } - // System.out.println("Creating attachment PDF"); - // ClassLoader classLoader = getClass().getClassLoader(); + @Test + @Order(30) + void testDiscardDraftWithoutAttachments() { + System.out.println("Test (30) : Discard draft without adding attachments"); + Boolean testStatus = false; - // 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"); + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID6 = response; + response = api.deleteEntityDraft(appUrl, entityName, entityID6); + if (response.equals("Entity Draft Deleted")) { + testStatus = true; + } + } + if (!testStatus) { + fail("Draft was not discarded properly"); + } + } - // List createResponse1 = - // api.createAttachment(appUrl, entityName, facet[0], entityID4, srvpath, postData1, - // file); - // if (createResponse1.get(0).equals("Attachment created")) { - // ID[0] = createResponse1.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(31) + void testDiscardDraftWithAttachments() throws IOException { + System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); + 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, facet[0], entityID4, srvpath, postData2, - // file); - // if (createResponse2.get(0).equals("Attachment created")) { - // ID2[0] = createResponse2.get(1); - // System.out.println("Attachment created"); - // } + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID6 = response; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID6); + 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 < facet.length; i++) { + List createResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, file); + if ("Attachment created".equals(createResponse.get(0))) { + System.out.println("Attachment created in facet: " + facet[i]); + } else { + System.out.println("Attachment creation failed in facet: " + facet[i]); + } + } + response = api.deleteEntityDraft(appUrl, entityName, entityID6); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + if (!testStatus) { + fail("Draft with attachments was not discarded properly"); + } + } - // 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, facet[0], entityID4, srvpath, postData3, - // file); - // if (createResponse3.get(0).equals("Attachment created")) { - // ID[0] = createResponse3.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(32) + void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { + System.out.println("Test (32): Upload to all facets, delete one, and create 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, facet[0], entityID4, srvpath, postData3, - // file); - // if (createResponse4.get(0).equals("Attachment created")) { - // ID4[0] = createResponse4.get(1); - // System.out.println("Attachment created"); - // } + boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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, facet[0], entityID4, srvpath, postData3, - // file); - // if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { - // testStatus = true; - // ID5[0] = 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"); - // } - // } + if (!"Could not create entity".equals(response)) { + entityID5 = response; + ClassLoader classLoader = getClass().getClassLoader(); + + File file1 = + new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + File file2 = + new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); + + Map postData2 = new HashMap<>(postData1); + postData2.put("up__ID", entityID5); + postData2.put("mimeType", "text/plain"); + postData2.put("createdAt", new Date().toString()); + postData2.put("createdBy", "test@test.com"); + postData2.put("modifiedBy", "test@test.com"); + + boolean allCreated = true; + for (int i = 0; i < facet.length; i++) { + List response1 = + api.createAttachment( + appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); + List response2 = + api.createAttachment( + appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); + + if (response1.get(0).equals("Attachment created") + && response2.get(0).equals("Attachment created")) { + ID4[i] = response1.get(1); // to keep one + ID5[i] = response2.get(1); // will delete this one + } else { + allCreated = false; + break; + } - // @Test - // @Order(29) - // void testUploadNAttachments() throws IOException { - // System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); + String deleteResponse = + api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); + if (!"Deleted".equals(deleteResponse)) { + allCreated = false; + break; + } + } - // 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, facet[0], 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"); - // } - // } - // } + if (allCreated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } - // @Test - // @Order(30) - // void testDiscardDraftWithoutAttachments() { - // System.out.println("Test (30) : Discard draft without adding attachments"); - // Boolean testStatus = false; + if (!testStatus) { + fail("Failed to upload multiple facet entries, delete one per facet and create entity"); + } + } - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID6 = response; - // response = api.deleteEntityDraft(appUrl, entityName, entityID6); - // if (response.equals("Entity Draft Deleted")) { - // testStatus = true; - // } - // } - // if (!testStatus) { - // fail("Draft was not discarded properly"); - // } - // } + @Test + @Order(33) + void testUpdateEntityDraft() throws IOException { + System.out.println("Test (33): Update entity draft with new facet content"); + boolean testStatus = false; - // @Test - // @Order(31) - // void testDiscardDraftWithAttachments() throws IOException { - // System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); - // boolean testStatus = false; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID6 = response; - // 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", entityID6); - // 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 < facet.length; i++) { - // List createResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, - // file); - // if ("Attachment created".equals(createResponse.get(0))) { - // System.out.println("Attachment created in facet: " + facet[i]); - // } else { - // System.out.println("Attachment creation failed in facet: " + facet[i]); - // } - // } - // response = api.deleteEntityDraft(appUrl, entityName, entityID6); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // if (!testStatus) { - // fail("Draft with attachments was not discarded properly"); - // } - // } + 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(32) - // void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { - // System.out.println("Test (32): Upload to all facets, delete one, and create entity"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); + if ("Entity in draft mode".equals(response)) { + boolean allCreated = true; - // boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + for (int i = 0; i < facet.length; i++) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); + if (!"Attachment created".equals(createResponse.get(0))) { + allCreated = false; + } + } - // if (!"Could not create entity".equals(response)) { - // entityID5 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - - // File file1 = - // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // File file2 = - // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); - - // Map postData2 = new HashMap<>(postData1); - // postData2.put("up__ID", entityID5); - // postData2.put("mimeType", "text/plain"); - // postData2.put("createdAt", new Date().toString()); - // postData2.put("createdBy", "test@test.com"); - // postData2.put("modifiedBy", "test@test.com"); - - // boolean allCreated = true; - // for (int i = 0; i < facet.length; i++) { - // List response1 = - // api.createAttachment( - // appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); - // List response2 = - // api.createAttachment( - // appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); - - // if (response1.get(0).equals("Attachment created") - // && response2.get(0).equals("Attachment created")) { - // ID4[i] = response1.get(1); // to keep one - // ID5[i] = response2.get(1); // will delete this one - // } else { - // allCreated = false; - // break; - // } + if (allCreated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } + api.deleteEntity(appUrl, entityName, entityID5); + if (!testStatus) { + fail("Failed to update draft with new attachments for all facets"); + } + } - // String deleteResponse = - // api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); - // if (!"Deleted".equals(deleteResponse)) { - // allCreated = false; - // break; - // } - // } + @Test + @Order(34) + void testUploadAttachmentWithoutSDMRole() throws IOException { + System.out.println("Test (34): Upload attachment across facets without SDM role"); + boolean testStatus = true; + + String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID7 = response; + 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", 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 (allCreated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } + for (int i = 0; i < facet.length; i++) { + List createResponse = + apiNoRoles.createAttachment( + appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); + String check = createResponse.get(0); + String expectedError = + "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions to upload attachments. Please contact your administrator for access.\"}}"; + if (!expectedError.equals(check)) { + testStatus = false; + } + } + } + api.deleteEntityDraft(appUrl, entityName, entityID7); + if (!testStatus) { + fail("Attachment uploaded without SDM role for one or more facets"); + } + } - // if (!testStatus) { - // fail("Failed to upload multiple facet entries, delete one per facet and create entity"); - // } - // } + @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<>(); + for (int i = 0; i < 3; i++) { + attachments.add(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(33) - // void testUpdateEntityDraft() throws IOException { - // System.out.println("Test (33): Update entity draft with new facet content"); - // boolean testStatus = false; + for (int i = 0; i < facet.length; i++) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + copyAttachmentSourceEntity, + srvpath, + postData, + file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + } + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facet[i], 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"); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + if (sourceObjectIds.size() == 6) { + String copyResponse; + int i = 0; + for (String facetName : facet) { + if (i != 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } + copyResponse = + api.copyAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + i += 2; + if (copyResponse.equals("Attachments copied successfully")) { + // Fetch copied attachment IDs from target draft + List> copiedMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + List copiedAttachmentIds = + copiedMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + 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"); + } + } - // 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 ("Entity in draft mode".equals(response)) { - // boolean allCreated = true; - - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); - // if (!"Attachment created".equals(createResponse.get(0))) { - // allCreated = false; - // } - // } + @Test + @Order(36) + void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (36): Copy incorrect 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")) { + if (sourceObjectIds.size() == 6) { + int i = 0; + for (String facet : facet) { + try { + List currentFacetObjectIds = + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + currentFacetObjectIds.add("incorrectObjectId"); + if (currentFacetObjectIds.size() != 3) { + fail("Not enough object IDs to copy attachments for facet: " + facet); + } + api.copyAttachment( + appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, currentFacetObjectIds); + fail("Copy attachments did not throw an error"); + } catch (IOException e) { + i += 2; + } + } + 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 (allCreated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } - // api.deleteEntity(appUrl, entityName, entityID5); - // if (!testStatus) { - // fail("Failed to update draft with new attachments for all facets"); - // } - // } + @Test + @Order(37) + void testCopyAttachmentWithNotesField() throws IOException { + System.out.println( + "Test (37): Create entity with attachments containing notes in multiple facets, copy to new entity and verify notes field"); + Boolean testStatus = false; - // @Test - // @Order(34) - // void testUploadAttachmentWithoutSDMRole() throws IOException { - // System.out.println("Test (34): Upload attachment across facets without SDM role"); - // boolean testStatus = true; + copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID7 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + String notesValue = "This is a test note for copy attachment verification"; + MediaType mediaType = MediaType.parse("application/json"); + + for (String facetName : facet) { + 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); - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } - // 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 sourceAttachmentId = createResponse.get(1); - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // apiNoRoles.createAttachment( - // appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); - // String check = createResponse.get(0); - // String expectedError = - // "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions - // to upload attachments. Please contact your administrator for access.\"}}"; - // if (!expectedError.equals(check)) { - // testStatus = false; - // } - // } - // } - // api.deleteEntityDraft(appUrl, entityName, entityID7); - // if (!testStatus) { - // fail("Attachment uploaded without SDM role for one or more facets"); - // } - // } + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - // @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<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(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"); + String updateResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateBody); - // for (int i = 0; i < facet.length; i++) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // copyAttachmentSourceEntity, - // srvpath, - // postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // } - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facet[i], 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"); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - // if (sourceObjectIds.size() == 6) { - // String copyResponse; - // int i = 0; - // for (String facetName : facet) { - // if (i != 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } - // copyResponse = - // api.copyAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - // i += 2; - // if (copyResponse.equals("Attachments copied successfully")) { - // // Fetch copied attachment IDs from target draft - // List> copiedMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // List copiedAttachmentIds = - // copiedMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // 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"); - // } - // } + if (!updateResponse.equals("Updated")) { + fail("Could not update attachment notes field in facet: " + facetName); + } + } - // @Test - // @Order(36) - // void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { - // System.out.println( - // "Test (36): Copy incorrect 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")) { - // if (sourceObjectIds.size() == 6) { - // int i = 0; - // for (String facet : facet) { - // try { - // List currentFacetObjectIds = - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - // currentFacetObjectIds.add("incorrectObjectId"); - // if (currentFacetObjectIds.size() != 3) { - // fail("Not enough object IDs to copy attachments for facet: " + facet); - // } - // api.copyAttachment( - // appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, - // currentFacetObjectIds); - // fail("Copy attachments did not throw an error"); - // } catch (IOException e) { - // i += 2; - // } - // } - // 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"); - // } - // } + List objectIdsToStore = new ArrayList<>(); + for (String facetName : facet) { + List> sourceAttachmentsMetadata = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); - // @Test - // @Order(37) - // void testCopyAttachmentWithNotesField() throws IOException { - // System.out.println( - // "Test (37): Create entity with attachments containing notes in multiple facets, copy to - // new entity and verify notes field"); - // Boolean testStatus = false; + if (sourceAttachmentsMetadata.isEmpty()) { + fail("No attachments found in source entity for facet: " + facetName); + } - // copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyCustomSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // String notesValue = "This is a test note for copy attachment verification"; - // MediaType mediaType = MediaType.parse("application/json"); + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId for facet: " + facetName); + } - // for (String facetName : facet) { - // 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 sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + objectIdsToStore.add(sourceObjectId); + + 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 for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + sourceNoteValue); + } + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + int startIndex = sourceObjectIds.size(); + sourceObjectIds.addAll(objectIdsToStore); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity"); + } - // String sourceAttachmentId = createResponse.get(1); + copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); + int facetIndex = 0; + for (String facetName : facet) { + if (facetIndex > 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } - // String updateResponse = - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // copyCustomSourceEntity, - // sourceAttachmentId, - // updateBody); + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - // if (!updateResponse.equals("Updated")) { - // fail("Could not update attachment notes field in facet: " + facetName); - // } - // } + String copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // List objectIdsToStore = new ArrayList<>(); - // for (String facetName : facet) { - // List> sourceAttachmentsMetadata = - // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity for facet: " + facetName); + } - // if (sourceAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in source entity for facet: " + facetName); - // } + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity for facet: " + facetName); + } - // Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); + facetIndex++; + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId for facet: " + facetName); - // } + for (String facetName : facet) { + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // objectIdsToStore.add(sourceObjectId); - - // 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 for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + sourceNoteValue); - // } - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity for facet: " + facetName); + } - // int startIndex = sourceObjectIds.size(); - // sourceObjectIds.addAll(objectIdsToStore); + 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 for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + copiedNoteValue); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyCustomTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment from target entity for facet: " + facetName); + } else { + testStatus = true; + } + } - // int facetIndex = 0; - // for (String facetName : facet) { - // if (facetIndex > 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } + if (!testStatus) { + fail( + "Could not verify that notes field was copied from source to target attachment for all facets"); + } + } - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + @Test + @Order(38) + void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { + System.out.println( + "Test (38): Verify that secondary properties are preserved when copying attachments between entities across multiple facets"); + Boolean testStatus = false; - // String copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity for facet: " + facetName); - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample1.pdf").getFile()); - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity for facet: " + facetName); - // } + List objectIdsToStore = new ArrayList<>(); - // facetIndex++; - // } + for (String facetName : facet) { + 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"); - // for (String facetName : facet) { - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity for facet: " + facetName); - // } + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } - // 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 for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + copiedNoteValue); - // } + String sourceAttachmentId = createResponse.get(1); + + 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 for facet: " + facetName); + } - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + 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 for facet: " + facetName); + } + } - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment from target entity for facet: " + facetName); - // } else { - // testStatus = true; - // } - // } + // Save source entity to persist attachments before fetching metadata + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after creating attachments"); + } - // if (!testStatus) { - // fail( - // "Could not verify that notes field was copied from source to target attachment for all - // facets"); - // } - // } + Integer customProperty2Value = 12345; + for (String facetName : facet) { + List> sourceAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - // @Test - // @Order(38) - // void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { - // System.out.println( - // "Test (38): Verify that secondary properties are preserved when copying attachments - // between entities across multiple facets"); - // Boolean testStatus = false; - - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyCustomSourceEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity"); - // } + Map sourceAttachmentMetadata = + sourceAttachmentsMetadata.stream() + .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample1.pdf").getFile()); + if (sourceAttachmentMetadata == null) { + fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); + } - // List objectIdsToStore = new ArrayList<>(); + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId for facet: " + facetName); + } - // for (String facetName : facet) { - // 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 sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + objectIdsToStore.add(sourceObjectId); + + 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 for facet " + + facetName + + ". Expected: true, Got: " + + sourceCustomProperty6); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } + int startIndex = sourceObjectIds.size(); + sourceObjectIds.addAll(objectIdsToStore); - // String sourceAttachmentId = createResponse.get(1); - - // 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 for facet: " + - // facetName); - // } + int facetIndex = 0; + for (String facetName : facet) { + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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 for facet: " + facetName); - // } - // } + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - // // Save source entity to persist attachments before fetching metadata - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after creating attachments"); - // } + String copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // Integer customProperty2Value = 12345; - // for (String facetName : facet) { - // List> sourceAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity for facet: " + facetName); + } - // Map sourceAttachmentMetadata = - // sourceAttachmentsMetadata.stream() - // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + // Fetch copied attachment IDs from target draft + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity for facet: " + facetName); + } - // if (sourceAttachmentMetadata == null) { - // fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); - // } + facetIndex++; + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId for facet: " + facetName); - // } + for (String facetName : facet) { + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // objectIdsToStore.add(sourceObjectId); - - // 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 for facet " - // + facetName - // + ". Expected: true, Got: " - // + sourceCustomProperty6); - // } + Map copiedAttachmentMetadata = + targetAttachmentsMetadata.stream() + .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } - // } + if (copiedAttachmentMetadata == null) { + fail( + "Could not find the copied attachment with file in target entity for facet: " + + facetName); + } - // int startIndex = sourceObjectIds.size(); - // sourceObjectIds.addAll(objectIdsToStore); + Boolean copiedCustomProperty6 = + copiedAttachmentMetadata.get("customProperty6") != null + ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + : null; + Integer copiedCustomProperty2 = + copiedAttachmentMetadata.get("customProperty2") != null + ? (Integer) copiedAttachmentMetadata.get("customProperty2") + : null; + + if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + fail( + "DocumentInfoRecordBoolean was not properly copied for facet " + + facetName + + ". Expected: true, Got: " + + copiedCustomProperty6); + } - // int facetIndex = 0; - // for (String facetName : facet) { - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // String copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment from target entity for facet: " + facetName); + } else { + testStatus = true; + } + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity for facet: " + facetName); - // } + if (!testStatus) { + fail( + "Could not verify that all secondary properties were copied from source to target attachment for all facets"); + } + } - // // Fetch copied attachment IDs from target draft - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity for facet: " + facetName); - // } + @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 across multiple facets"); + Boolean testStatus = false; - // facetIndex++; - // } + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity"); + } - // for (String facetName : facet) { - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample2.pdf").getFile()); + + String notesValue = "This attachment has both notes and secondary properties for testing"; + MediaType mediaType = MediaType.parse("application/json"); + Integer customProperty2Value = 99999; + List objectIdsToStore = new ArrayList<>(); + + for (String facetName : facet) { + 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); - // Map copiedAttachmentMetadata = - // targetAttachmentsMetadata.stream() - // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } - // if (copiedAttachmentMetadata == null) { - // fail( - // "Could not find the copied attachment with file in target entity for facet: " - // + facetName); - // } + String sourceAttachmentId = createResponse.get(1); - // Boolean copiedCustomProperty6 = - // copiedAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - // : null; - // Integer copiedCustomProperty2 = - // copiedAttachmentMetadata.get("customProperty2") != null - // ? (Integer) copiedAttachmentMetadata.get("customProperty2") - // : null; - - // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean was not properly copied for facet " - // + facetName - // + ". Expected: true, Got: " - // + copiedCustomProperty6); - // } + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + String updateNotesResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateNotesBody); - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update attachment notes field for facet: " + facetName); + } - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment from target entity for facet: " + facetName); - // } else { - // testStatus = 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 for facet: " + facetName); + } - // if (!testStatus) { - // fail( - // "Could not verify that all secondary properties were copied from source to target - // attachment for all facets"); - // } - // } + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); - // @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 across multiple facets"); - // 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 (!updateSecondaryPropertyResponse2.equals("Updated")) { + fail("Could not update attachment customProperty2 field for facet: " + facetName); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // Save source entity to persist attachments before fetching metadata and copying + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after creating attachments"); + } - // String notesValue = "This attachment has both notes and secondary properties for testing"; - // MediaType mediaType = MediaType.parse("application/json"); - // Integer customProperty2Value = 99999; - // List objectIdsToStore = new ArrayList<>(); + for (String facetName : facet) { + List> sourceAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - // for (String facetName : facet) { - // 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"); + Map sourceAttachmentMetadata = + sourceAttachmentsMetadata.stream() + .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + if (sourceAttachmentMetadata == null) { + fail("Could not find attachment with file in facet: " + facetName); + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId for facet: " + facetName); + } - // String sourceAttachmentId = createResponse.get(1); + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + objectIdsToStore.add(sourceObjectId); + + 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 for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + sourceNoteValue); + } - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + 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 for facet " + + facetName + + ". Expected: true, Got: " + + sourceCustomProperty6); + } - // String updateNotesResponse = - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // copyCustomSourceEntity, - // sourceAttachmentId, - // updateNotesBody); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } + } - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update attachment notes field for facet: " + facetName); - // } + int startIndex = sourceObjectIds.size(); + sourceObjectIds.addAll(objectIdsToStore); - // 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 for facet: " + - // facetName); - // } + int facetIndex = 0; + for (String facetName : facet) { + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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 for facet: " + facetName); - // } - // } + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - // // Save source entity to persist attachments before fetching metadata and copying - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after creating attachments"); - // } + String copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // for (String facetName : facet) { - // List> sourceAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity for facet: " + facetName); + } - // Map sourceAttachmentMetadata = - // sourceAttachmentsMetadata.stream() - // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity for facet: " + facetName); + } - // if (sourceAttachmentMetadata == null) { - // fail("Could not find attachment with file in facet: " + facetName); - // } + facetIndex++; + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId for facet: " + facetName); - // } + for (String facetName : facet) { + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // objectIdsToStore.add(sourceObjectId); - - // 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 for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + sourceNoteValue); - // } + Map copiedAttachmentMetadata = + targetAttachmentsMetadata.stream() + .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // 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 for facet " - // + facetName - // + ". Expected: true, Got: " - // + sourceCustomProperty6); - // } + if (copiedAttachmentMetadata == null) { + fail( + "Could not find the copied attachment with file in target entity for facet: " + + facetName); + } - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } - // } + String copiedNoteValue = + copiedAttachmentMetadata.get("note") != null + ? copiedAttachmentMetadata.get("note").toString() + : null; + + if (!notesValue.equals(copiedNoteValue)) { + fail( + "Notes field was not properly copied for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + copiedNoteValue); + } - // int startIndex = sourceObjectIds.size(); - // sourceObjectIds.addAll(objectIdsToStore); + Boolean copiedCustomProperty6 = + copiedAttachmentMetadata.get("customProperty6") != null + ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + : null; + Integer copiedCustomProperty2 = + copiedAttachmentMetadata.get("customProperty2") != null + ? (Integer) copiedAttachmentMetadata.get("customProperty2") + : null; + + if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + fail( + "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " + + facetName + + ". Expected: true, Got: " + + copiedCustomProperty6); + } + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // int facetIndex = 0; - // for (String facetName : facet) { - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment from target entity for facet: " + facetName); + } else { + testStatus = true; + } + } + api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); + api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); + if (!testStatus) { + fail( + "Could not verify that notes field and all secondary properties were copied from source to target attachment for all facets"); + } + } - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + @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<>(); + for (int i = 0; i < 3; i++) { + attachments.add(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"), "sample3.pdf"); + Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); + File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + copyAttachmentSourceEntity, + srvpath, + postData, + file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + } + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); + attachmentsMetadata.add(fetchAttachmentMetadataResponse); + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } + } - // String copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + sourceObjectIds.clear(); + for (Map metadata : attachmentsMetadata) { + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + if (sourceObjectIds.size() == 6) { + String copyResponse; + int i = 0; + for (String facetName : facet) { + if (i != 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } + List currentFacetObjectIds = + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + if (currentFacetObjectIds.size() != 2) { + fail("Not enough object IDs to copy attachments for facet: " + facet); + } + copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, currentFacetObjectIds); + i += 2; + if (copyResponse.equals("Attachments copied successfully")) { + // Fetch copied attachment IDs from target draft + List> copiedMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + List copiedAttachmentIds = + copiedMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + 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"); + } + } + } + } 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"); + } + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity for facet: " + facetName); - // } + @Test + @Order(41) + void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { + System.out.println("Test (41): Copy attachments from one entity to another new entity"); + 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")) { + if (sourceObjectIds.size() == 6) { + int i = 0; + for (String facetName : facet) { + List currentFacetObjectIds = + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + currentFacetObjectIds.add("incorrectObjectId"); + if (currentFacetObjectIds.size() != 3) { + fail("Not enough object IDs to copy attachments for facet: " + facet); + } + try { + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error"); + } catch (IOException e) { + i += 2; + } + } + 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 saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity for facet: " + facetName); - // } + @Test + @Order(42) + void testCreateLinkSuccess() throws IOException { + System.out.println("Test (42): Create link in entity"); + List attachments = new ArrayList<>(); - // facetIndex++; - // } + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // for (String facetName : facet) { - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + for (String facetName : facet) { + 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")) { + fail("Could not create links for facet : " + facetName + createLinkResponse1); + } + } - // Map copiedAttachmentMetadata = - // targetAttachmentsMetadata.stream() - // .filter(attachment -> "sample2.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 for facet: " - // + facetName); - // } + for (String facetName : facet) { + 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); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open created link in facet : " + facetName); + } + } + } + } - // String copiedNoteValue = - // copiedAttachmentMetadata.get("note") != null - // ? copiedAttachmentMetadata.get("note").toString() - // : null; - - // if (!notesValue.equals(copiedNoteValue)) { - // fail( - // "Notes field was not properly copied for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + copiedNoteValue); - // } + @Test + @Order(43) + void testCreateLinkDifferentEntity() throws IOException { + System.out.println("Test (43): Create link with same name in different entity"); - // Boolean copiedCustomProperty6 = - // copiedAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - // : null; - // Integer copiedCustomProperty2 = - // copiedAttachmentMetadata.get("customProperty2") != null - // ? (Integer) copiedAttachmentMetadata.get("customProperty2") - // : null; - - // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " - // + facetName - // + ". Expected: true, Got: " - // + copiedCustomProperty6); - // } - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + String createLinkDifferentEntity = + api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkDifferentEntity.equals("Could not edit entity")) { + fail("Could not create entity"); + } - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment from target entity for facet: " + facetName); - // } else { - // testStatus = true; - // } - // } - // api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); - // api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); - // if (!testStatus) { - // fail( - // "Could not verify that notes field and all secondary properties were copied from source - // to target attachment for all facets"); - // } - // } + String linkName = "sample"; + String linkUrl = "https://example.com"; + for (String facetName : facet) { + 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"); + } + } - // @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<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(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"), "sample3.pdf"); - // Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); - // File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // copyAttachmentSourceEntity, - // srvpath, - // postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // } - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); - // attachmentsMetadata.add(fetchAttachmentMetadataResponse); - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } - // } + String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkDifferentEntity); + if (!response.equals("Saved")) { + fail("Could not save 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"); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - // if (sourceObjectIds.size() == 6) { - // String copyResponse; - // int i = 0; - // for (String facetName : facet) { - // if (i != 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } - // List currentFacetObjectIds = - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - // if (currentFacetObjectIds.size() != 2) { - // fail("Not enough object IDs to copy attachments for facet: " + facet); - // } - // copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, - // currentFacetObjectIds); - // i += 2; - // if (copyResponse.equals("Attachments copied successfully")) { - // // Fetch copied attachment IDs from target draft - // List> copiedMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // List copiedAttachmentIds = - // copiedMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // 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"); - // } - // } - // } - // } 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"); - // } - // } + response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // @Test - // @Order(41) - // void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { - // System.out.println("Test (41): Copy attachments from one entity to another new entity"); - // 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")) { - // if (sourceObjectIds.size() == 6) { - // int i = 0; - // for (String facetName : facet) { - // List currentFacetObjectIds = - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - // currentFacetObjectIds.add("incorrectObjectId"); - // if (currentFacetObjectIds.size() != 3) { - // fail("Not enough object IDs to copy attachments for facet: " + facet); - // } - // try { - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error"); - // } catch (IOException e) { - // i += 2; - // } - // } - // 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"); - // } - // } + @Test + @Order(44) + void testCreateLinkFailure() throws IOException { + System.out.println("Test (41): Create link fails due to invalid URL and name"); + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (editEntityResponse.equals("Could not edit entity")) { + fail("Could not edit entity"); + } + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "example.com"; + try { + String response = + 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 < 6; i++) { + api.createLink( + appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + linkUrl); + } + System.out.println("Created 5 links in facet: " + facetName); + if (!facetName.equals("footnotes")) { + 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); + if (facetName.equals("references")) { + assertEquals("Cannot upload more than 5 attachments.", errorMessage); + } else if (facetName.equals("attachments")) { + assertEquals("Cannot upload more than 4 attachments.", errorMessage); + } + } + } - // @Test - // @Order(42) - // void testCreateLinkSuccess() throws IOException { - // System.out.println("Test (42): Create link in entity"); - // List attachments = new ArrayList<>(); + String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!response.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"); - // } + response = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // for (String facetName : facet) { - // 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")) { - // fail("Could not create links for facet : " + facetName + createLinkResponse1); - // } - // } + @Test + @Order(45) + void testCreateLinkNoSDMRoles() throws IOException { + System.out.println("Test (42): Create link fails due to no SDM roles assigned"); - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + String createLinkEntityNoSDMRoles = + apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { + fail("Could not create entity"); + } - // for (String facetName : facet) { - // 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); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open created link in facet : " + facetName); - // } - // } - // } - // } + for (String facetName : facet) { + 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); + } + } - // @Test - // @Order(43) - // void testCreateLinkDifferentEntity() throws IOException { - // System.out.println("Test (43): Create link with same name in different entity"); + String response = + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); + if (!response.equals("Saved")) { + fail("Could not save entity"); + } - // String createLinkDifferentEntity = - // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkDifferentEntity.equals("Could not edit entity")) { - // fail("Could not create entity"); - // } + response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // String linkName = "sample"; - // String linkUrl = "https://example.com"; - // for (String facetName : facet) { - // 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"); - // } - // } + @Test + @Order(46) + void testDeleteLink() throws IOException { + System.out.println("Test (43): Delete link in entity"); + List> attachments = new ArrayList<>(); - // String response = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkDifferentEntity); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } + String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + for (String facetName : facet) { + 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 for facet : " + facetName); + } + } - // @Test - // @Order(44) - // void testCreateLinkFailure() throws IOException { - // System.out.println("Test (41): Create link fails due to invalid URL and name"); - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (editEntityResponse.equals("Could not edit entity")) { - // fail("Could not edit entity"); - // } - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "example.com"; - // try { - // String response = - // 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 < 6; i++) { - // api.createLink( - // appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + - // linkUrl); - // } - // System.out.println("Created 5 links in facet: " + facetName); - // if (!facetName.equals("footnotes")) { - // 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); - // if (facetName.equals("references")) { - // assertEquals("Cannot upload more than 5 attachments.", errorMessage); - // } else if (facetName.equals("attachments")) { - // assertEquals("Cannot upload more than 4 attachments.", errorMessage); - // } - // } - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } - // response = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // @Test - // @Order(45) - // void testCreateLinkNoSDMRoles() throws IOException { - // System.out.println("Test (42): Create link fails due to no SDM roles assigned"); + int index = 0; + for (String facetName : facet) { + String deleteLinkResponse = + api.deleteAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); + System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + if (!deleteLinkResponse.equals("Deleted")) { + fail("Could not delete created link"); + } + index += 1; + } - // String createLinkEntityNoSDMRoles = - // apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { - // fail("Could not create entity"); - // } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // for (String facetName : facet) { - // 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); - // } - // } + index = 0; + attachments.clear(); + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + System.out.println( + "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + if (attachments.get(index).size() != 0) { + fail("Link wasn't deleted"); + } + index += 1; + } - // String response = - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } + String response = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (44): Rename link in entity"); + List> attachments = new ArrayList<>(); - // @Test - // @Order(46) - // void testDeleteLink() throws IOException { - // System.out.println("Test (43): Delete link in entity"); - // List> attachments = new ArrayList<>(); + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + for (String facetName : facet) { + 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"); + } + } - // for (String facetName : facet) { - // 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 for facet : " + facetName); - // } - // } + 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"); - // } + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } - // for (String facetName : facet) { - // attachments.add( - // 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 editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + int index = 0; + for (String facetName : facet) { + successfullyRenamedAttachments.add(attachments.get(index).get(0)); + String renameLinkResponse = + api.renameAttachment( + appUrl, + entityName, + facetName, + createLinkEntity, + attachments.get(index).get(0), + "sampleRenamed"); + if (!renameLinkResponse.equals("Renamed")) { + fail("Could not Renamed created link"); + } + index += 1; + } - // int index = 0; - // for (String facetName : facet) { - // String deleteLinkResponse = - // api.deleteAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); - // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - // if (!deleteLinkResponse.equals("Deleted")) { - // fail("Could not delete created link"); - // } - // index += 1; - // } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + @Test + @Order(48) + void testRenameLinkDuplicate() throws IOException { + System.out.println("Test (45): 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"); + } - // index = 0; - // attachments.clear(); - // for (String facetName : facet) { - // attachments.add( - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // System.out.println( - // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - // if (attachments.get(index).size() != 0) { - // fail("Link wasn't deleted"); - // } - // index += 1; - // } + int index = 0; + for (String facetName : facet) { + 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 response = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (saveResponse.equals("Could not save entity")) { + fail("Could not save entity"); + } - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (44): Rename link in entity"); - // List> attachments = new ArrayList<>(); + index = 0; + List facetAttachments; + for (String facetName : facet) { + int lambdaIndex = index; + facetAttachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .filter( + item -> + !successfullyRenamedAttachments + .get(lambdaIndex) + .equals(item.get("ID"))) // skip unwanted filename + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + index += 1; + attachments.add(facetAttachments.get(0)); + } - // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + System.out.println("Attachments to be renamed: " + attachments); + String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!response.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // for (String facetName : facet) { - // 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"); - // } - // } + index = 0; + for (String facetName : facet) { + api.renameAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(index), "sampleRenamed"); + index += 1; + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + String saveError = 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: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + ObjectMapper mapper = new ObjectMapper(); + assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); - // for (String facetName : facet) { - // attachments.add( - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // } + String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); + if (!deleteEntityResponse.equals("Entity Draft Deleted")) { + fail("Entity draft not deleted"); + } + } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + @Test + @Order(49) + void testRenameLinkUnsupportedCharacters() throws IOException { + System.out.println( + "Test (46): Rename link in entity fails due to unsupported characters in name"); + List> attachments = new ArrayList<>(); - // int index = 0; - // for (String facetName : facet) { - // successfullyRenamedAttachments.add(attachments.get(index).get(0)); - // String renameLinkResponse = - // api.renameAttachment( - // appUrl, - // entityName, - // facetName, - // createLinkEntity, - // attachments.get(index).get(0), - // "sampleRenamed"); - // if (!renameLinkResponse.equals("Renamed")) { - // fail("Could not Renamed created link"); - // } - // index += 1; - // } + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } - // } + String linkName = "sample2"; + String linkUrl = "https://www.example.com"; - // @Test - // @Order(48) - // void testRenameLinkDuplicate() throws IOException { - // System.out.println("Test (45): 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"); - // } + for (String facetName : facet) { + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } + } - // int index = 0; - // for (String facetName : facet) { - // 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 saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (saveResponse.equals("Could not save entity")) { - // fail("Could not save entity"); - // } + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } - // index = 0; - // List facetAttachments; - // for (String facetName : facet) { - // int lambdaIndex = index; - // facetAttachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .filter( - // item -> - // !successfullyRenamedAttachments - // .get(lambdaIndex) - // .equals(item.get("ID"))) // skip unwanted filename - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // index += 1; - // attachments.add(facetAttachments.get(0)); - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // System.out.println("Attachments to be renamed: " + attachments); - // String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!response.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + int index = 0; + for (String facetName : facet) { + api.renameAttachment( + appUrl, + entityName, + facetName, + createLinkEntity, + attachments.get(index).get(0), + "sampleRenamed//"); + index += 1; + } - // index = 0; - // for (String facetName : facet) { - // api.renameAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(index), - // "sampleRenamed"); - // index += 1; - // } + String error = + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + String expectedError = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + ObjectMapper mapper = new ObjectMapper(); + assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); - // String saveError = 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: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named - // \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An - // object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: - // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // 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"); - // } - // } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!deleteEntityResponse.equals("Entity Deleted")) { + fail("Entity draft not deleted"); + } + } - // @Test - // @Order(49) - // void testRenameLinkUnsupportedCharacters() throws IOException { - // System.out.println( - // "Test (46): Rename link in entity fails due to unsupported characters in name"); - // List> attachments = new ArrayList<>(); + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (47): Edit existing link in entity"); + List> attachmentsPerFacet = new ArrayList<>(); - // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (editLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // String linkName = "sample2"; - // String linkUrl = "https://www.example.com"; + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facetName); + } + } - // for (String facetName : facet) { - // 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // 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"); + } - // for (String facetName : facet) { - // attachments.add( - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // } + for (String facetName : facet) { + List attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).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"); - // } + if (attachments.isEmpty()) { + fail("Could not find link in facet: " + facetName); + } + attachmentsPerFacet.add(attachments); + } - // int index = 0; - // for (String facetName : facet) { - // api.renameAttachment( - // appUrl, - // entityName, - // facetName, - // createLinkEntity, - // attachments.get(index).get(0), - // "sampleRenamed//"); - // index += 1; - // } + int index = 0; + for (String facetName : facet) { + String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); + } + index++; + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + + int verificationIndex = 0; + for (String facetName : facet) { + List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); + for (String attachmentId : attachmentsInFacet) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open edited link " + attachmentId + " in facet: " + facetName); + } + } + verificationIndex++; + } + } - // String error = - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // String expectedError = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" - // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // ObjectMapper mapper = new ObjectMapper(); - // assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); - - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!deleteEntityResponse.equals("Entity Deleted")) { - // fail("Entity draft not deleted"); - // } - // } + @Test + @Order(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (48): Edit existing link with invalid url"); + List> attachmentsPerFacet = new ArrayList<>(); + + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (47): Edit existing link in entity"); - // List> attachmentsPerFacet = new ArrayList<>(); + for (String facetName : facet) { + List attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (editLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + if (attachments.isEmpty()) { + fail("Could not edit link in facet: " + facetName); + } + attachmentsPerFacet.add(attachments); + } - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facetName); - // } - // } + int index = 0; + for (String facetName : facet) { + try { + String linkId = attachmentsPerFacet.get(index).get(0); + String updatedUrl = "https://editedexample"; + index++; + String editLinkResponse = + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + System.out.println("response " + editLinkResponse); + fail("Edit link did not throw an error for invalid url 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("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); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + @Test + @Order(52) + void testEditLinkFailureEmptyURL() throws IOException { + System.out.println("Test (49): Edit existing link with an empty url"); + List> attachmentsPerFacet = new ArrayList<>(); + + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + for (String facetName : facet) { + List attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // for (String facetName : facet) { - // List 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 in facet: " + facetName); + } + attachmentsPerFacet.add(attachments); + } - // if (attachments.isEmpty()) { - // fail("Could not find link in facet: " + facetName); - // } - // attachmentsPerFacet.add(attachments); - // } + int index = 0; + for (String facetName : facet) { + try { + String linkId = attachmentsPerFacet.get(index).get(0); + String updatedUrl = ""; + index++; + + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + fail("Edit link did not throw an error for empty url 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"); + String expected = "Provide the missing value."; + assertEquals("409008", errorCode); + assertEquals(expected, errorMessage); + } + } + api.deleteEntity(appUrl, entityName, editLinkEntity); + } - // int index = 0; - // for (String facetName : facet) { - // String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); - // } - // index++; - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - - // int verificationIndex = 0; - // for (String facetName : facet) { - // List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); - // for (String attachmentId : attachmentsInFacet) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open edited link " + attachmentId + " in facet: " + facetName); - // } - // } - // verificationIndex++; - // } - // } + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); - // @Test - // @Order(51) - // void testEditLinkFailureInvalidURL() throws IOException { - // System.out.println("Test (48): Edit existing link with invalid url"); - // List> attachmentsPerFacet = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + Boolean testStatus = false; - // for (String facetName : facet) { - // List attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (editLinkEntity.equals("Could not create entity")) { + fail("Could not edit entity"); + } - // if (attachments.isEmpty()) { - // fail("Could not edit link in facet: " + facetName); - // } - // attachmentsPerFacet.add(attachments); - // } + for (String facetName : facet) { + String linkName = "sampleNoRole_" + facetName; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } - // int index = 0; - // for (String facetName : facet) { - // try { - // String linkId = attachmentsPerFacet.get(index).get(0); - // String updatedUrl = "https://editedexample"; - // index++; - // String editLinkResponse = - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // System.out.println("response " + editLinkResponse); - // fail("Edit link did not throw an error for invalid url 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("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); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // @Test - // @Order(52) - // void testEditLinkFailureEmptyURL() throws IOException { - // System.out.println("Test (49): Edit existing link with an empty url"); - // List> attachmentsPerFacet = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + String editEntityResponse = + apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // for (String facetName : facet) { - // List attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + for (String facetName : facet) { + List 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 in facet: " + facetName); - // } - // attachmentsPerFacet.add(attachments); - // } + if (attachments.isEmpty()) { + fail("Could not find link in facet: " + facetName); + } - // int index = 0; - // for (String facetName : facet) { - // try { - // String linkId = attachmentsPerFacet.get(index).get(0); - // String updatedUrl = ""; - // index++; - - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // fail("Edit link did not throw an error for empty url 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"); - // String expected = "Provide the missing value."; - // assertEquals("409008", errorCode); - // assertEquals(expected, errorMessage); - // } - // } - // api.deleteEntity(appUrl, entityName, editLinkEntity); - // } + String linkId = attachments.get(0); + String updatedUrl = "https://www.editedexample.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; + } + } + api.deleteEntity(appUrl, entityName, editLinkEntity); + if (!testStatus) { + fail("Link got edited without SDM roles"); + } + } - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); + @Test + @Order(54) + void testCopyLinkSuccessNewEntity() throws IOException { + System.out.println("Test (51): Copy link from one entity to another new entity"); + List> attachmentsByFacet = new ArrayList<>(); + String linkUrl = "https://www.example.com"; + for (int i = 0; i < facet.length; i++) { + attachmentsByFacet.add(new ArrayList<>()); + } - // Boolean testStatus = false; + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (editLinkEntity.equals("Could not create entity")) { - // fail("Could not edit entity"); - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entities"); + } - // for (String facetName : facet) { - // String linkName = "sampleNoRole_" + facetName; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // String editEntityResponse = - // apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + sourceObjectIds.clear(); + for (int i = 0; i < facet.length; i++) { + List objectIds = + api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + sourceObjectIds.addAll(objectIds); + } - // for (String facetName : facet) { - // List attachments = - // apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + if (sourceObjectIds.size() != facet.length) { + fail( + "Could not fetch object Ids for all attachments. Expected: " + + facet.length + + ", Found: " + + sourceObjectIds.size()); + } - // if (attachments.isEmpty()) { - // fail("Could not find link in facet: " + facetName); - // } + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft for facet: " + facetName); + } - // String linkId = attachments.get(0); - // String updatedUrl = "https://www.editedexample.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; - // } - // } - // api.deleteEntity(appUrl, entityName, editLinkEntity); - // if (!testStatus) { - // fail("Link got edited without SDM roles"); - // } - // } + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // @Test - // @Order(54) - // void testCopyLinkSuccessNewEntity() throws IOException { - // System.out.println("Test (51): Copy link from one entity to another new entity"); - // List> attachmentsByFacet = new ArrayList<>(); - // String linkUrl = "https://www.example.com"; - // for (int i = 0; i < facet.length; i++) { - // attachmentsByFacet.add(new ArrayList<>()); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity after copying attachments for facet " + facetName); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entities"); - // } + List> attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch in facet " + facetName); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + System.out.println("Attachment type and URL validated for facet " + facetName); + + List attachments = + attachmentsMetadata.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + for (String attachment : attachments) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open copied link in facet: " + facetName); + } + } - // sourceObjectIds.clear(); - // for (int i = 0; i < facet.length; i++) { - // List objectIds = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // sourceObjectIds.addAll(objectIds); - // } + objectIdIndex++; + } - // if (sourceObjectIds.size() != facet.length) { - // fail( - // "Could not fetch object Ids for all attachments. Expected: " - // + facet.length - // + ", Found: " - // + sourceObjectIds.size()); - // } + String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + if (!deleteTargetResponse.equals("Entity Deleted")) { + fail("Could not delete target entity"); + } + } - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft for facet: " + facetName); - // } + @Test + @Order(55) + void testCopyLinkUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (52): Copy invalid type of link from one entity to another new entity"); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + if (!editResponse.equals("Entity in draft mode") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not edit source entity or create target entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - // } + sourceObjectIds.add("incorrectObjectId"); - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity after copying attachments for facet " + facetName); - // } + for (String facetName : facet) { + try { + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error for facet: " + facetName); + } catch (IOException e) { + System.out.println("Successfully caught expected error for facet: " + facetName); + } + } + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + } - // List> attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + @Test + @Order(56) + void testCopyLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println("Test (53): Copy link from a new entity to an existing target entity"); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + List> attachmentsByFacet = new ArrayList<>(); + String linkUrl = "https://www.example.com"; + for (int i = 0; i < facet.length; i++) { + attachmentsByFacet.add(new ArrayList<>()); + } - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch in facet " + facetName); + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - // System.out.println("Attachment type and URL validated for facet " + facetName); + for (int i = 0; i < facet.length; i++) { + String linkName = "newsample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } - // List attachments = - // attachmentsMetadata.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // for (String attachment : attachments) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open copied link in facet: " + facetName); - // } - // } + sourceObjectIds.clear(); + for (String facetName : facet) { + List objectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + sourceObjectIds.addAll(objectIds); + } - // objectIdIndex++; - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Ids for any attachments"); + } - // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // if (!deleteTargetResponse.equals("Entity Deleted")) { - // fail("Could not delete target entity"); - // } - // } + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft for facet: " + facetName); + } - // @Test - // @Order(55) - // void testCopyLinkUnsuccessfulNewEntity() throws IOException { - // System.out.println( - // "Test (52): Copy invalid type of link from one entity to another new entity"); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // if (!editResponse.equals("Entity in draft mode") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not edit source entity or create target entity"); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + } - // sourceObjectIds.add("incorrectObjectId"); + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity after copying attachments for facet " + facetName); + } - // for (String facetName : facet) { - // try { - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error for facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Successfully caught expected error for facet: " + facetName); - // } - // } - // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // } + List> attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // @Test - // @Order(56) - // void testCopyLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println("Test (53): Copy link from a new entity to an existing target entity"); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // List> attachmentsByFacet = new ArrayList<>(); - // String linkUrl = "https://www.example.com"; - // for (int i = 0; i < facet.length; i++) { - // attachmentsByFacet.add(new ArrayList<>()); - // } + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch in facet " + facetName); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + System.out.println("Attachment type and URL validated for facet " + facetName); + + List attachments = + attachmentsMetadata.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + for (String attachment : attachments) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open copied link in facet: " + facetName); + } + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + objectIdIndex++; + } - // for (int i = 0; i < facet.length; i++) { - // String linkName = "newsample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + @Test + @Order(57) + void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println( + "Test (54): Copy invalid type of link from new entity to existing target entity"); + String linkUrl = "https://www.example.com"; - // sourceObjectIds.clear(); - // for (String facetName : facet) { - // List objectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // sourceObjectIds.addAll(objectIds); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Ids for any attachments"); - // } + for (int i = 0; i < facet.length; i++) { + String linkName = "newsample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit entities"); + } + for (String facetName : facet) { + List sourceObjectIds = new ArrayList<>(); + sourceObjectIds.add("incorrectObjectId"); + try { + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error for facet: " + facetName); + } catch (IOException e) { + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + } - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft for facet: " + facetName); - // } + @Test + @Order(58) + void testCopyLinkSuccessNewEntityDraft() throws IOException { + System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); + List> attachmentsByFacet = new ArrayList<>(); + String linkUrl = "https://www.example.com"; + for (int i = 0; i < facet.length; i++) { + attachmentsByFacet.add(new ArrayList<>()); + } - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entities"); + } - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity after copying attachments for facet " + facetName); - // } + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } - // List> attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + sourceObjectIds.clear(); + for (int i = 0; i < facet.length; i++) { + List objectIds = + api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + sourceObjectIds.addAll(objectIds); + } - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch in facet " + facetName); + if (sourceObjectIds.size() != facet.length) { + fail( + "Could not fetch object Ids for all attachments. Expected: " + + facet.length + + ", Found: " + + sourceObjectIds.size()); + } - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - // System.out.println("Attachment type and URL validated for facet " + facetName); + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft for facet: " + facetName); + } - // List attachments = - // attachmentsMetadata.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // for (String attachment : attachments) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open copied link in facet: " + facetName); - // } - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + } - // objectIdIndex++; - // } + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity after copying attachments for facet " + facetName); + } - // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // } + List> attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // @Test - // @Order(57) - // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println( - // "Test (54): Copy invalid type of link from new entity to existing target entity"); - // String linkUrl = "https://www.example.com"; + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch in facet " + facetName); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + System.out.println("Attachment type and URL validated for facet " + facetName); + + List attachments = + attachmentsMetadata.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + for (String attachment : attachments) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open copied link in facet: " + facetName); + } + } - // for (int i = 0; i < facet.length; i++) { - // String linkName = "newsample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit entities"); - // } - // for (String facetName : facet) { - // List sourceObjectIds = new ArrayList<>(); - // sourceObjectIds.add("incorrectObjectId"); - // try { - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error for facet: " + facetName); - // } catch (IOException e) { - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // } + objectIdIndex++; + } - // @Test - // @Order(58) - // void testCopyLinkSuccessNewEntityDraft() throws IOException { - // System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); - // List> attachmentsByFacet = new ArrayList<>(); - // String linkUrl = "https://www.example.com"; - // for (int i = 0; i < facet.length; i++) { - // attachmentsByFacet.add(new ArrayList<>()); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @Test + @Order(59) + void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { + System.out.println( + "Test (56): Copy attachments from one entity to another new entity draft mode"); + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(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(); - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entities"); - // } + for (int i = 0; i < facet.length; i++) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + copyAttachmentSourceEntity, + srvpath, + postData, + file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + } + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facet[i], 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"); + } + } - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } + if (sourceObjectIds.size() == 6) { + String copyResponse; + int i = 0; + for (String facetName : facet) { + if (i != 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } + copyResponse = + api.copyAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + i += 2; + if (copyResponse.equals("Attachments copied successfully")) { + // Fetch copied attachment IDs from target draft + List> copiedMetadataResponse = + api.fetchEntityMetadataDraft( + appUrl, entityName, facetName, copyAttachmentTargetEntity); + List copiedAttachmentIds = + copiedMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (saveEntityResponse.equals("Saved")) { + List> fetchEntityMetadataResponse; + fetchEntityMetadataResponse = + api.fetchEntityMetadataDraft( + 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); + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + @Test + @Order(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println( + "Test (60): View changelog for newly created attachment in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // Create a new entity for changelog test + changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); + assertNotEquals("Could not create entity", changelogEntityID[i]); + + // 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"); + + // Create attachment + Map postData = new HashMap<>(); + postData.put("up__ID", changelogEntityID[i]); + postData.put("mimeType", "text/plain"); + 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[i], srvpath, postData, file); - // sourceObjectIds.clear(); - // for (int i = 0; i < facet.length; i++) { - // List objectIds = - // api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], - // copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // sourceObjectIds.addAll(objectIds); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + changelogAttachmentID[i] = createResponse.get(1); - // if (sourceObjectIds.size() != facet.length) { - // fail( - // "Could not fetch object Ids for all attachments. Expected: " - // + facet.length - // + ", Found: " - // + sourceObjectIds.size()); - // } + assertEquals("Attachment created", status, "Attachment should be created successfully"); + assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); + assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft for facet: " + facetName); - // } + // Fetch changelog for the newly created attachment + Map changelogResponse = + api.fetchChangelog( + appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - // } + // 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"); + } + } - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity after copying attachments for facet " + facetName); - // } + @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 in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // 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[i], + changelogAttachmentID[i], + 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[i], + changelogAttachmentID[i], + bodyInt); + assertEquals( + "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - // List> attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // Edit entity again to fetch changelog + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch in facet " + facetName); + // Fetch changelog after modifications + Map changelogResponse = + api.fetchChangelog( + appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - // System.out.println("Attachment type and URL validated for facet " + facetName); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // List attachments = - // attachmentsMetadata.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + // 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)"); - // for (String attachment : attachments) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open copied link in facet: " + facetName); - // } - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); - // objectIdIndex++; - // } + // 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)"); + + // Save the entity so test 62 can edit it + String saveResponseFinal = + api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); + } + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.deleteEntity(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 in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // Edit entity to put it in draft mode (entity was saved at end of test 61) + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + 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[i], + changelogAttachmentID[i], + newFileName); + assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); + + // Save entity after rename + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + + // Edit entity again and fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // Fetch changelog after rename + Map changelogAfterRename = + api.fetchChangelog( + appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + 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"); - // @Test - // @Order(59) - // void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { - // System.out.println( - // "Test (56): Copy attachments from one entity to another new entity draft mode"); - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(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"); + @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[i]); + } + } - // sourceObjectIds.clear(); - - // for (int i = 0; i < facet.length; i++) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // copyAttachmentSourceEntity, - // srvpath, - // postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // } - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facet[i], 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(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 in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // 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.pdf").getFile()); + assertTrue(file.exists(), "Sample file should exist"); + + // 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, 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"); - // if (sourceObjectIds.size() == 6) { - // String copyResponse; - // int i = 0; - // for (String facetName : facet) { - // if (i != 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } - // copyResponse = - // api.copyAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - // i += 2; - // if (copyResponse.equals("Attachments copied successfully")) { - // // Fetch copied attachment IDs from target draft - // List> copiedMetadataResponse = - // api.fetchEntityMetadataDraft( - // appUrl, entityName, facetName, copyAttachmentTargetEntity); - // List copiedAttachmentIds = - // copiedMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (saveEntityResponse.equals("Saved")) { - // List> fetchEntityMetadataResponse; - // fetchEntityMetadataResponse = - // api.fetchEntityMetadataDraft( - // 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); - // } + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println( - // "Test (60): View changelog for newly created attachment in all three facets"); + // 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"); - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; + // Fetch changelog after initial save + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // // Create a new entity for changelog test - // changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); - // assertNotEquals("Could not create entity", changelogEntityID[i]); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // // 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"); + // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + + // customProperty2) + assertEquals( + 3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); - // // Create attachment - // Map postData = new HashMap<>(); - // postData.put("up__ID", changelogEntityID[i]); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, changelogEntityID[i], srvpath, postData, file); - - // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - // String status = createResponse.get(0); - // changelogAttachmentID[i] = createResponse.get(1); - - // assertEquals("Attachment created", status, "Attachment should be created successfully"); - // assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); - // assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); - - // // Fetch changelog for the newly created attachment - // Map changelogResponse = - // api.fetchChangelog( - // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - // 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"); - // } - // } + // Save entity again without any modifications + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // @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 in all three facets"); - - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; - - // // 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[i], - // changelogAttachmentID[i], - // 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[i], - // changelogAttachmentID[i], - // bodyInt); - // assertEquals( - // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - - // // Save the entity - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - - // // Edit entity again to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after modifications - // Map changelogResponse = - // api.fetchChangelog( - // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - // 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)"); - - // // Save the entity so test 62 can edit it - // String saveResponseFinal = - // api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - // assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); - // } - // } + // 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"); - // @Test - // @Order(62) - // void testChangelogAfterRenamingAttachment() throws IOException { - // System.out.println( - // "Test (62): Rename attachment and verify changelog increases with rename entry in all - // three facets"); - - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; - - // // Edit entity to put it in draft mode (entity was saved at end of test 61) - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // 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[i], - // changelogAttachmentID[i], - // newFileName); - // assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); - - // // Save entity after rename - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - - // // Edit entity again and fetch changelog - // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after rename - // Map changelogAfterRename = - // api.fetchChangelog( - // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - // 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[i]); - // } - // } + // Fetch changelog after second save + Map changelogAfterSecondSave = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // @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 in all three facets"); + assertNotNull( + changelogAfterSecondSave, "Changelog response should not be null after second save"); - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; + // 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"); - // // 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); + @SuppressWarnings("unchecked") + List> changeLogsAfterSecondSave = + (List>) changelogAfterSecondSave.get("changeLogs"); + assertEquals( + 3, + changeLogsAfterSecondSave.size(), + "Should still have exactly 3 changelog entries after second save"); - // // 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"); + // Clean up the entity + api.deleteEntity(appUrl, entityName, newEntityID); + } + } - // // 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"); + @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 in all three facets"); - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; - // 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); - // } - // } + // 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(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 in all three facets"); + // 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"); - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; + // 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 a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(newEntityID, "Failed to create new entity"); - // assertNotEquals("Could not create entity", newEntityID); + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // // 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"); + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + String newAttachmentID = createResponse.get(1); - // // 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"); + 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"); - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); + // Save the entity immediately without any modifications + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // 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); - // } - // } + // 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"); - // @Test - // @Order(65) - // void testMoveAttachmentsWithSourceFacet() throws IOException { - // System.out.println( - // "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // Save entity again without modifying the attachment + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source 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"); - // 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())); + // Fetch changelog for the attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); - // 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"); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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 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); + } + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println( + "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } + for (int i = 0; i < facet.length; i++) { + 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 all objectIds from source entity"); - // } + 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity"); + } - // // Save target before move - // String saveTargetBeforeMoveTest65 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveTest65.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all attachments after move"); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after - // move"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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"); + // Save target before move + String saveTargetBeforeMoveTest65 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveTest65.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // 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> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + sourceAttachmentIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all 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"); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after move"); - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + @Test + @Order(66) + public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + System.out.println( + "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } + for (int i = 0; i < facet.length; i++) { + 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 all objectIds from source entity"); - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save 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"); - - // File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - // List targetCreateResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // srvpath, - // targetPostData, - // duplicateFile); - - // if (!targetCreateResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment on target entity"); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - - // int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target should have duplicate skipped, other attachments moved"); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // int expectedSourceCount = - // sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); - // assertEquals( - // expectedSourceCount, - // sourceMetadataAfterMove.size(), - // "Source should have duplicate attachment remaining"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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"); - // @Test - // @Order(67) - // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - // System.out.println( - // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + List targetCreateResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + srvpath, + targetPostData, + duplicateFile); + + if (!targetCreateResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment on target entity"); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // 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> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + 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"); + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target should have duplicate skipped, other attachments moved"); + + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + int expectedSourceCount = + sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); + assertEquals( + expectedSourceCount, + sourceMetadataAfterMove.size(), + "Source should have duplicate attachment remaining"); - // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update notes for attachment: " + attachmentId); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // Integer customProperty2Value = 54321; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], 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"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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); - // } - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); - // } + 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update notes for attachment: " + attachmentId); + } + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + Integer customProperty2Value = 54321; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + + for (String attachmentId : sourceAttachmentIds) { + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + if (!updateCustomPropertyResponse.equals("Updated")) { + fail("Could not update custom property for attachment: " + attachmentId); + } + } - // // Save target before move - // String saveTargetBeforeMoveTest67 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveTest67.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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); + } + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all attachments after move"); - - // 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, facet[i], moveTargetEntity, - // targetAttachmentId); - - // 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); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // 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); - // } - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); + // Save target before move + String saveTargetBeforeMoveTest67 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveTest67.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); + } - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // @Test - // @Order(68) - // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (68): Move valid attachments from Source Entity to Target Entity without - // sourceFacet"); + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + sourceAttachmentIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all attachments after move"); + + 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, facet[i], moveTargetEntity, targetAttachmentId); + + 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); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + 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); + } + } - // 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> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, sourceMetadataAfterMove.size(), "Source entity has no 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"); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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(68) + public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + System.out.println( + "Test (68): Move valid attachments from Source Entity to Target Entity without sourceFacet"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // 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()); - // } - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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 object IDs for all attachments"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + 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 target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // moveObjectIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all moved attachments"); - - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // String readResponse = - // api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read moved attachment from 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"); + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // moveObjectIds.size(), - // sourceMetadataAfterMove.size(), - // "Source entity should still have attachments in UI when sourceFacet is not specified"); - - // for (Map metadata : sourceMetadataAfterMove) { - // String objectId = (String) metadata.get("objectId"); - // assertTrue( - // moveObjectIds.contains(objectId), - // "Source entity should still show attachment with objectId: " + objectId); - // } + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + moveObjectIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all moved attachments"); + + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + String readResponse = + api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read moved attachment from 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"); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + moveObjectIds.size(), + sourceMetadataAfterMove.size(), + "Source entity should still have attachments in UI when sourceFacet is not specified"); + + for (Map metadata : sourceMetadataAfterMove) { + String objectId = (String) metadata.get("objectId"); + assertTrue( + moveObjectIds.contains(objectId), + "Source entity should still show attachment with objectId: " + objectId); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // 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(69) + public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + System.out.println( + "Test (69): Move attachments into existing Target Entity when duplicate exists 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"); + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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 saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // 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()); - // } - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + 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()); + } + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target 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"); - - // List createTargetResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // srvpath, - // targetPostData, - // files.get(0)); - // if (!createTargetResponse.get(0).equals("Attachment created")) { - // fail("Could not create duplicate attachment in target 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"); - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + List createTargetResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + srvpath, + targetPostData, + files.get(0)); + if (!createTargetResponse.get(0).equals("Attachment created")) { + fail("Could not create duplicate attachment in target entity"); + } - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int initialTargetCount = targetMetadataBeforeMove.size(); - - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int initialTargetCount = targetMetadataBeforeMove.size(); + + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // int nonDuplicateCount = moveObjectIds.size() - 1; - // int expectedTargetCount = initialTargetCount + nonDuplicateCount; + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target entity should have initial attachments plus non-duplicate moved attachments"); + int nonDuplicateCount = moveObjectIds.size() - 1; + int expectedTargetCount = initialTargetCount + nonDuplicateCount; - // assertTrue( - // targetMetadataAfterMove.size() > initialTargetCount, - // "Target should have more attachments after move (non-duplicates added)"); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have initial attachments plus non-duplicate moved attachments"); - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // moveObjectIds.size(), - // sourceMetadataAfterMove.size(), - // "Source entity should still have all attachments in UI when sourceFacet is not - // specified"); + assertTrue( + targetMetadataAfterMove.size() > initialTargetCount, + "Target should have more attachments after move (non-duplicates added)"); - // 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); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + moveObjectIds.size(), + sourceMetadataAfterMove.size(), + "Source entity should still have all attachments in UI when sourceFacet is not specified"); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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); + } - // @Test - // @Order(70) - // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - // throws Exception { - // System.out.println( - // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + @Test + @Order(70) + public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + throws Exception { + System.out.println( + "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - // 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())); + for (int i = 0; i < facet.length; i++) { + 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"); + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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 sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update notes for attachment: " + attachmentId); + } + } - // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update notes for attachment: " + attachmentId); - // } - // } + Integer customProperty2Value = 54321; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + + for (String attachmentId : sourceAttachmentIds) { + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + if (!updateCustomPropertyResponse.equals("Updated")) { + fail("Could not update custom property for attachment: " + attachmentId); + } + } - // Integer customProperty2Value = 54321; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); - // if (!updateCustomPropertyResponse.equals("Updated")) { - // fail("Could not update custom property for attachment: " + attachmentId); - // } - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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); + } + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + List> sourceMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not 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"); + } - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have " + expectedTargetCount + " attachments after move"); + + 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, facet[i], moveTargetEntity, targetAttachmentId); + + 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); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target entity should have " + expectedTargetCount + " attachments after move"); - - // 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, facet[i], moveTargetEntity, - // targetAttachmentId); - - // 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); - // } + 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 (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); - // } - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + sourceCountBeforeMove, + sourceMetadataAfterMove.size(), + "Source entity should still have " + + sourceCountBeforeMove + + " attachments (without sourceFacet)"); + + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // sourceCountBeforeMove, - // sourceMetadataAfterMove.size(), - // "Source entity should still have " - // + sourceCountBeforeMove - // + " attachments (without sourceFacet)"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + @Test + @Order(71) + public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + System.out.println( + "Test (71): Move attachments with invalid or undefined secondary properties"); - // @Test - // @Order(71) - // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - // System.out.println( - // "Test (71): Move attachments with invalid or undefined secondary properties"); + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // 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())); + String validAttachmentId = sourceAttachmentIds.get(0); + Integer validCustomProperty2Value = 12345; + RequestBody validPropertyBody = + RequestBody.create( + "{\"customProperty2\": " + validCustomProperty2Value + "}", + MediaType.parse("application/json")); + + String validPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, validPropertyBody); + if (!validPropertyResponse.equals("Updated")) { + fail("Could not update valid property for attachment: " + validAttachmentId); + } - // 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"); + String invalidAttachmentId = sourceAttachmentIds.get(1); + RequestBody invalidPropertyBody = + RequestBody.create( + "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); + + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, invalidPropertyBody); + + String undefinedAttachmentId = sourceAttachmentIds.get(2); + RequestBody undefinedPropertyBody = + RequestBody.create( + "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", + MediaType.parse("application/json")); + + api.updateSecondaryProperty( + appUrl, + entityName, + facet[i], + moveSourceEntity, + undefinedAttachmentId, + undefinedPropertyBody); + + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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); + } + } - // String validAttachmentId = sourceAttachmentIds.get(0); - // Integer validCustomProperty2Value = 12345; - // RequestBody validPropertyBody = - // RequestBody.create( - // "{\"customProperty2\": " + validCustomProperty2Value + "}", - // MediaType.parse("application/json")); - - // String validPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, - // validPropertyBody); - // if (!validPropertyResponse.equals("Updated")) { - // fail("Could not update valid property for attachment: " + validAttachmentId); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // String invalidAttachmentId = sourceAttachmentIds.get(1); - // RequestBody invalidPropertyBody = - // RequestBody.create( - // "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); - - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, - // invalidPropertyBody); - - // String undefinedAttachmentId = sourceAttachmentIds.get(2); - // RequestBody undefinedPropertyBody = - // RequestBody.create( - // "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", - // MediaType.parse("application/json")); - - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facet[i], - // moveSourceEntity, - // undefinedAttachmentId, - // undefinedPropertyBody); - - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + List> sourceMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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); - // } - // } + 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 saveTargetBeforeMoveResponseTest72 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); + } - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // // Save target before move - // String saveTargetBeforeMoveResponseTest72 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); - // } + assertTrue( + targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + assertEquals( + sourceCountBeforeMove, + targetMetadataAfterMove.size(), + "All attachments should move (invalid properties are ignored)"); + + 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, facet[i], moveTargetEntity, targetAttachmentId); + + if (detailedMetadata.containsKey("customProperty2") + && detailedMetadata.get("customProperty2") != null) { + assertEquals( + validCustomProperty2Value, + detailedMetadata.get("customProperty2"), + "Valid customProperty2 should be preserved"); + } + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterMove.size(), + "Source entity should have no attachments after move with sourceFacet"); - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - - // assertTrue( - // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after - // move"); - // assertEquals( - // sourceCountBeforeMove, - // targetMetadataAfterMove.size(), - // "All attachments should move (invalid properties are ignored)"); - - // 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, facet[i], moveTargetEntity, - // targetAttachmentId); - - // if (detailedMetadata.containsKey("customProperty2") - // && detailedMetadata.get("customProperty2") != null) { - // assertEquals( - // validCustomProperty2Value, - // detailedMetadata.get("customProperty2"), - // "Valid customProperty2 should be preserved"); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterMove.size(), - // "Source entity should have no attachments after move with sourceFacet"); + @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"); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create 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"); + 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create 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); + } - // 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())); + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // 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"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // 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 editSourceResponse = + api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!editSourceResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity back to draft mode"); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + 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 saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // 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"); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertTrue( + targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + assertEquals( + sourceCountBeforeMove, + targetMetadataAfterMove.size(), + "Target should have " + sourceCountBeforeMove + " attachments after move"); + + 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()); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String saveSourceAfterMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceAfterMoveResponse.equals("Saved")) { + fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); + } - // // Save target before move - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], 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()); + } - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertTrue( - // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after - // move"); - // assertEquals( - // sourceCountBeforeMove, - // targetMetadataAfterMove.size(), - // "Target should have " + sourceCountBeforeMove + " attachments after move"); - - // 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()); - // } + @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"); - // String saveSourceAfterMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceAfterMoveResponse.equals("Saved")) { - // fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); - // } + for (int i = 0; i < facet.length; i++) { + 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, facet[i], 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()); - // } + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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"); - // @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"); + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in source entity"); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String attachmentId = createResponse.get(1); + assertNotNull(attachmentId, "Attachment ID should not be null"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + 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", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + List> metadataBeforeRename = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); + assertEquals( + "sample.txt", + metadataBeforeRename.get(0).get("fileName"), + "Original filename should be sample.txt"); + + String editSourceResponse = + api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!editSourceResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity to draft mode"); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in source entity"); - // } + String newFileName = "testEdited.txt"; + String renameResponse = + api.renameAttachment( + appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); + assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); - // String attachmentId = createResponse.get(1); - // assertNotNull(attachmentId, "Attachment ID should not be null"); + saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after rename: " + saveSourceResponse); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + List> metadataAfterRename = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); + assertEquals( + newFileName, + metadataAfterRename.get(0).get("fileName"), + "Filename should be updated to " + newFileName); + + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); + moveObjectIds.add(objectId); + + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> metadataBeforeRename = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); - // assertEquals( - // "sample.txt", - // metadataBeforeRename.get(0).get("fileName"), - // "Original filename should be sample.txt"); - - // String editSourceResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!editSourceResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity to draft mode"); - // } + // Save target before move + String saveTargetBeforeMoveResponseTest73 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); + } - // String newFileName = "testEdited.txt"; - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); - // assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after rename: " + saveSourceResponse); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); - // List> metadataAfterRename = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); - // assertEquals( - // newFileName, - // metadataAfterRename.get(0).get("fileName"), - // "Filename should be updated to " + newFileName); - - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterMove.size(), + "Source entity should have no attachments after move with sourceFacet"); - // // Save target before move - // String saveTargetBeforeMoveResponseTest73 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + @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"); - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterMove.size(), - // "Source entity should have no attachments after move with sourceFacet"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + for (int i = 0; i < facet.length; i++) { + 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"); + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // 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())); + int sourceCountInitial = sourceAttachmentIds.size(); + assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // 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"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity 1"); + } - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + 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"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult1 = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult1 == null) { + fail("Move operation from source to target 1 returned null result"); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + List> target1MetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertTrue( + target1MetadataAfterMove.size() > 0, + "Target entity 1 should have attachments after move"); + assertEquals( + sourceCountInitial, + target1MetadataAfterMove.size(), + "Target 1 should have " + sourceCountInitial + " attachments"); + + 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()); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity 1"); - // } + List> sourceMetadataAfterFirstMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterFirstMove.size(), + "Source entity should have no attachments after move to target 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"); - // } + String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity2.equals("Could not create entity")) { + fail("Could not create target entity 2"); + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult1 = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult1 == null) { - // fail("Move operation from source to target 1 returned null result"); - // } + // 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"); + } - // List> target1MetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertTrue( - // target1MetadataAfterMove.size() > 0, - // "Target entity 1 should have attachments after move"); - // assertEquals( - // sourceCountInitial, - // target1MetadataAfterMove.size(), - // "Target 1 should have " + sourceCountInitial + " attachments"); - - // 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()); - // } + List target1AttachmentIds = new ArrayList<>(); + for (Map metadata : target1MetadataAfterMove) { + String attachmentId = metadata.get("ID").toString(); + target1AttachmentIds.add(attachmentId); + } - // List> sourceMetadataAfterFirstMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterFirstMove.size(), - // "Source entity should have no attachments after move to target 1"); + moveObjectIds = new ArrayList<>(); + String target1FolderId = null; + for (String attachmentId : target1AttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + 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()); + } + } - // String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity2.equals("Could not create entity")) { - // fail("Could not create target entity 2"); - // } + assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); + + Map moveResult2 = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity2, + target1FolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult2 == null) { + fail("Move operation from target 1 to target 2 returned null result"); + } - // // 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"); - // } + List> target2MetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); + assertTrue( + target2MetadataAfterMove.size() > 0, + "Target entity 2 should have attachments after move"); + assertEquals( + sourceCountInitial, + target2MetadataAfterMove.size(), + "Target 2 should have " + sourceCountInitial + " attachments"); + + 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()); + } - // List target1AttachmentIds = new ArrayList<>(); - // for (Map metadata : target1MetadataAfterMove) { - // String attachmentId = metadata.get("ID").toString(); - // target1AttachmentIds.add(attachmentId); - // } + List> target1MetadataAfterSecondMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + 0, + target1MetadataAfterSecondMove.size(), + "Target entity 1 should have no attachments after move to target 2"); - // moveObjectIds = new ArrayList<>(); - // String target1FolderId = null; - // for (String attachmentId : target1AttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // 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()); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity2); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); - - // Map moveResult2 = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity2, - // target1FolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult2 == null) { - // fail("Move operation from target 1 to target 2 returned null result"); - // } + @Test + @Order(75) + public void testMoveAttachmentsWithoutSDMRole() throws Exception { + System.out.println("Test (75): Move attachments when user does not have SDM Role"); - // List> target2MetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); - // assertTrue( - // target2MetadataAfterMove.size() > 0, - // "Target entity 2 should have attachments after move"); - // assertEquals( - // sourceCountInitial, - // target2MetadataAfterMove.size(), - // "Target 2 should have " + sourceCountInitial + " attachments"); - - // 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()); - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // List> target1MetadataAfterSecondMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // 0, - // target1MetadataAfterSecondMove.size(), - // "Target entity 1 should have no attachments after move to target 2"); + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // api.deleteEntity(appUrl, entityName, moveTargetEntity2); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // @Test - // @Order(75) - // public void testMoveAttachmentsWithoutSDMRole() throws Exception { - // System.out.println("Test (75): Move attachments when user does not have SDM Role"); + int sourceCountInitial = sourceAttachmentIds.size(); + assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // for (int i = 0; i < facet.length; i++) { - // 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"); + } - // 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(moveSourceFolderId, "Source folder ID 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"); + moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity with no SDM role"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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 = + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = null; + boolean moveOperationFailed = false; + String errorMessage = null; + + try { + moveResult = + apiNoRoles.moveAttachment( + appUrl, + entityName, + facet[i], + 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(); + } - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + 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); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + sourceCountInitial, + sourceMetadataAfterMove.size(), + "Source should still have all attachments after failed move"); - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed move"); - // moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity with no SDM role"); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // // Save target before move - // String saveTargetBeforeMoveResponse = - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + @Test + @Order(76) + void testRenameAttachmentWithExtensionChange() throws IOException { + System.out.println( + "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = null; - // boolean moveOperationFailed = false; - // String errorMessage = null; - - // try { - // moveResult = - // apiNoRoles.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // 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 1: Create a new entity + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (newEntityID.equals("Could not create entity")) { + fail("Could not create entity"); + } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!saveResponse.equals("Saved")) { + fail("Could not save new entity: " + saveResponse); + } - // 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); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // sourceCountInitial, - // sourceMetadataAfterMove.size(), - // "Source should still have all attachments after failed move"); - - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed - // move"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + // Step 2: Upload a PDF attachment to each facet + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // @Test - // @Order(76) - // void testRenameAttachmentWithExtensionChange() throws IOException { - // System.out.println( - // "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - - // should return extension change warning"); + 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"); - // // Step 1: Create a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (newEntityID.equals("Could not create entity")) { - // fail("Could not create entity"); - // } - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save new entity: " + saveResponse); - // } + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!"Entity in draft mode".equals(editResponse)) { + fail("Could not put entity in draft mode for PDF upload"); + } - // // Step 2: Upload a PDF attachment to each facet - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // 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"); - - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!"Entity in draft mode".equals(editResponse)) { - // fail("Could not put entity in draft mode for PDF upload"); - // } + String[] facetAttachmentIDs = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + facetAttachmentIDs[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + if (facetAttachmentIDs[i] == null) { + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not upload sample.pdf to facet: " + facet[i]); + } + } - // String[] facetAttachmentIDs = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // facetAttachmentIDs[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - // if (facetAttachmentIDs[i] == null) { - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not upload sample.pdf to facet: " + facet[i]); - // } - // } + // Step 3: Save the entity + String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!savedAfterUpload.equals("Saved")) { + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not save entity after PDF upload: " + savedAfterUpload); + } - // // Step 3: Save the entity - // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!savedAfterUpload.equals("Saved")) { - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not save entity after PDF upload: " + savedAfterUpload); - // } + // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> .txt + for (int i = 0; i < facet.length; i++) { + String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!"Entity in draft mode".equals(editDraftResponse)) { + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not put entity in draft mode for rename on facet: " + facet[i]); + } - // // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> - // .txt - // for (int i = 0; i < facet.length; i++) { - // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!"Entity in draft mode".equals(editDraftResponse)) { - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not put entity in draft mode for rename on facet: " + facet[i]); - // } + String renameResponse = + api.renameAttachment( + appUrl, + entityName, + facet[i], + newEntityID, + facetAttachmentIDs[i], + "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + } - // String renameResponse = - // api.renameAttachment( - // appUrl, - // entityName, - // facet[i], - // newEntityID, - // facetAttachmentIDs[i], - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - // } + // Step 6: Save and validate the extension change warning message + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - // // Step 6: Save and validate the extension change warning message - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); - // } + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } - // // Clean up - // api.deleteEntity(appUrl, entityName, newEntityID); - // } + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + } - // @Test - // @Order(77) - // void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { - // System.out.println( - // "Test (77) : Upload attachment in draft, rename changing extension before save across all - // facets - should return extension change warning"); + // Clean up + api.deleteEntity(appUrl, entityName, newEntityID); + } - // for (int i = 0; i < facet.length; i++) { - // // Step 1: Create a new entity draft (do NOT save it yet) - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (newEntityID.equals("Could not create entity")) { - // fail("Could not create entity for facet: " + facet[i]); - // } + @Test + @Order(77) + void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { + System.out.println( + "Test (77) : Upload attachment in draft, rename changing extension before save across all facets - should return extension change warning"); - // // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + for (int i = 0; i < facet.length; i++) { + // Step 1: Create a new entity draft (do NOT save it yet) + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (newEntityID.equals("Could not create entity")) { + fail("Could not create entity for facet: " + facet[i]); + } - // 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"); + // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + 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"); + + String facetAttachmentID = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + if (facetAttachmentID == null) { + api.deleteEntityDraft(appUrl, entityName, newEntityID); + fail("Could not upload sample.pdf to facet: " + facet[i]); + } - // String facetAttachmentID = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - // if (facetAttachmentID == null) { - // api.deleteEntityDraft(appUrl, entityName, newEntityID); - // fail("Could not upload sample.pdf to facet: " + facet[i]); - // } + // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not saved + String renameResponse = + api.renameAttachment( + appUrl, entityName, facet[i], newEntityID, facetAttachmentID, "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.deleteEntityDraft(appUrl, entityName, newEntityID); + fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + } - // // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not - // saved - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facet[i], newEntityID, facetAttachmentID, - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.deleteEntityDraft(appUrl, entityName, newEntityID); - // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - // } + // Step 4: Save — should receive extension change warning, not "Saved" + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - // // Step 4: Save — should receive extension change warning, not "Saved" - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } - // // Clean up - // api.deleteEntity(appUrl, entityName, newEntityID); - // } - // } + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + + // Clean up + api.deleteEntity(appUrl, entityName, newEntityID); + } + } @Test @Order(78) @@ -7704,7 +7491,7 @@ void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOExcepti // fail("Failed to parse error response for references facet: " + e.getMessage()); // } // } else { - // fail("Attachment got created in references facet with file size exceeding maximum + // fail("Attachment got created in references facet with file size exceeding maximum // // limit"); // } // } else { 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 482340853..444a63841 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 @@ -2,14 +2,18 @@ import static org.junit.jupiter.api.Assertions.*; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; 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.*; @@ -388,6237 +392,6073 @@ void testUploadAttachmentWithoutSDMRole() throws IOException { } } - // @Test - // @Order(7) - // void testUploadSingleAttachmentPDFDuplicate() throws IOException { - // System.out.println("Test (7) : Upload duplicate pdf"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Boolean testStatus = false; + @Test + @Order(7) + void testUploadSingleAttachmentPDFDuplicate() throws IOException { + System.out.println("Test (7) : Upload duplicate pdf"); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Boolean testStatus = false; + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID); + 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, entityID); + if (response == "Entity in draft mode") { + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + testStatus = false; + } else { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + String expectedJson = + "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" already exists. Rename the object and try again.\"}}"; + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode actualJsonNode = objectMapper.readTree(check); + JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); + if (expectedJsonNode.equals(actualJsonNode)) { + testStatus = true; + } + } + } + } + if (!testStatus) { + fail("Attachment created"); + } + } + + @Test + @Order(8) + void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { + System.out.println("Test (8) : Upload duplicate pdf in different entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { + entityID2 = response; + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if (response == "Saved") { + response = api.checkEntity(appUrl, entityName, entityID2); + if (response.equals("Entity exists")) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Could not create entity"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID2); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + if (response == "Entity in draft mode") { + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, file); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + attachmentID4 = createResponse.get(1); + response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, attachmentID4); + if (response.equals("OK")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if (response.equals("Saved")) { + response = api.readAttachment(appUrl, entityName, facetName, entityID2, attachmentID4); + + if (response.equals("OK")) { + testStatus = true; + } + } + } + } + } + if (!testStatus) { + fail("Could not upload sample.pdf " + response); + } + } + + @Test + @Order(9) + void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { + System.out.println("Test (9): Create attachment with restricted character in filename"); + + 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", entityID); + 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, entityID); + if (response.equals("Entity in draft mode")) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + attachmentID6 = createResponse.get(1); + + String restrictedFilename = "a/\\bc.pdf"; + response = + api.renameAttachment( + appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); + + if (response.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + api.renameAttachment( + appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Saved".equals(response)) testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + } + if (!testStatus) { + fail("Attachment created with restricted character in filename"); + } + } + + @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"); + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // 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 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 response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response == "Entity in draft mode") { - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // testStatus = false; - // } else { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // String expectedJson = - // "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" - // already exists. Rename the object and try again.\"}}"; - // ObjectMapper objectMapper = new ObjectMapper(); - // JsonNode actualJsonNode = objectMapper.readTree(check); - // JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); - // if (expectedJsonNode.equals(actualJsonNode)) { - // testStatus = true; - // } - // } - // } - // } - // if (!testStatus) { - // fail("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; + } - // @Test - // @Order(8) - // void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { - // System.out.println("Test (8) : Upload duplicate pdf in different entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - // entityID2 = response; - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if (response == "Saved") { - // response = api.checkEntity(appUrl, entityName, entityID2); - // if (response.equals("Entity exists")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Could not create entity"); - // } + 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; + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + 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; + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID2); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + 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"); + } + } - // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - // if (response == "Entity in draft mode") { - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, - // file); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // attachmentID4 = createResponse.get(1); - // response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, - // attachmentID4); - // if (response.equals("OK")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if (response.equals("Saved")) { - // response = api.readAttachment(appUrl, entityName, facetName, entityID2, - // attachmentID4); - - // if (response.equals("OK")) { - // testStatus = true; - // } - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not upload sample.pdf " + response); - // } - // } + @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; + } - // @Test - // @Order(9) - // void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { - // System.out.println("Test (9): Create attachment with restricted character in filename"); + 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; + } - // boolean testStatus = false; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + 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; + } - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + 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"); + } + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // 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(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 response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Entity in draft mode")) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // attachmentID6 = createResponse.get(1); - - // String restrictedFilename = "a/\\bc.pdf"; - // response = - // api.renameAttachment( - // appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); - - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // api.renameAttachment( - // appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if ("Saved".equals(response)) testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // } - // if (!testStatus) { - // fail("Attachment created with restricted character in filename"); - // } - // } + System.out.println("Entity created"); - // @Test - // @Order(10) - // void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { - // System.out.println("Test (10): Upload attachments, delete one and create entity"); + System.out.println("Creating attachment PDF"); + ClassLoader classLoader = getClass().getClassLoader(); - // 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 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"); + } - // 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); + 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); + + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } + + // Store objectId in array + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.isEmpty()) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(0, sourceObjectId); + } + + 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); + } + + // Create target entity + copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } + + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array + + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } + + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity"); + } + + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } + + // 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); + } + + // 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 (readResponse.equals("OK")) { + testStatus = true; + } + if (!testStatus) { + fail("Could not verify that notes field was copied from source to target attachment"); + } + } + + @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"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample1.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 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); + } + + // 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); + } + + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity. Response: " + saveSourceResponse); + } + + // Fetch attachment metadata to get objectId and verify secondary properties + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } + + // Store objectId in array for reuse + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.size() < 2) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(1, sourceObjectId); + } - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Failed to create entity after deleting one attachment"); - // } - // } + // 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); + } - // @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()); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment. Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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"); + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array - // 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); - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // @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"); - // } - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // @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"); - // } - // } + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save 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"); - // } - // } + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // @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"); - // } - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // @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); - // } - // } + // 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); - // if (!testStatus) { - // fail("Multiple renames should have failed due to one unsupported characters"); - // } - // } + if (copiedAttachmentMetadata == null) { + fail("Could not find the copied attachment with file in target entity"); + } - // @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."); - // } - // } + 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); + } - // @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; - // } - // } + // Verify customProperty2 + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied. Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // @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"); - // } - // } + // 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(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"); - // } - // } + 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(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(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; - // @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 editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source 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"); - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample2.pdf").getFile()); - // @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"); - // } - // } + 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(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"); - // } - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // @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 (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } - // @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"); - // } + String sourceAttachmentId = createResponse.get(1); - // 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"); - // } + // 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); - // 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 updateNotesResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateNotesBody); - // 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"); - // } - // } + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update attachment notes field"); + } - // @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; - // } + // 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); + } - // 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; - // } + // 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); + } - // 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; - // } + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity. Response: " + saveSourceResponse); + } - // 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"); - // } - // } + // Fetch attachment metadata to get objectId and verify notes and secondary properties + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - // @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"); - // } + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } - // 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"); - // } + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.size() < 3) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(2, sourceObjectId); + } - // 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 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); + } - // 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"); - // } - // } + 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); + } - // @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; - // } + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment. Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } - // 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; - // } + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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; - // } + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array - // 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"); - // } - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // @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"); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // 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"); - // } + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target 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"); - // } + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // 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"); - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // 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"); - // } - // } + // 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(32) - // void testUploadNAttachments() throws IOException { - // System.out.println("Test (32): Upload maximum 4 attachments in an exsisting entity"); + if (copiedAttachmentMetadata == null) { + fail("Could not find the copied attachment with fil in target 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"); - // } - // } - // } + // 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); + } - // @Test - // @Order(33) - // void testDiscardDraftWithoutAttachments() { - // System.out.println("Test (33) : Discard draft without adding attachments"); + // 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 response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // Verify customProperty2 + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied. Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // if (response.equals("Could not create entity")) { - // 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); - // response = api.deleteEntityDraft(appUrl, entityName, response); - // if (!response.equals("Entity Draft Deleted")) { - // fail("Draft was not discarded properly"); - // } - // } + 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(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(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(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"); - // } - // } + sourceObjectIds.clear(); + 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"); - // } - // } + 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(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(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"); + } + } - // @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(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"); + } + } - // // 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(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"); + } + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + @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"); + } + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + @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 sourceAttachmentId = createResponse.get(1); + @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"); + } + } - // // 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); + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (47): Rename link in entity"); + List attachments = new ArrayList<>(); - // String updateResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, - // updateBody); + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // if (!updateResponse.equals("Updated")) { - // fail("Could not update attachment notes field"); - // } + 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"); - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // // Fetch attachment metadata to get objectId - // 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 - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.isEmpty()) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(0, 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"); - // 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); - // } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + } - // // 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(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"); + } - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array + 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 copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target 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"); + } + } - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + @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<>(); - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // // 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); - // } + 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 attachment content can be read from target entity - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // if (readResponse.equals("OK")) { - // testStatus = true; - // } - // if (!testStatus) { - // fail("Could not verify that notes field was copied from source to target attachment"); - // } - // } + 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(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"); - // } + 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"); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample1.pdf").getFile()); + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (50): Edit existing link in 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"); + 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 createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + 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 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(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (51): Edit existing link with invalid url"); + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // // 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 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); - // // Save source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity. Response: " + saveSourceResponse); - // } + testStatus = true; + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!testStatus) { + fail("Could not edit link with an invalid URL"); + } + } - // // Fetch attachment metadata to get objectId and verify secondary properties - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + @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<>(); - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + 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"); + } + } - // // Store objectId in array for reuse - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.size() < 2) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(1, sourceObjectId); - // } + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); - // // 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); - // } + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment. Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } + 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 editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + @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<>(); - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + 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"); + } - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + List sourceObjectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Id for 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 copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link: " + copyResponse); + } - // if (copiedAttachmentMetadata == null) { - // fail("Could not find the copied attachment with file in target entity"); - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } - // 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); - // } + attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // // Verify customProperty2 - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied. Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + 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"); + } - // // 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 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"); + } + } - // 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(55) + void testCopyLinkUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (55): Copy invalid type of link from one entity to another new entity"); - // @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"); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target 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"); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + List invalidObjectIds = Collections.singletonList("incorrectObjectId"); - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + 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 (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after unsuccessful copy"); + } - // 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"); - // } + String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + if (!deleteSourceResponse.equals("Entity Deleted")) { + fail("Could not delete source entity"); + } + } - // // 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(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"); + } - // // 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 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 source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity. Response: " + saveSourceResponse); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save new source entity"); + } - // // Fetch attachment metadata to get objectId and verify notes and secondary properties - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + List sourceObjectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.size() < 3) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(2, sourceObjectId); - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch objectId from new source 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); - // } + 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); + } - // 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); - // } + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment. Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target 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"); - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch. Expected '" + + expectedType + + "' but got '" + + receivedType + + "'."); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + System.out.println("Attachment type and URL validated successfully."); - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + String attachmentId = (String) copiedAttachment.get("ID"); + assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + 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 deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + if (!deleteResponse.equals("Entity Deleted")) { + fail("Could not delete new source 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(57) + void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println( + "Test (57): Copy invalid type of link from new entity to existing target entity"); - // if (copiedAttachmentMetadata == null) { - // fail("Could not find the copied attachment with fil in target entity"); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create new source 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); - // } + 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 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 saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save new source entity"); + } - // // Verify customProperty2 - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied. Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } - // // 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 invalidObjectIds = Collections.singletonList("invalidObjectId123"); - // 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); - // } + 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()); + } - // @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()); - // } - // } + // 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"); + } - // 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 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"); + } + } - // 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(58) + void testCopyLinkSuccessNewEntityDraft() throws IOException { + System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); - // @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"); - // } - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // @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"); - // } - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target 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"); - // } - // } + 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"); + } - // @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"); - // } - // } + List sourceObjectIds = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .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"); - // } - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Id for link"); + } - // @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"); - // } - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link: " + copyResponse); + } - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (47): Rename link in entity"); - // List attachments = new ArrayList<>(); + 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"); - // 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 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); + } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save 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"); - // @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"); - // } + sourceObjectIds.clear(); - // 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"); - // } + 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"); + } + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + 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"); + } + } - // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit 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; + 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); + } - // 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(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println("Test (60): View changelog for newly created attachment"); - // @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<>(); + // 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); - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + // 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"); - // 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"); - // } + // 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); - // 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"); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + changelogAttachmentID = createResponse.get(1); - // 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"); - // } - // } + 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"); - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (50): Edit existing link in entity"); + // Fetch changelog for the newly created attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // 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"; + assertNotNull(changelogResponse, "Changelog response should not be null"); - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link"); - // } + // 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"); + } - // 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"); - // } - // } - // } + @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"); - // @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"); - // } - // } + // 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); - // @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 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"); - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // Boolean testStatus = false; - // List attachments = new ArrayList<>(); + // 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 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); - // } + // Fetch changelog after modifications + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // @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<>(); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // 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)"); - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); - // 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 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"); + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + @Test + @Order(62) + void testChangelogAfterRenamingAttachment() throws IOException { + System.out.println( + "Test (62): Rename attachment and verify changelog increases with rename entry"); - // List sourceObjectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + // 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"); - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Id for link"); - // } + // 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 copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link: " + copyResponse); - // } + // Save entity after rename + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.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"); - - // 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 after rename + Map changelogAfterRename = + 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(changelogAfterRename, "Changelog response should not be null after rename"); - // @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 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"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @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); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or 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"); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // List invalidObjectIds = Collections.singletonList("incorrectObjectId"); + // 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); - // 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()); - // } + // 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"); - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target entity after unsuccessful copy"); - // } + // 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"); - // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // if (!deleteSourceResponse.equals("Entity Deleted")) { - // fail("Could not delete source entity"); - // } - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // @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"); - // } + 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 sourceObjectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch objectId from new source entity"); - // } + // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + + // customProperty2) + assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); - // 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); - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // Save entity again without any modifications + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // 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, newEntityID); + 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 second save + Map changelogAfterSecondSave = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch. Expected '" - // + expectedType - // + "' but got '" - // + receivedType - // + "'."); + assertNotNull( + changelogAfterSecondSave, "Changelog response should not be null after second save"); - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // 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"); - // System.out.println("Attachment type and URL validated successfully."); + @SuppressWarnings("unchecked") + List> changeLogsAfterSecondSave = + (List>) changelogAfterSecondSave.get("changeLogs"); + assertEquals( + 3, + changeLogsAfterSecondSave.size(), + "Should still have exactly 3 changelog entries after second save"); - // String attachmentId = (String) copiedAttachment.get("ID"); - // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // Clean up the entity + api.deleteEntity(appUrl, entityName, newEntityID); + } - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open the 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"); - // String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // if (!deleteResponse.equals("Entity Deleted")) { - // fail("Could not delete new 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); - // @Test - // @Order(57) - // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println( - // "Test (57): Copy invalid type of link from new entity to existing target 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"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create new 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"); - // 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"); - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save new source entity"); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + String newAttachmentID = createResponse.get(1); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } + 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"); - // List invalidObjectIds = Collections.singletonList("invalidObjectId123"); + // 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"); - // 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()); - // } + // Save entity again without modifying the attachment + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // // 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"); - // } + // Edit entity to fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // 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"); - // } - // } + // Fetch changelog for the attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); - // @Test - // @Order(58) - // void testCopyLinkSuccessNewEntityDraft() throws IOException { - // System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // 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); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println( + "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // 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 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 sourceObjectIds = - // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, - // copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + // 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 (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Id for link"); - // } + 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"); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link: " + copyResponse); - // } + // 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> 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"); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // 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); - // } + // 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()); + } + } - // @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"); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // 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 target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target 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; - // 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); - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetBeforeMoveResponse); + } - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println("Test (60): View changelog for newly created attachment"); + // 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"); + } + } - // // 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); + // 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"); - // // 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"); + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // 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"); + @Test + @Order(66) + public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + System.out.println( + "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // 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"); - // } + // 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(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"); - // } + // 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(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); - // } + 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(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"); + // 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()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata for attachment: " + attachmentId); + } + } - // // 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 all objectIds from 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"); - - // // 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); - // } + // 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(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"); + 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 a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(newEntityID, "Failed to create new entity"); - // assertNotEquals("Could not create entity", newEntityID); + File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + List targetCreateResponse = + api.createAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + srvpath, + targetPostData, + duplicateFile); - // // 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"); + if (!targetCreateResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment on 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"); + // 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); + } - // 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); - // } + // 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"); - // @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(67) + public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + System.out.println( + "Test (67): Move attachments with notes and secondary properties with 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()); - // } - // } + // 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); + } + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // 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 target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create 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 before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity: " + 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); + } + } - // // 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"); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // // 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"); + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // 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); + } - // @Test - // @Order(66) - // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - // System.out.println( - // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // 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"); + } - // // 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 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); + } - // // 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 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); + } + } - // 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 (all moved with sourceFacet) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals(0, sourceMetadataAfterMove.size(), "Source entity has no attachments after 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"); - // } - // } + // 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(68) + public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + System.out.println( + "Test (68): Move valid attachments from Source Entity to Target Entity 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()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // 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 all objectIds from 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())); - // // 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"); - // } + 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 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"); + } + } - // 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"); - // } + // 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()); + // 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()); + } + } - // // 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); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // @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())); + // 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"); + } - // 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 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"); + } + } - // // 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"); - // } - // } + // 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"); - // // 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 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); + } - // // 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); - // } - // } + // 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()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // 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 all objectIds from 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())); - // // Create target entity - // 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"); - // // 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 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 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 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()); + // 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 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 object IDs for all attachments"); + } - // // 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"); + // 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"); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // 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"); - // @Test - // @Order(68) - // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (68): Move valid attachments from Source Entity to Target Entity without - // sourceFacet"); + 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"); + } - // // 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 entity before move + String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // // 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 initial target metadata count + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int initialTargetCount = targetMetadataBeforeMove.size(); - // 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"); + // 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 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"); - // } - // } + // Expected Behavior - Verify duplicate was skipped, other attachments moved + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + int nonDuplicateCount = moveObjectIds.size() - 1; + int expectedTargetCount = initialTargetCount + nonDuplicateCount; - // // 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()); - // } - // } + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have initial attachments plus non-duplicate moved attachments"); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Verify at least one non-duplicate attachment was moved + assertTrue( + targetMetadataAfterMove.size() > initialTargetCount, + "Target should have more attachments after move (non-duplicates added)"); - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // 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 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"); - // } + @Test + @Order(70) + public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + throws Exception { + System.out.println( + "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - // // 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"); - // } - // } + // 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: 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); - // } + // 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(69) - // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (69): Move attachments into existing Target Entity when duplicate exists 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"); - // } + // 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); + } + } - // // 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 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); + } + } - // 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()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata 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); - // } + 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()); - // // 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 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 object IDs for all attachments"); - // } + // 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 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"); - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // // 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"); + // Get target attachment count before move + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); - // 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"); - // } + // 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 entity before move - // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + // 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 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"); - // } + // 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); + } + } - // // 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); - // } + // 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); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + @Test + @Order(71) + public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + System.out.println( + "Test (71): Move attachments with invalid or undefined secondary properties"); - // @Test - // @Order(70) - // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - // throws Exception { - // System.out.println( - // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + // 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 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"); - // } + // 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())); + 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"); - // } - // } + // 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); + } - // // 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); - // } - // } + // 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 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); - // } - // } + // 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 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 saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + // Verify attachments moved to target + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // // 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"); - // } + 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 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 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 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 - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // 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); - // } + @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(71) - // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - // System.out.println( - // "Test (71): Move attachments with invalid or undefined secondary properties"); + // 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 add attachments - // 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"); - // // 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); - // } + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // 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); - // } + // 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()); + } + } - // // 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); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // // Get source attachment count before move - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + 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"); - // } + // 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 saveTargetBeforeMoveResponse68 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse68.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); - // } + // 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 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"); - // } + // 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"); + } - // // 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 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 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); - // } + // Verify all expected attachments are in target + Set targetFileNames = + targetMetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); - // @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"); + for (File file : files) { + assertTrue( + targetFileNames.contains(file.getName()), + "Target should contain attachment: " + file.getName()); + } - // // 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"); - // } + // 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); + } - // // 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())); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + sourceCountBeforeMove, + sourceMetadataAfterMove.size(), + "Source entity in draft mode retains attachments after move (copy behavior)"); - // 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"); + Set sourceFileNamesAfterMove = + sourceMetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); - // // 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"); - // } - // } + for (File file : files) { + assertTrue( + sourceFileNamesAfterMove.contains(file.getName()), + "Source (draft) should still contain attachment: " + file.getName()); + } - // // 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); - // } + // 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(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"); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // Add attachment with original name (sample.txt) + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // 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"); - // } + 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"); - // // 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 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"); - // // 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 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 - // 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()); - // } + // 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"); + } - // // 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); - // } + // 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"); - // 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 after rename + saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after rename: " + saveSourceResponse); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // 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"); + } - // @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 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 attachment - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // 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"); + } - // // Add attachment with original name (sample.txt) - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // 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); - // 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 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"); - // 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(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"); - // // 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"); + } - // // 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"); - // } + // 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())); - // // 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", "application/pdf"); + 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"); - // } + // 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"); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // 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"); - // } + // 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 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); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // @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"); + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // // 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 1 + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity 1"); + } - // // 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 target1 before move + String saveTarget1BeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTarget1BeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity 1 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 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"); + } - // // 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 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"); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // 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()); - // // Get count of attachments in source - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + for (File file : files) { + assertTrue( + target1FileNames.contains(file.getName()), + "Target 1 should contain attachment: " + file.getName()); + } - // // 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 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 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 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 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 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 all expected files are in Target Entity 2 + Set target2FileNames = + target2MetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); - // // 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"); - // } + for (File file : files) { + assertTrue( + target2FileNames.contains(file.getName()), + "Target 2 should contain attachment: " + file.getName()); + } - // // 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); - // } + // 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); + } - // 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()); - // } - // } + @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"); + } - // 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())); - // // 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 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"); + } + } - // @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"); - // } + // 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); + } - // // 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 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()); + } + } - // 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"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // // 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"); - // } - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // // 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); - // } + // 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"); + } - // // Get count of attachments in source - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + // 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(); + } - // // 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 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); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // 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"); - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // 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"); - // // 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"); - // } + // Clean up - delete both entities using SDM role + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // 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(); - // } + @Test + @Order(76) + void testRenameAttachmentWithExtensionChange() throws IOException { + System.out.println( + "Test (76) : Rename attachment changing extension from .pdf to .txt - should return extension change warning"); - // // 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); - // } + // Step 1: Create a new entity + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (newEntityID.equals("Could not create entity")) { + fail("Could not create entity"); + } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!saveResponse.equals("Saved")) { + fail("Could not save new entity: " + saveResponse); + } - // @Test - // @Order(76) - // void testRenameAttachmentWithExtensionChange() throws IOException { - // System.out.println( - // "Test (76) : Rename attachment changing extension from .pdf to .txt - should return - // extension change warning"); + // Step 2: Upload a PDF attachment + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // // Step 1: Create a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (newEntityID.equals("Could not create entity")) { - // fail("Could not create entity"); - // } - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save new entity: " + saveResponse); - // } + 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"); - // // Step 2: Upload a PDF attachment - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (editResponse != "Entity in draft mode") { + fail("Could not put entity in draft mode for PDF upload"); + } - // 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, newEntityID, srvpath, postData, file); + String check = createResponse.get(0); + if (!check.equals("Attachment created")) { + fail("Could not upload sample.pdf: " + check); + } + String newAttachmentID = createResponse.get(1); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (editResponse != "Entity in draft mode") { - // fail("Could not put entity in draft mode for PDF upload"); - // } + // Step 3: Save the entity + String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!savedAfterUpload.equals("Saved")) { + fail("Could not save entity after PDF upload: " + savedAfterUpload); + } - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); - // String check = createResponse.get(0); - // if (!check.equals("Attachment created")) { - // fail("Could not upload sample.pdf: " + check); - // } - // String newAttachmentID = createResponse.get(1); + // Step 4: Edit the entity + String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (editDraftResponse != "Entity in draft mode") { + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not put entity in draft mode for rename"); + } - // // Step 3: Save the entity - // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!savedAfterUpload.equals("Saved")) { - // fail("Could not save entity after PDF upload: " + savedAfterUpload); - // } + // Step 5: Rename the attachment changing the extension from .pdf to .txt + String renameResponse = + api.renameAttachment( + appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); + if (!renameResponse.equals("Renamed")) { + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not rename attachment: " + renameResponse); + } - // // Step 4: Edit the entity - // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (editDraftResponse != "Entity in draft mode") { - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not put entity in draft mode for rename"); - // } + // Step 6: Save and validate the extension change error message + String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertNotNull(saveWithWarningResponse, "Response should not be null"); - // // Step 5: Rename the attachment changing the extension from .pdf to .txt - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); - // if (!renameResponse.equals("Renamed")) { - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not rename attachment: " + renameResponse); - // } + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - // // Step 6: Save and validate the extension change error message - // String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // newEntityID); - // assertNotNull(saveWithWarningResponse, "Response should not be null"); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match expected value"); - // break; - // } - // } - // } + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match expected value"); + break; + } + } + } - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found in response. Full response: " - // + saveWithWarningResponse); + assertTrue( + foundExtensionError, + "Expected extension change warning not found in response. Full response: " + + saveWithWarningResponse); - // // Clean up - // api.deleteEntity(appUrl, entityName, newEntityID); - // } + // Clean up + api.deleteEntity(appUrl, entityName, newEntityID); + } @Test @Order(77) From 5f11ff4a610d272620872e20c1cd628ac4f6c050 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 16:27:12 +0530 Subject: [PATCH 09/13] Revert "Uncommenting tests" This reverts commit d680ae86bd3cee56d1e09bb592217f12f831c301. --- ...ntegrationTest_Chapters_MultipleFacet.java | 12332 +++++++-------- .../sdm/IntegrationTest_MultipleFacet.java | 12421 ++++++++-------- .../cds/sdm/IntegrationTest_SingleFacet.java | 11440 +++++++------- 3 files changed, 18346 insertions(+), 17847 deletions(-) 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 8efd27964..b7b6c8441 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 @@ -467,6109 +467,6235 @@ void testCreateNewBookWithChapterAndAttachments() throws IOException { } } - @Test - @Order(7) - void testRenameChapterAttachments() { - System.out.println("Test (7) : Rename single attachment, reference, and footnote inchapter"); - Boolean testStatus = true; - - try { - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - if ("Entity in draft mode".equals(response)) { - String[] name = {"sample123", "reference123", "footnote123"}; - for (int i = 0; i < facet.length; i++) { - // Read the facet to ensure it exists - response = - api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); - if (!"Renamed".equals(response)) { - testStatus = false; - System.out.println(facet[i] + " was not renamed: " + response); - } - } - // Save book draft if everything is renamed - if (testStatus) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if (!"Saved".equals(response)) { - testStatus = false; - System.out.println("Book draft was not saved: " + response); - } - } else { - // Attempt save despite potential rename failures - api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - } - } else { - testStatus = false; - System.out.println("Book was not put into draft mode: " + response); - } - } catch (Exception e) { - testStatus = false; - System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); - } - - if (!testStatus) { - fail("There was an error during the rename test process for chapter."); - } - } - - @Test - @Order(8) - void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { - System.out.println("Test (8): Create chapter attachments with unsupported characters"); - 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("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, bookEntityName, srvpath, bookID); - if (!"Entity in draft mode".equals(response)) { - fail("Book not in draft mode: " + response); - return; - } - - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", chapterID); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); - - if (!"Attachment created".equals(createResponse.get(0))) { - fail("Could not create attachment in chapter facet: " + facet[i]); - return; - } - - String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE - response = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); - - System.out.println("Rename response for chapter " + facet[i] + ": " + response); - } - - // Save should fail - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - // ---------------- PARSE JSON ---------------- - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - String message = root.path("error").path("message").asText(); - - // ---------------- NORMALIZE MESSAGE ---------------- - // 1. Normalize smart quotes - // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared - message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); - - // ---------------- EXPECTED MESSAGE (EXACT) ---------------- - String expectedMessage = - "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" - + "Table: attachments\n" - + "Page: IntegrationTestEntity"; - - if (message.equals(expectedMessage)) { - - for (int i = 0; i < facet.length; i++) { - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if ("Saved".equals(response)) { - testStatus = true; - } - } - - if (!testStatus) { - fail("Test for unsupported characters in chapter attachments failed"); - } - } - - @Test - @Order(9) - void testRenameSingleDuplicateInChapter() throws IOException { - System.out.println( - "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); - Boolean testStatus = false; - int counter = 0; - - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Edit entity response: " + response); - - if ("Entity in draft mode".equals(response)) { - // To create a duplicate within the same facet, we need to rename ID2[i] to - // the same name as an existing file in that facet. After test 7, the existing files are: - // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) - // We rename ID2[i] (sample123.txt from test 8) to these names which already exist - String[] duplicateNames = {"sample123", "reference123", "footnote123"}; - String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; - - // Try to rename to duplicate file names (names that already exist in each facet) - for (int i = 0; i < facet.length; i++) { - response = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); - System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); - if ("Renamed".equals(response)) { - counter++; - } - } - System.out.println("Renamed count: " + counter); - - if (counter == facet.length) { - // Try to save - should fail with duplicate error - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Save response (expecting error): " + response); - - // Parse JSON response to check for duplicate error - ObjectMapper mapper = new ObjectMapper(); - try { - JsonNode root = mapper.readTree(response); - String message = root.path("error").path("message").asText(); - - if (message.contains("already exists")) { - System.out.println("Duplicate error detected as expected: " + message); - counter = 0; - // Rename with valid different names - for (int i = 0; i < facet.length; i++) { - response = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); - System.out.println("Rename " + facet[i] + " to valid name: " + response); - if ("Renamed".equals(response)) { - counter++; - } - } - - if (counter == facet.length) { - // Save should now succeed - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Final save response: " + response); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } else { - System.out.println("Unexpected error message: " + message); - } - } catch (Exception e) { - // Response might not be JSON if save succeeded (shouldn't happen with duplicates) - System.out.println("Response was not JSON error: " + response); - // If save succeeded unexpectedly, we still need to ensure book is saved - if ("Saved".equals(response)) { - System.out.println( - "Save succeeded unexpectedly - duplicates might be in different facets"); - } - } - } - } else { - System.out.println("Book was not put into draft mode: " + response); - } - - if (!testStatus) { - fail("Duplicate rename test failed for chapter"); - } - } - - @Test - @Order(10) - void testRenameToValidateNamesInChapter() throws IOException { - System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); - Boolean testStatus = false; - - // Create a new book and chapter for this test - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - bookID3 = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); - if (!"Could not create entity".equals(chapterResponse)) { - chapterID3 = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID3); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); - } - - String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; - - boolean allRenamed = true; - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); - if (!"Renamed".equals(response1)) { - allRenamed = false; - System.out.println( - "Failed to rename " - + facet[i] - + " to valid name " - + validNames[i] - + ": " - + response1); - } - } - - if (allRenamed) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } - } - - if (!testStatus) { - fail("Could not rename chapter attachments to valid names"); - } - } - - @Test - @Order(11) - void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { - System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); - boolean testStatus = true; - - try { - String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Edit entity response: " + response); - - if (response.equals("Entity in draft mode")) { - String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; - for (int i = 0; i < facet.length; i++) { - response = - apiNoRoles.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); - System.out.println("Rename response for " + facet[i] + ": " + response); - if (!"Renamed".equals(response)) { - testStatus = false; - } - } - - if (testStatus) { - // Save should fail with permission error - response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - System.out.println("Save response (expecting permission error): " + response); - - // The expected error should indicate no permissions to update - String expected = - "[{\"code\":\"\",\"message\":\"Could not update the following files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - - // Check if response contains permission error - if (!response.equals(expected) - && !response.contains("do not have the required permissions")) { - System.out.println("Expected permission error but got: " + response); - testStatus = false; - } else { - System.out.println("Got expected permission error"); - } - } else { - // Some renames failed - save to release draft - apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - } - } else { - System.out.println("Could not edit entity: " + response); - testStatus = false; - } - } catch (Exception e) { - System.out.println("Exception: " + e.getMessage()); - testStatus = false; - } - - if (!testStatus) { - fail("Chapter attachment got renamed without SDM roles."); - } - } - - @Test - @Order(12) - void testDeleteSingleChapterAttachment() throws IOException { - System.out.println( - "Test (12) : Delete single attachment, reference, and footnote from chapter"); - Boolean testStatus = false; - int deleteCounter = 0; - - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - if (response.equals("Deleted")) deleteCounter++; - } - if (deleteCounter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - if (response.equals("Saved")) { - int verifyCounter = 0; - for (int i = 0; i < facet.length; i++) { - response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - if (response.equals("Could not read Attachment")) verifyCounter++; - } - if (verifyCounter == facet.length) { - testStatus = true; - } else { - fail( - "Could not verify all deleted chapter facets. Verified: " - + verifyCounter - + "/" - + facet.length); - } - } else { - fail("Could not save book after deleting chapter attachments"); - } - } else { - fail( - "Could not delete all chapter attachments. Deleted: " - + deleteCounter - + "/" - + facet.length); - } - } else { - fail("Could not edit book to draft mode"); - } - - if (!testStatus) { - fail("Test failed to delete chapter attachments"); - } - } - - @Test - @Order(13) - void testUploadBlockedMimeTypeToChapter() throws IOException { - System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); - Boolean testStatus = false; - - // Create new book and chapter - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - bookID4 = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); - if (!"Could not create entity".equals(chapterResponse)) { - chapterID4 = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = - new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID4); - postData.put("mimeType", "application/rtf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - boolean allBlocked = true; - for (int i = 0; i < facet.length; i++) { - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID4, 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)) { - allBlocked = false; - System.out.println( - "Chapter facet " - + facet[i] - + " incorrectly accepted blocked mimeType: " - + actualResponse); - } - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); - if ("Saved".equals(response) && allBlocked) { - testStatus = true; - } - } - } - - if (!testStatus) { - fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); - } - } - - @Test - @Order(14) - void testDeleteBookAndChapter() { - System.out.println("Test (14) : Delete book (and its chapters)"); - Boolean testStatus = false; - // Delete books (chapters are deleted automatically as they're composition) - String response = api.deleteEntity(appUrl, bookEntityName, bookID); - String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); - String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); - String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); - if (response.equals("Entity Deleted") - && response2.equals("Entity Deleted") - && response3.equals("Entity Deleted") - && response4.equals("Entity Deleted")) testStatus = true; - if (!testStatus) fail("Could not delete books"); - } - - @Test - @Order(15) - void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { - System.out.println( - "Test (15) : Rename & Update secondary property in chapter before book is saved"); - System.out.println("Creating book and chapter"); - - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (!response.equals("Could not create entity")) { - bookID5 = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); - if (!chapterResponse.equals("Could not create entity")) { - chapterID5 = chapterResponse; - - System.out.println("Creating attachment, reference, and footnote in chapter"); - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID5); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - boolean allCreated = true; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); - if (tempID[i] == null || tempID[i].isEmpty()) { - System.out.println("Failed to create attachment for facet: " + facet[i]); - allCreated = false; - } - } - - System.out.println("Attachments, References, and Footnotes created in chapter"); - System.out.println( - "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + tempID[2]); - - if (!allCreated) { - fail("Could not create all attachments for test 15"); - } - - // Reset counter for this test - counter = 0; - - // Use valid dropdown value for customProperty1 - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; - - for (int i = 0; i < facet.length; i++) { - System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - System.out.println("Rename response for " + facet[i] + ": " + response1); - - // Update customProperty1 (String - dropdown value) - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); - - // Update customProperty2 (Integer) - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - - // Update customProperty5 (DateTime) - using customProperty5 like Books test - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - - // Update customProperty6 (Boolean) - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - // Check all updates succeeded - if ("Renamed".equals(response1) - && "Updated".equals(updateSecondaryPropertyResponse1) - && "Updated".equals(updateSecondaryPropertyResponse2) - && "Updated".equals(updateSecondaryPropertyResponse3) - && "Updated".equals(updateSecondaryPropertyResponse4)) { - counter++; - } else { - System.out.println( - "Update failed for " - + facet[i] - + ": rename=" - + response1 - + ", dropdown=" - + updateSecondaryPropertyResponse1 - + ", int=" - + updateSecondaryPropertyResponse2 - + ", datetime=" - + updateSecondaryPropertyResponse3 - + ", bool=" - + updateSecondaryPropertyResponse4); - } - } - - System.out.println("Counter after all facets: " + counter); - if (counter == facet.length) { - // Save the book (not the chapter) - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - System.out.println("Save response: " + response); - if ("Saved".equals(response)) { - testStatus = true; - } - } else { - System.out.println( - "Counter is less than " + facet.length + ", not saving. Counter: " + counter); - } - } - } - - if (!testStatus) { - fail( - "Could not update secondary properties in chapter before book save. Counter: " + counter); - } - } - - @Test - @Order(16) - void testUploadNAttachmentsToChapter() throws IOException { - System.out.println("Test (16) : Upload N attachments to chapter"); - Boolean testStatus = false; - counter = 0; - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); - - for (int j = 0; j < 5; j++) { - // Create temp file with unique name per iteration - File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); - Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID5); - 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, bookEntityName, srvpath, bookID5); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - String check = facetResponse.get(0); - if (check.equals("Attachment created")) { - counter++; - } else { - System.out.println( - "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - } - } - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if (!response.equals("Saved")) { - System.out.println( - "Failed to save book after creating attachments in chapter: " + response); - } - } else { - System.out.println("Could not edit book draft: " + response); - } - tempFile.delete(); - } - - if (counter == 15) { // 5 iterations * 3 facets - testStatus = true; - } - - if (!testStatus) { - fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); - } - } - - @Test - @Order(17) - void testDiscardDraftWithoutChapterAttachments() { - System.out.println("Test (17) : Discard book draft without chapter attachments"); - Boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - // Create chapter but don't add attachments - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft without chapter attachments was not discarded properly"); - } - } - - @Test - @Order(18) - void testDiscardDraftWithChapterAttachments() throws IOException { - System.out.println("Test (18) : Discard book draft with chapter attachments"); - Boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - // Create chapter - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - 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 chapter - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - String check = facetResponse.get(0); - if (!check.equals("Attachment created")) { - System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - } - } - - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft with chapter attachments was not discarded properly"); - } - } - - @Test - @Order(19) - void testUploadChapterAttachmentWithoutSDMRole() throws IOException { - System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); - Boolean testStatus = true; - - String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - try { - List createResponse = - apiNoRoles.createAttachment( - appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); - String check = createResponse.get(0); - - if (check.equals("Attachment created")) { - testStatus = false; - } - } catch (Exception e) { - // Expected to fail - testStatus = true; - } - - apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - } - } - - if (!testStatus) { - fail("Chapter attachment was uploaded without SDM roles"); - } - } - - @Test - @Order(20) - void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { - System.out.println( - "Test (20): Rename & Update secondary property in chapter after book is saved"); - Boolean testStatus = false; - counter = 0; // Reset counter for this test - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - System.out.println("Editing book, response: " + response); - - if (response.equals("Entity in draft mode")) { - // Use unique names that won't conflict with existing attachments - String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; - Integer secondaryPropertyInt = 42; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - System.out.println("Renaming and updating secondary properties for chapter attachment"); - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - // Get the first attachment ID from the chapter - try { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); - if (!metadata.isEmpty()) { - tempID[i] = (String) metadata.get(0).get("ID"); - } - } catch (IOException e) { - fail("Could not fetch metadata for chapter: " + e.getMessage()); - } - - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if (response.equals("Saved")) { - testStatus = true; - System.out.println("Renamed & updated Secondary properties for chapter attachment"); - } - } - } - if (!testStatus) fail("Could not update secondary properties in chapter after book is saved"); - } - - @Test - @Order(21) - void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { - System.out.println( - "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); - System.out.println("Creating book and chapter"); - Boolean testStatus = false; - int localCounter = 0; - int createCounter = 0; - - // Create new book and chapter for this test - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - if (tempID[i] != null) { - createCounter++; - } - } - - // Only proceed if all facets were created successfully - if (createCounter == facet.length) { - // Prepare test data - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testid"; - - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - localCounter++; - } - } - - if (localCounter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - // Fetch metadata and verify values weren't updated due to invalid property - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - - // Parse JSON response and check for expected error messages - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved with expected invalid property errors"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for chapter attachment is unsuccessful"); - } - } else { - System.out.println( - "Not all facets updated successfully. localCounter: " + localCounter); - } - } else { - System.out.println( - "Not all facets created successfully. createCounter: " + createCounter); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, tempBookID); - } - } - if (!testStatus) - fail("Could not update invalid secondary property in chapter before book is saved"); - } - - @Test - @Order(22) - void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { - System.out.println( - "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); - System.out.println("Creating book and chapter"); - Boolean testStatus = false; - int localCounter = 0; - int createCounter = 0; - - // Create new book and chapter - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] tempID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - tempID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - if (tempID[i] != null) { - createCounter++; - } - } - - // Only proceed if all facets were created successfully - if (createCounter != facet.length) { - api.deleteEntity(appUrl, bookEntityName, tempBookID); - fail("Not all facets created successfully. createCounter: " + createCounter); - } - - // Save the book first - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (!response.equals("Saved")) { - api.deleteEntity(appUrl, bookEntityName, tempBookID); - fail("Could not save book initially"); - } - - // Now edit to update with invalid property - response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (response.equals("Entity in draft mode")) { - String name1 = "sample.pdf"; - Integer secondaryPropertyInt = 12; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testidinvalid"; - - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - localCounter++; - } - } - - if (localCounter == facet.length) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - - // Parse JSON response and check for expected error messages - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved with expected invalid property errors"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for chapter attachment is unsuccessful"); - } - } else { - System.out.println( - "Not all facets updated successfully. localCounter: " + localCounter); - } - } - api.deleteEntity(appUrl, bookEntityName, tempBookID); - } - } - if (!testStatus) - fail("Could not update invalid secondary property in chapter after book is saved"); - } - - @Test - @Order(23) - void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { - System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); - - boolean testStatus = false; - - // Reuse bookID5 and chapterID5 - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - - if (response.equals("Entity in draft mode")) { - ClassLoader classLoader = getClass().getClassLoader(); - - // Use temp files with unique names to avoid duplicate name errors - File originalPdf = - new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - File originalTxt = - new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); - - File file1 = File.createTempFile("test23_pdf_", ".pdf"); - File file2 = File.createTempFile("test23_txt_", ".txt"); - Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData1 = new HashMap<>(); - postData1.put("up__ID", chapterID5); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - Map postData2 = new HashMap<>(postData1); - postData2.put("up__ID", chapterID5); - postData2.put("mimeType", "text/plain"); - - boolean allCreated = true; - String[] tempID1 = new String[facet.length]; - String[] tempID2 = new String[facet.length]; - - for (int i = 0; i < facet.length; i++) { - List response1 = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); - List response2 = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); - - if (response1.get(0).equals("Attachment created") - && response2.get(0).equals("Attachment created")) { - tempID1[i] = response1.get(1); // to keep one - tempID2[i] = response2.get(1); // will delete this one - } else { - System.out.println("Failed to create attachments for facet " + facet[i]); - System.out.println("Response 1: " + response1.get(0)); - System.out.println("Response 2: " + response2.get(0)); - allCreated = false; - break; - } - - String deleteResponse = - api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); - if (!"Deleted".equals(deleteResponse)) { - allCreated = false; - break; - } - } - - file1.delete(); - file2.delete(); - - if (allCreated) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } else { - System.out.println("Could not edit book: " + response); - } - - if (!testStatus) { - fail("Failed to upload multiple chapter facet entries, delete one per facet and save book"); - } - } - - @Test - @Order(24) - void testUpdateChapterEntityDraft() throws IOException { - System.out.println("Test (24): Update chapter in book draft with new facet content"); - boolean testStatus = false; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - // Use unique temp file name to avoid duplicates - File tempFile = File.createTempFile("test24_sample_", ".pdf"); - Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", chapterID5); - 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, bookEntityName, srvpath, bookID5); - if (response.equals("Entity in draft mode")) { - boolean allCreated = true; - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - String check = facetResponse.get(0); - if (!check.equals("Attachment created")) { - allCreated = false; - System.out.println( - "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - } - } - - if (allCreated) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } else { - System.out.println("Could not edit book: " + response); - } - - tempFile.delete(); - - if (!testStatus) { - fail("Failed to update chapter entity draft with new attachments"); - } - } - - @Test - @Order(25) - void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - throws IOException { - System.out.println( - "Test (25): Rename & Update secondary properties for multiple chapter attachments after book is saved"); - System.out.println("Creating book and chapter with multiple attachments"); - - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!"Could not create entity".equals(chapterResponse)) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create PDF attachments - postData.put("mimeType", "application/pdf"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - String[] pdfID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - pdfID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create TXT attachments - postData.put("mimeType", "application/txt"); - file = new File(classLoader.getResource("sample.txt").getFile()); - String[] txtID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - txtID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create EXE attachments - postData.put("mimeType", "application/exe"); - file = new File(classLoader.getResource("sample.exe").getFile()); - String[] exeID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - exeID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Save book first - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (!"Saved".equals(response)) { - fail("Could not save book initially"); - } - - // Edit book to update chapter attachments - response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (response.equals("Entity in draft mode")) { - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; - - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // Update PDF properties - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String renameResp = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - String upd3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - String upd4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - - if ("Renamed".equals(renameResp) - && "Updated".equals(upd1) - && "Updated".equals(upd2) - && "Updated".equals(upd3) - && "Updated".equals(upd4)) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - - // Update TXT properties (only boolean) - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - String upd = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - if ("Updated".equals(upd)) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } - - // Update EXE properties (dropdown and int) - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValueExe = integrationTestUtils.getDropDownValue(); - String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - for (int i = 0; i < facet.length; i++) { - RequestBody bodyDropdownExe = - RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - RequestBody bodyIntExe = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - - if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (response.equals("Saved")) { - System.out.println("Book saved"); - testStatus = true; - System.out.println("Renamed & updated Secondary properties for chapter attachments"); - } - } - } - api.deleteEntity(appUrl, bookEntityName, tempBookID); - } - } - if (!testStatus) { - fail("Could not update secondary property in chapter after book is saved"); - } - } - - @Test - @Order(26) - void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() - throws IOException { - System.out.println( - "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter facets before book is saved"); - System.out.println("Creating book and chapter"); - - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (!"Could not create entity".equals(response)) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!"Could not create entity".equals(chapterResponse)) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create PDF attachments - postData.put("mimeType", "application/pdf"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - String[] pdfID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - pdfID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create TXT attachments - postData.put("mimeType", "application/txt"); - file = new File(classLoader.getResource("sample.txt").getFile()); - String[] txtID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - txtID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - // Create EXE attachments - postData.put("mimeType", "application/exe"); - file = new File(classLoader.getResource("sample.exe").getFile()); - String[] exeID = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - exeID[i] = - CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); - } - - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; - - String name1 = "sample1234.pdf"; - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - - // Update PDF properties - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String renameResp = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - String upd3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - String upd4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - String updInvalid = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], invalidPropertyPDF); - - if ("Renamed".equals(renameResp) - && "Updated".equals(upd1) - && "Updated".equals(upd2) - && "Updated".equals(upd3) - && "Updated".equals(upd4) - && "Updated".equals(updInvalid)) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - - // Update TXT properties - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - String upd = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - if ("Updated".equals(upd)) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } - - // Update EXE properties - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValueExe = integrationTestUtils.getDropDownValue(); - String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - for (int i = 0; i < facet.length; i++) { - RequestBody bodyDropdownExe = - RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - RequestBody bodyIntExe = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); - String upd2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - - if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - - // Verify PDF metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); - assertEquals(expectedNames[0], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertNull(metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } - - // Verify TXT metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); - assertEquals(expectedNames[1], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertTrue((Boolean) metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } - - // Verify EXE metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); - assertEquals(expectedNames[2], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); - assertEquals(1234, metadata.get("customProperty2")); - } - - // Parse JSON response and check for expected error messages - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved with expected invalid property errors"); - testStatus = true; - System.out.println( - "Rename & update unsuccessful for invalid properties and successful for valid attachments"); - } - } - } - } - - if (!testStatus) { - fail("Could not update secondary property before book is saved"); - } - } - - @Test - @Order(27) - void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - throws IOException { - System.out.println( - "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter attachments after book is saved"); - - // Reuse bookID5 and chapterID5 - System.out.println("Editing book with bookID5: " + bookID5); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - System.out.println("Edit entity response: " + response); - - if (response.equals("Entity in draft mode")) { - // Fetch existing attachments from the chapter - List> attachmentsMeta = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); - List> referencesMeta = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); - List> footnotesMeta = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); - - System.out.println("Attachments count: " + attachmentsMeta.size()); - System.out.println("References count: " + referencesMeta.size()); - System.out.println("Footnotes count: " + footnotesMeta.size()); - - if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) { - String[] pdfID = new String[facet.length]; - String[] txtID = new String[facet.length]; - String[] exeID = new String[facet.length]; - - pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); - pdfID[1] = (String) referencesMeta.get(0).get("ID"); - pdfID[2] = (String) footnotesMeta.get(0).get("ID"); - - txtID[0] = (String) attachmentsMeta.get(1).get("ID"); - txtID[1] = (String) referencesMeta.get(1).get("ID"); - txtID[2] = (String) footnotesMeta.get(1).get("ID"); - - exeID[0] = (String) attachmentsMeta.get(2).get("ID"); - exeID[1] = (String) referencesMeta.get(2).get("ID"); - exeID[2] = (String) footnotesMeta.get(2).get("ID"); - - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; - - String name1 = "sample.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // PDF - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); - - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); - - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); - - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); - - String updateSecondaryPropertyResponse5 = - api.updateInvalidSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated") - && updateSecondaryPropertyResponse5.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); - String updateSecondaryPropertyResponseTXT1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } - - Integer secondaryPropertyInt3 = 12; - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - for (int i = 0; i < facet.length; i++) { - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); - - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); - - if (updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // Note: Don't verify specific filenames since previous tests may have changed them - System.out.println("Save response: " + response); - - // Parse JSON response to check for invalid secondary property errors in all three tables - ObjectMapper mapper = new ObjectMapper(); - JsonNode root = mapper.readTree(response); - boolean hasAttachmentsError = false; - boolean hasReferencesError = false; - boolean hasFootnotesError = false; - if (root.isArray()) { - for (JsonNode node : root) { - String message = node.path("message").asText(); - if (message.contains("id1") && message.contains("Table: attachments")) { - hasAttachmentsError = true; - } - if (message.contains("id1") && message.contains("Table: references")) { - hasReferencesError = true; - } - if (message.contains("id1") && message.contains("Table: footnotes")) { - hasFootnotesError = true; - } - } - } - if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - System.out.println("Book saved"); - testStatus = true; - System.out.println( - "Rename & update unsuccessful for invalid Secondary properties and successful for valid property attachments"); - } else { - System.out.println("Save response did not match expected: " + response); - } - } else { - System.out.println("Not enough attachments in facets - need at least 3 per facet"); - } - } - } else { - System.out.println( - "Could not edit book - it may be stuck in draft mode from a previous test"); - } - if (!testStatus) { - fail("Could not update secondary property before book is saved"); - } - } - - // Tests 28 and 29 removed - chapters have no attachment limit - - // Tests 28-29 skipped - chapters have no attachment limit - - @Test - @Order(30) - void testDiscardBookDraftWithoutChapterAttachments() { - System.out.println("Test (30) : Discard book draft without adding chapter attachments"); - Boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!chapterResponse.equals("Could not create entity")) { - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if (response.equals("Entity Draft Deleted")) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft with chapter was not discarded properly"); - } - } - - @Test - @Order(31) - void testDiscardBookDraftWithChapterAttachments() throws IOException { - System.out.println("Test (31): Discard book draft with chapter attachments"); - boolean testStatus = false; - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (!"Could not create entity".equals(chapterResponse)) { - String tempChapterID = chapterResponse; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = - new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", tempChapterID); - 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 < facet.length; i++) { - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - if ("Attachment created".equals(createResponse.get(0))) { - System.out.println("Attachment created in chapter facet: " + facet[i]); - } else { - System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - } - } - - response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Book draft with chapter attachments was not discarded properly"); - } - } - - // Tests 32-34 covered in tests 19, 23, 24 // Tests 37-41 skipped - copy with notes/secondary - // properties not applicable - - @Test - @Order(42) - void testCreateLinkSuccessInChapter() throws IOException { - System.out.println("Test (42): Create link in chapter"); - List attachments = new ArrayList<>(); - - // Create book and chapter for link testing - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create book"); - } - String createLinkBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - if (chapterResponse.equals("Could not create entity")) { - fail("Could not create chapter"); - } - String createLinkChapterID = chapterResponse; - - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - for (String facetName : facet) { - String createLinkResponse1 = - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - String createLinkResponse2 = - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", linkUrl); - if (!createLinkResponse1.equals("Link created successfully") - || !createLinkResponse2.equals("Link created successfully")) { - fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); - } - } - - String saveEntityResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save book"); - } - - for (String facetName : facet) { - attachments = - api - .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) - .stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String openAttachmentResponse; - for (String attachment : attachments) { - openAttachmentResponse = - api.openAttachment( - appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open created link in chapter facet : " + facetName); - } - } - } - api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - } - - @Test - @Order(43) - void testCreateLinkDifferentChapter() throws IOException { - System.out.println("Test (43): Create link with same name in different chapter"); - - // Create new book and chapter - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (response.equals("Could not edit entity")) { - fail("Could not create book"); - } - String tempBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - if (chapterResponse.equals("Could not create entity")) { - fail("Could not create chapter"); - } - String tempChapterID = chapterResponse; - - String linkName = "sample"; - String linkUrl = "https://example.com"; - for (String facetName : facet) { - String createResponse = - api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); - if (!createResponse.equals("Link created successfully")) { - fail("Could not create link in different chapter with same name"); - } - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - if (!response.equals("Saved")) { - fail("Could not save book"); - } - - response = api.deleteEntity(appUrl, bookEntityName, tempBookID); - if (!response.equals("Entity Deleted")) { - fail("Could not delete book"); - } - } - - @Test - @Order(44) - void testCreateLinkFailureInChapter() throws IOException { - System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if ("Could not create entity".equals(response)) { - fail("Could not create book"); - } - String createLinkBookID = response; - - response = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - if ("Could not create entity".equals(response)) { - fail("Could not create chapter"); - } - String createLinkChapterID = response; - - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - - ObjectMapper mapper = new ObjectMapper(); - - for (String facetName : facet) { - - // Create initial link for this facet first (so duplicate test works) - response = - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - if (!"Link created successfully".equals(response)) { - fail("Could not create initial link for facet: " + facetName); - } - - /* ---------- INVALID URL ---------- */ - try { - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); - fail("Expected invalid URL error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - assertEquals("400018", error.path("code").asText()); - assertTrue( - error.path("message").asText().contains("expected pattern"), - "Unexpected message: " + error.path("message").asText()); - } - - /* ---------- INVALID NAME ---------- */ - try { - api.createLink( - appUrl, - chapterEntityName, - facetName, - createLinkChapterID, - "sample//", - "https://example.com"); - fail("Expected invalid name error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); - - assertEquals("500", error.path("code").asText()); - assertTrue( - message.contains("contains unsupported characters") - && message.contains("Rename and try again"), - "Unexpected message: " + message); - } - - /* ---------- EMPTY NAME & URL ---------- */ - try { - api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); - fail("Expected missing value error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - assertEquals("409008", error.path("code").asText()); - assertEquals("Provide the missing value.", error.path("message").asText()); - } - - /* ---------- DUPLICATE NAME ---------- */ - try { - api.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - fail("Expected duplicate name error"); - } catch (IOException e) { - JsonNode error = - mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - assertEquals("500", error.path("code").asText()); - assertEquals( - "An object named \"sample\" already exists. Rename the object and try again.", - error.path("message").asText()); - } - } - - response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - if (!"Saved".equals(response)) { - fail("Could not save book"); - } - - response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - if (!"Entity Deleted".equals(response)) { - fail("Could not delete book"); - } - } - - @Test - @Order(45) - void testCreateLinkNoSDMRolesInChapter() throws IOException { - System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); - - String createLinkBookNoRoles = - apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (createLinkBookNoRoles.equals("Could not edit entity")) { - fail("Could not create book"); - } - - String createLinkChapterNoRoles = - apiNoRoles.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); - if (createLinkChapterNoRoles.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - for (String facetName : facet) { - String linkName = "sample27"; - String linkUrl = "https://example.com"; - try { - apiNoRoles.createLink( - appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); - if (!response.equals("Saved")) { - fail("Could not save book"); - } - - response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); - if (!response.equals("Entity Deleted")) { - fail("Could not delete book"); - } - } - - @Test - @Order(46) - void testDeleteLinkInChapter() throws IOException { - System.out.println("Test (46): Delete link in chapter"); - List> attachments = new ArrayList<>(); - - String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create book"); - } - String deleteLinkBookID = response; - - String chapterResponse = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); - if (chapterResponse.equals("Could not create entity")) { - fail("Could not create chapter"); - } - String deleteLinkChapterID = chapterResponse; - - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink( - appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for chapter facet : " + facetName); - } - } - - String saveEntityResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save book"); - } - - for (String facetName : facet) { - attachments.add( - api - .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - .stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } - - String editEntityResponse = - api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - int index = 0; - for (String facetName : facet) { - String deleteLinkResponse = - api.deleteAttachment( - appUrl, - chapterEntityName, - facetName, - deleteLinkChapterID, - attachments.get(index).get(0)); - System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - if (!deleteLinkResponse.equals("Deleted")) { - fail("Could not delete created link"); - } - index += 1; - } - - saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save book"); - } - - index = 0; - attachments.clear(); - for (String facetName : facet) { - attachments.add( - api - .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - .stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - System.out.println( - "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - if (attachments.get(index).size() != 0) { - fail("Link wasn't deleted"); - } - index += 1; - } - - response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); - if (!response.equals("Entity Deleted")) { - fail("Could not delete book"); - } - } - - @Test - @Order(35) - void testCopyAttachmentsToNewChapterInSameBook() throws IOException { - System.out.println( - "Test (35): Copy attachments from one chapter to another new chapter in the same book"); - - // Create source book and chapter with attachments - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Load original files for copying content - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(new ArrayList<>()); - } - - // Create attachments in all facets - each upload needs a unique filename - int fileCounter = 0; - for (int i = 0; i < facet.length; i++) { - boolean useTxt = (i == 1); // Use txt for references facet - postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - File originalFile = useTxt ? originalTxt : originalPdf; - String extension = useTxt ? ".txt" : ".pdf"; - - for (int j = 0; j < 2; j++) { // Create 2 attachments per facet - // Create unique temp file for EACH upload to avoid duplicate filename errors - fileCounter++; - File tempFile = - File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - System.out.println("Created attachment ID: " + createResponse.get(1)); - } else { - System.out.println("Failed to create attachment: " + createResponse.get(0)); - fail("Could not create attachment in facet: " + facet[i]); - } - } - } - - // Fetch object IDs from source attachments - List objectIds = new ArrayList<>(); - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - if (metadata.containsKey("objectId")) { - objectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - } - - // Save the source book - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Create target chapter in the SAME book - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source book for adding target chapter"); - } - - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (targetChapterID.equals("Could not create entity")) { - fail("Could not create target chapter in same book"); - } - - // Copy attachments to target chapter - int objectIdIndex = 0; - for (String facetName : facet) { - List facetObjectIds = - objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, facetObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - } - - // Fetch and wait for copied attachments - List> copiedMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - for (Map meta : copiedMetadata) { - String copiedId = (String) meta.get("ID"); - } - objectIdIndex += 2; - } - - // Save the book with new chapter - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book after copying attachments"); - } - - // Verify attachments were copied - read them - for (String facetName : facet) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.size() != 2) { - fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); - } - for (Map meta : targetMetadata) { - String attachmentId = (String) meta.get("ID"); - String readResponse = - api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment in facet: " + facetName); - } - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - } - - @Test - @Order(36) - void testCopyAttachmentsToChapterInDifferentBook() throws IOException { - System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); - - // Create Book1 with source chapter and attachments - copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create Book2 with target chapter - copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Load original files for copying content - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(new ArrayList<>()); - } - - // Create attachments in all facets of source chapter - each upload needs unique filename - int fileCounter = 0; - for (int i = 0; i < facet.length; i++) { - boolean useTxt = (i == 1); // Use txt for references facet - postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - File originalFile = useTxt ? originalTxt : originalPdf; - String extension = useTxt ? ".txt" : ".pdf"; - - for (int j = 0; j < 2; j++) { - // Create unique temp file for EACH upload to avoid duplicate filename errors - fileCounter++; - File tempFile = - File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - System.out.println("Created attachment ID: " + createResponse.get(1)); - } else { - System.out.println("Failed to create attachment: " + createResponse.get(0)); - fail("Could not create attachment in facet: " + facet[i]); - } - } - } - - // Fetch object IDs - sourceObjectIds.clear(); - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - } - - // Save Book1 - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Copy attachments from Book1's chapter to Book2's chapter - if (sourceObjectIds.size() == 6) { - int objectIdIndex = 0; - for (String facetName : facet) { - List facetObjectIds = - sourceObjectIds.subList( - objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, facetObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - } - - objectIdIndex += 2; - } - - // Save Book2 - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book after copying attachments"); - } - - // Verify attachments were copied - for (String facetName : facet) { - List> targetMetadata = - api.fetchEntityMetadata( - appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); - if (targetMetadata.size() != 2) { - fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); - } - for (Map meta : targetMetadata) { - String attachmentId = (String) meta.get("ID"); - String readResponse = - api.readAttachment( - appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, attachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment in facet: " + facetName); - } - } - } - - // Cleanup - delete both books after verification - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - } else { - fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); - } - } - - @Test - @Order(37) - void testCopyAttachmentsWithNotePreserved() throws IOException { - System.out.println("Test (37): Copy attachments with note field preserved"); - - // Create source book and chapter - copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create attachments with notes in source chapter - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] sourceAttachmentIds = new String[facet.length]; - String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); - - for (int i = 0; i < facet.length; i++) { - // Create unique temp file for each facet - File tempFile = - File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - sourceAttachmentIds[i] = createResponse.get(1); - - // Update note field using RequestBody - String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - String noteResponse = - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - noteBody); - if (!noteResponse.equals("Updated")) { - fail("Could not update note for attachment in facet: " + facet[i]); - } - System.out.println("Note updated for facet: " + facet[i]); - } - - // Save source book - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Verify notes were saved in source - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - if (!testNote.equals(metadata.get("note"))) { - fail("Note not saved correctly in source for facet: " + facet[i]); - } - } - - // Create target book and chapter - copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Get object IDs and copy attachments - for (int i = 0; i < facet.length; i++) { - Map sourceMetadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - String objectId = sourceMetadata.get("objectId").toString(); - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - System.out.println("Attachment copied to facet: " + facet[i]); - } - - // Save target book - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify notes were preserved in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); - if (targetMetadata.isEmpty()) { - fail("No attachments found in target facet: " + facet[i]); - } - Map copiedAttachment = targetMetadata.get(0); - String copiedNote = (String) copiedAttachment.get("note"); - if (!testNote.equals(copiedNote)) { - fail( - "Note not preserved after copy in facet: " - + facet[i] - + ". Expected: " - + testNote - + ", Got: " - + copiedNote); - } - System.out.println("Note preserved in target facet: " + facet[i]); - } - - System.out.println("Test 37 passed - notes preserved during copy"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - copyAttachmentSourceBook = null; - copyAttachmentTargetBook = null; - } - - @Test - @Order(38) - void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { - System.out.println("Test (38): Copy attachments with secondary properties preserved"); - - // Use entities from test 37 or create new ones if needed - boolean sourceBookJustCreated = false; - boolean targetBookJustCreated = false; - - if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - copyAttachmentSourceBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - sourceBookJustCreated = true; - } - - if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - copyAttachmentTargetBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - targetBookJustCreated = true; - } - - // If source book was just created, save it first before we can edit it - if (sourceBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created source book"); - } - } - - // If target book was just created, save it first - if (targetBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created target book"); - } - } - - // Edit source book - String editResponse = - api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source book"); - } - - // Create new attachments with secondary properties - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] sourceAttachmentIds = new String[facet.length]; - Boolean testBooleanProp = true; - Integer testIntegerProp = 12345; - - for (int i = 0; i < facet.length; i++) { - // Create unique temp file - File tempFile = - File.createTempFile( - "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - sourceAttachmentIds[i] = createResponse.get(1); - - // Update secondary properties using RequestBody (customProperty6 - Boolean, customProperty2 - - // Integer) - String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - String boolResponse = - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - boolBody); - if (!boolResponse.equals("Updated")) { - System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); - } - - String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - String intResponse = - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - intBody); - if (!intResponse.equals("Updated")) { - System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); - } - - System.out.println("Secondary properties updated for facet: " + facet[i]); - } - - // Save source book - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Edit target book - editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - // Copy attachments to target - for (int i = 0; i < facet.length; i++) { - Map sourceMetadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - String objectId = sourceMetadata.get("objectId").toString(); - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); - } - - // Save target book - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify secondary properties were preserved in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); - - // Find the attachment we just copied (most recent one) - boolean found = false; - for (Map attachment : targetMetadata) { - Object boolProp = attachment.get("customProperty6"); - Object intProp = attachment.get("customProperty2"); - - if (boolProp != null && intProp != null) { - if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { - found = true; - System.out.println("Secondary properties preserved in target facet: " + facet[i]); - break; - } - } - } - if (!found) { - System.out.println( - "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); - } - } - - System.out.println("Test 38 passed - secondary properties checked during copy"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - copyAttachmentSourceBook = null; - copyAttachmentTargetBook = null; - } - - @Test - @Order(39) - void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { - System.out.println( - "Test (39): Copy attachments with both note and secondary properties preserved"); - - // Use entities from previous tests or create new ones - boolean sourceBookJustCreated = false; - boolean targetBookJustCreated = false; - - if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - copyAttachmentSourceBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentSourceBook.equals("Could not create entity")) { - fail("Could not create source book"); - } - copyAttachmentSourceChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - if (copyAttachmentSourceChapter.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - sourceBookJustCreated = true; - } - - if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - copyAttachmentTargetBook = - api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (copyAttachmentTargetBook.equals("Could not create entity")) { - fail("Could not create target book"); - } - copyAttachmentTargetChapter = - api.createEntityDraft( - appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - if (copyAttachmentTargetChapter.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - targetBookJustCreated = true; - } - - // If source book was just created, save it first before we can edit it - if (sourceBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created source book"); - } - } - - // If target book was just created, save it first - if (targetBookJustCreated) { - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save newly created target book"); - } - } - - // Edit source book - String editResponse = - api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source book"); - } - - // Create new attachments with both note and secondary properties - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", copyAttachmentSourceChapter); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String[] sourceAttachmentIds = new String[facet.length]; - String testNote = "Combined test note - " + System.currentTimeMillis(); - Boolean testBooleanProp = true; - Integer testIntegerProp = 99999; - - for (int i = 0; i < facet.length; i++) { - // Create unique temp file - File tempFile = - File.createTempFile( - "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - List createResponse = - api.createAttachment( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - srvpath, - postData, - tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - sourceAttachmentIds[i] = createResponse.get(1); - - // Update note using RequestBody - String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - noteBody); - - // Update secondary properties using RequestBody - String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - boolBody); - - String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - api.updateSecondaryProperty( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i], - intBody); - - System.out.println("Note and secondary properties updated for facet: " + facet[i]); - } - - // Save source book - String saveResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Edit target book - editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - // Copy attachments to target - for (int i = 0; i < facet.length; i++) { - Map sourceMetadata = - api.fetchMetadata( - appUrl, - chapterEntityName, - facet[i], - copyAttachmentSourceChapter, - sourceAttachmentIds[i]); - String objectId = sourceMetadata.get("objectId").toString(); - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - - String copyResponse = - api.copyAttachment( - appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - System.out.println("Attachment with note and properties copied to facet: " + facet[i]); - } - - // Save target book - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify note and secondary properties were preserved in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); - - boolean noteFound = false; - boolean propsFound = false; - - for (Map attachment : targetMetadata) { - String copiedNote = (String) attachment.get("note"); - Object boolProp = attachment.get("customProperty6"); - Object intProp = attachment.get("customProperty2"); - - if (testNote.equals(copiedNote)) { - noteFound = true; - System.out.println("Note preserved in target facet: " + facet[i]); - } - - if (boolProp != null && intProp != null) { - if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { - propsFound = true; - System.out.println("Secondary properties preserved in target facet: " + facet[i]); - } - } - } - - if (!noteFound) { - System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); - } - if (!propsFound) { - System.out.println( - "Warning: Secondary properties may not be preserved in facet: " + facet[i]); - } - } - - // Cleanup - delete both books - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - - // Reset static variables - copyAttachmentSourceBook = null; - copyAttachmentTargetBook = null; - copyAttachmentSourceChapter = null; - copyAttachmentTargetChapter = null; - - System.out.println("Test 39 passed - both note and secondary properties checked during copy"); - } - - @Test - @Order(40) - void testCopyAttachmentsWithInvalidObjectId() throws IOException { - System.out.println("Test (40): Copy attachments with invalid object ID should fail"); - - // Create independent test entities (don't rely on previous tests) - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create test book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create test chapter"); - } - - // Save the book first so it's not in draft mode - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save test book"); - } - - // Now edit it to test copy with invalid object IDs - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit test book"); - } - - // Try to copy with invalid object ID - for (String facetName : facet) { - try { - List invalidObjectIds = new ArrayList<>(); - invalidObjectIds.add("invalidObjectId123"); - invalidObjectIds.add("anotherInvalidId456"); - api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, invalidObjectIds); - fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); - } catch (IOException e) { - // Expected - copy should fail with invalid object ID - System.out.println( - "Expected error received for invalid object ID in facet " - + facetName - + ": " - + e.getMessage()); - } - } - - // Save and cleanup - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - - // Also cleanup test 36 entities if they exist - if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { - try { - api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - } catch (Exception e) { - // Ignore - may already be deleted - } - } - if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { - try { - api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - } catch (Exception e) { - // Ignore - may already be deleted - } - } - } - - @Test - @Order(41) - void testCopyAttachmentsToExistingChapter() throws IOException { - System.out.println( - "Test (41): Copy attachments to an existing chapter that already has attachments"); - - // Create Book1 with source chapter - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create Book2 with target chapter that has existing attachments - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (targetBookID.equals("Could not create entity")) { - fail("Could not create target book"); - } - - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - if (targetChapterID.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Create temp files with unique names to avoid duplicate filename errors - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test41_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create attachment in source chapter - List sourceObjIds = new ArrayList<>(); - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create source attachment"); - } - String attachmentId = createResponse.get(1); - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - sourceObjIds.add(metadata.get("objectId").toString()); - } - - // Create existing attachment in target chapter - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", targetChapterID); - postData.put("mimeType", "text/plain"); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create existing target attachment"); - } - String attachmentId = createResponse.get(1); - } - - // Save both books - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Edit target book and copy attachments - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - // Copy from source to target (target already has 1 attachment per facet) - for (int i = 0; i < facet.length; i++) { - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjIds.get(i)); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, objectIdsToCopy); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to facet: " + facet[i]); - } - } - - // Save target book - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) - for (String facetName : facet) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.size() != 2) { - fail( - "Expected 2 attachments in facet " - + facetName - + " (1 existing + 1 copied), found " - + targetMetadata.size()); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - // ============= LINK RENAME TESTS (47-49) ============= - - @Test - @Order(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (47): Rename link in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links in all facets - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit and rename links - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - String renameResponse = - api.renameAttachment( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); - if (!renameResponse.equals("Renamed")) { - fail("Could not rename link in facet: " + facetName); - } - } - - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book after renaming links"); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(48) - void testRenameLinkDuplicate() throws IOException { - System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create two links in all facets - for (String facetName : facet) { - String createLinkResponse1 = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "link1", - "https://www.example1.com"); - String createLinkResponse2 = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "link2", - "https://www.example2.com"); - if (!createLinkResponse1.equals("Link created successfully") - || !createLinkResponse2.equals("Link created successfully")) { - fail("Could not create links in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit and try to rename link2 to link1 (duplicate) - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.size() < 2) { - fail("Expected 2 links in facet: " + facetName); - } - - // Find link2 and rename to link1 - for (Map attachment : attachments) { - if ("link2".equals(attachment.get("fileName"))) { - String linkId = (String) attachment.get("ID"); - api.renameAttachment( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); - break; - } - } - } - - // Save should fail with duplicate error - String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - ObjectMapper mapper = new ObjectMapper(); - try { - JsonNode errorJson = mapper.readTree(saveError); - String errorMessage = errorJson.path("error").path("message").asText(); - if (!errorMessage.contains("already exists")) { - fail("Expected duplicate error but got: " + saveError); - } - } catch (Exception e) { - if (!saveError.contains("already exists")) { - fail("Expected duplicate error but got: " + saveError); - } - } - - // Cleanup - api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(49) - void testRenameLinkUnsupportedCharacters() throws IOException { - System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links in all facets - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit and rename with unsupported characters - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - api.renameAttachment( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); - } - - // Save should fail with unsupported characters error - String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveError.contains("unsupported characters")) { - fail("Expected unsupported characters error but got: " + saveError); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - // ============= LINK EDIT TESTS (50-53) ============= - - @Test - @Order(50) - void testEditLinkSuccess() throws IOException { - System.out.println("Test (50): Edit existing link URL in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links in all facets - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit links - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - String editLinkResponse = - api.editLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - linkId, - "https://www.editedexample.com"); - if (!editLinkResponse.equals("Link edited successfully")) { - fail("Could not edit link in facet: " + facetName); - } - } - - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book after editing links"); - } - - // Verify links open successfully - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - for (Map attachment : attachments) { - String linkId = (String) attachment.get("ID"); - String openResponse = - api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); - if (!openResponse.equals("Attachment opened successfully")) { - fail("Could not open edited link in facet: " + facetName); - } - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(51) - void testEditLinkFailureInvalidURL() throws IOException { - System.out.println("Test (51): Edit link with invalid URL fails in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create links - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - try { - api.editLink( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://editedexample"); - fail("Edit link should have failed with invalid URL in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected error received for invalid URL in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(52) - void testEditLinkFailureEmptyURL() throws IOException { - System.out.println("Test (52): Edit link with empty URL fails in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - try { - api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); - fail("Edit link should have failed with empty URL in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected error received for empty URL in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - for (String facetName : facet) { - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facetName, - testChapterID, - "sample", - "https://www.example.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - for (String facetName : facet) { - List> attachments = - apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - if (attachments.isEmpty()) { - fail("No links found in facet: " + facetName); - } - - String linkId = (String) attachments.get(0).get("ID"); - try { - apiNoRoles.editLink( - appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://www.edited.com"); - fail("Edit link should have failed without SDM roles in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected permission error received in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - // ============= COPY LINK TESTS (54-58) ============= - - @Test - @Order(54) - void testCopyLinkSuccessNewChapter() throws IOException { - System.out.println("Test (54): Copy link from one chapter to another new chapter"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - if (sourceChapterID.equals("Could not create entity") - || targetChapterID.equals("Could not create entity")) { - fail("Could not create chapters"); - } - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - // Create links in source chapter - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch object IDs - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - // Copy links to target chapter - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link for facet " + facetName + ": " + copyResponse); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify link type and URL - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.isEmpty()) { - fail("No links found in target chapter for facet: " + facetName); - } - - Map copiedLink = targetMetadata.get(0); - String receivedUrl = (String) copiedLink.get("linkUrl"); - assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); - - objectIdIndex++; - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(55) - void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { - System.out.println("Test (55): Copy invalid link object ID to chapter fails"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - for (String facetName : facet) { - try { - List invalidObjectIds = new ArrayList<>(); - invalidObjectIds.add("incorrectObjectId"); - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, invalidObjectIds); - fail("Copy should have thrown error for invalid object ID in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected error received for invalid object ID in facet " + facetName); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(56) - void testCopyLinkToExistingChapter() throws IOException { - System.out.println("Test (56): Copy link to existing chapter that has attachments"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - // Create links in source chapter - for (int i = 0; i < facet.length; i++) { - String linkName = "sourceLink" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in source chapter for facet: " + facet[i]); - } - } - - // Create existing links in target chapter - for (int i = 0; i < facet.length; i++) { - String linkName = "existingLink" + i; - String createLinkResponse = - api.createLink( - appUrl, - chapterEntityName, - facet[i], - targetChapterID, - linkName, - "https://www.existing.com"); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create existing link in target chapter for facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch source object IDs - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - // Copy links to target chapter - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link for facet " + facetName); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify target has 2 links (existing + copied) - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.size() != 2) { - fail( - "Expected 2 links in target chapter facet " - + facetName - + ", found " - + targetMetadata.size()); - } - - objectIdIndex++; - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(57) - void testCopyLinkNoSDMRoles() throws IOException { - System.out.println("Test (57): Copy link fails due to no SDM roles"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch object IDs - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - // Try to copy with no SDM roles - int objectIdIndex = 0; - for (String facetName : facet) { - try { - // Use normal api to put book in draft mode - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // Use apiNoRoles to attempt copy (should fail) - apiNoRoles.copyAttachment( - appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - fail("Copy should have failed without SDM roles in facet: " + facetName); - } catch (IOException e) { - System.out.println("Expected permission error in facet " + facetName); - // Discard draft to clean up for next iteration - api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); - } - objectIdIndex++; - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(58) - void testCopyLinkFromDraftChapter() throws IOException { - System.out.println("Test (58): Copy link from draft chapter to another chapter"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - String linkUrl = "https://www.example.com"; - List linkObjectIds = new ArrayList<>(); - - // Create links in source chapter (NOT saved yet - draft mode) - for (int i = 0; i < facet.length; i++) { - String linkName = "draftLink" + i; - String createLinkResponse = - api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - - // Save target book only - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch object IDs from draft - for (int i = 0; i < facet.length; i++) { - List> metadata = - api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); - for (Map meta : metadata) { - if (meta.containsKey("objectId")) { - linkObjectIds.add(meta.get("objectId").toString()); - } - } - } - - if (linkObjectIds.size() != facet.length) { - fail("Could not fetch all object IDs from draft"); - } - - // Copy links from draft to target - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link from draft for facet " + facetName); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify link was copied - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.isEmpty()) { - fail("No links found in target chapter for facet: " + facetName); - } - - objectIdIndex++; - } - - // Cleanup - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= - - @Test - @Order(59) - void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { - System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - if (sourceChapterID.equals("Could not create entity") - || targetChapterID.equals("Could not create entity")) { - fail("Could not create chapters"); - } - - // Create temp files with unique names - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test59_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List sourceObjectIds = new ArrayList<>(); - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(new ArrayList<>()); - } - - // Create attachments in source chapter (still in draft) - for (int i = 0; i < facet.length; i++) { - postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); - File file = i == 1 ? tempTxt : tempPdf; - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment in facet: " + facet[i]); - } - } - - // Fetch object IDs from draft - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - Map metadata = - api.fetchMetadataDraft( - appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - } - - // Save target book only - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Copy attachments from draft to target - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment from draft for facet " + facetName); - } - - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book"); - } - - // Verify attachment was copied - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - if (targetMetadata.isEmpty()) { - fail("No attachments found in target chapter for facet: " + facetName); - } - - // Read attachment to verify - String attachmentId = (String) targetMetadata.get(0).get("ID"); - String readResponse = - api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment in facet: " + facetName); - } - - objectIdIndex++; - } - - // Cleanup - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - // ============= CHANGELOG TESTS (60-64) ============= - - @Test - @Order(60) - void testViewChangelogForNewlyCreatedAttachment() throws IOException { - System.out.println("Test (60): View changelog for newly created attachment in chapter"); - - for (int i = 0; i < facet.length; i++) { - String facetName = facet[i]; - - // Create book and chapter - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = - File.createTempFile( - "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } - - String attachmentId = createResponse.get(1); - - // Fetch changelog - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); - - // Cleanup - api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - } - } - - @Test - @Order(61) - void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - System.out.println("Test (61): Changelog after modifying note and custom property in chapter"); - - for (int i = 0; i < facet.length; i++) { - String facetName = facet[i]; - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = - File.createTempFile( - "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Update note - String notesValue = "Test note for changelog verification"; - RequestBody updateNotesBody = - RequestBody.create( - MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); - - // Update custom property - RequestBody bodyInt = - RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 12345}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); - - // Save - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit to fetch changelog - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - // Fetch changelog - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - int numItems = (int) changelogResponse.get("numItems"); - assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - } - - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println("Test (62): Changelog after renaming attachment in chapter"); - - for (int i = 0; i < facet.length; i++) { - String facetName = facet[i]; - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = - File.createTempFile( - "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Rename attachment - String renameResponse = - api.renameAttachment( - appUrl, - chapterEntityName, - facetName, - testChapterID, - attachmentId, - "renamed_file.txt"); - if (!renameResponse.equals("Renamed")) { - fail("Could not rename attachment"); - } - - // Save - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book"); - } - - // Edit to fetch changelog - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit book"); - } - - // Fetch changelog - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - int numItems = (int) changelogResponse.get("numItems"); - assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - } - - @Test - @Order(63) - void testChangelogForCopiedAttachment() throws IOException { - System.out.println("Test (63): Changelog for copied attachment in chapter"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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 in source - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Save both books - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Get object ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - - // Copy to target - String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target book"); - } - - List objectIds = new ArrayList<>(); - objectIds.add(objectId); - String copyResponse = - api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment"); - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Fetch changelog for copied attachment - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); - - editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - Map changelogResponse = - api.fetchChangelog( - appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - int numItems = (int) changelogResponse.get("numItems"); - assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(64) - void testChangelogForNewChapter() throws IOException { - System.out.println("Test (64): Changelog for attachment in newly created chapter"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - if (testChapterID.equals("Could not create entity")) { - fail("Could not create chapter"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), ".txt"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalFile.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", testChapterID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String attachmentId = createResponse.get(1); - - // Fetch changelog before saving - Map changelogResponse = - api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - - assertNotNull(changelogResponse, "Changelog response should not be null"); - assertEquals( - 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); - - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); - - // Cleanup - api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - } - - // ============= MOVE ATTACHMENT TESTS (65-75) ============= - - @Test - @Order(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println("Test (65): Move attachments from source chapter to target chapter"); - - for (int i = 0; i < facet.length; i++) { - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create temp files - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempPdf.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), - tempTxt.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List sourceAttachmentIds = new ArrayList<>(); - File[] files = {tempPdf, tempTxt}; - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source chapter"); - } - } - - // Save source book - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save source book"); - } - - // Get object IDs and folder ID - List moveObjectIds = new ArrayList<>(); - String sourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (sourceFolderId == null && metadata.containsKey("folderId")) { - sourceFolderId = metadata.get("folderId").toString(); - } - } - } - - // Create target book and chapter - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (targetBookID.equals("Could not create entity")) { - fail("Could not create target book"); - } - - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - if (targetChapterID.equals("Could not create entity")) { - fail("Could not create target chapter"); - } - - // Save target book before moving attachments (moveAttachments requires Active entity) - saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save target book before move"); - } - - // Move attachments to Active entity - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[i], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } - - // Verify - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - assertEquals( - sourceAttachmentIds.size(), - targetMetadata.size(), - "Target should have all attachments after move"); - - List> sourceMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, targetBookID); - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - } - } - - @Test - @Order(66) - void testMoveAttachmentsToChapterWithDuplicate() throws IOException { - System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Create attachment in source with specific name - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, originalPdf); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create source attachment"); - } - String sourceAttachmentId = createResponse.get(1); - - // Create attachment in target with SAME name (duplicate) - postData.put("up__ID", targetChapterID); - createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, originalPdf); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create target attachment"); - } - - // Save both - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Get source object ID and folder ID - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, sourceAttachmentId); - String objectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move to saved target - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Move should handle duplicate - attachment stays in source - - // Verify source still has attachment (duplicate not moved) - List> sourceMetadataAfter = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertTrue( - sourceMetadataAfter.size() >= 1, - "Source should still have attachment when duplicate exists in target"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(67) - void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { - System.out.println("Test (67): Move attachments with notes and secondary properties"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - if (sourceChapterID.equals("Could not create entity")) { - fail("Could not create source chapter"); - } - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - // Add note and secondary property - String testNote = "Test note for move"; - RequestBody noteBody = - RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + "\"}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); - - RequestBody propBody = - RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); - api.updateSecondaryProperty( - appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); - - // Save source - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get object ID and folder ID - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // Create target - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null"); - } - - // Verify note was preserved - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - if (!targetMetadata.isEmpty()) { - String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); - Map movedMetadata = - api.fetchMetadata( - appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); - - // Note should be preserved - if (movedMetadata.containsKey("note")) { - assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); - } - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(68) - void testMoveAttachmentsPartialFailure() throws IOException { - System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get real object ID and folder ID - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String realObjectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // Create target - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Try to move with mix of valid and invalid object IDs - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(realObjectId); - moveObjectIds.add("invalidObjectId123"); - - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Should handle partial failure - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(69) - void testMoveAttachmentsEmptyList() throws IOException { - System.out.println("Test (69): Move attachments with empty object ID list"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Try to move with empty list - List emptyObjectIds = new ArrayList<>(); - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - - try { - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - "someFolderId", - emptyObjectIds, - targetFacet, - sourceFacet); - // Should either fail or do nothing - } catch (Exception e) { - System.out.println("Expected: Move with empty list handled: " + e.getMessage()); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(70) - void testMoveAttachmentsToSameChapter() throws IOException { - System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String testChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - 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"); - - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Get object ID and folder ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String folderId = metadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move to same chapter - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - testChapterID, - folderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Should handle gracefully - attachment stays in place - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Verify attachment still exists - List> metadataAfter = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); - assertEquals(1, metadataAfter.size(), "Attachment should still exist"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(71) - void testMoveAttachmentsBetweenFacets() throws IOException { - System.out.println("Test (71): Move attachments between different facets in chapters"); - - String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (testBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - // Create in attachments facet - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // Get object ID and folder ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String sourceFolderId = metadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move from attachments to references facet - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[1], // references facet - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify moved to different facet - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); - assertTrue( - targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, testBookID); - } - - @Test - @Order(72) - void testMoveMultipleAttachments() throws IOException { - System.out.println("Test (72): Move multiple attachments at once between chapters"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // Create multiple temp files - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - String uniqueSuffix = "_test72_" + System.currentTimeMillis(); - File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); - File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); - tempPdf.deleteOnExit(); - tempTxt.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - java.nio.file.Files.copy( - originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List sourceAttachmentIds = new ArrayList<>(); - File[] files = {tempPdf, tempTxt}; - String[] mimeTypes = {"application/pdf", "text/plain"}; - - for (int i = 0; i < files.length; i++) { - postData.put("mimeType", mimeTypes[i]); - List createResponse = - api.createAttachment( - appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get object IDs - List moveObjectIds = new ArrayList<>(); - String sourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - moveObjectIds.add(metadata.get("objectId").toString()); - if (sourceFolderId == null) { - sourceFolderId = metadata.get("folderId").toString(); - } - } - - // Create target - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Move all at once - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - Map moveResult = - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify all moved - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - assertEquals( - sourceAttachmentIds.size(), - targetMetadata.size(), - "All attachments should be moved to target"); - - List> sourceMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(73) - void testMoveAttachmentsAllFacets() throws IOException { - System.out.println("Test (73): Move attachments from all facets between chapters"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || targetBookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String targetChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Create attachment in each facet - for (int i = 0; i < facet.length; i++) { - String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); - File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), - tempFile.toPath(), - java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facet[i]); - } - } - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // Move from each facet - for (int i = 0; i < facet.length; i++) { - List> sourceMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - if (sourceMetadata.isEmpty()) { - continue; - } - - String attachmentId = (String) sourceMetadata.get(0).get("ID"); - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String sourceFolderId = metadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - api.moveAttachment( - appUrl, - chapterEntityName, - facet[i], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - } - - // Verify all facets have attachments in target - for (int i = 0; i < facet.length; i++) { - List> targetMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + facet[i]); - } - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(74) - void testChainMoveAttachments() throws IOException { - System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - if (sourceBookID.equals("Could not create entity") - || target1BookID.equals("Could not create entity") - || target2BookID.equals("Could not create entity")) { - fail("Could not create books"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - String target1ChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); - String target2ChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); - api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); - - // First move: Source -> Target1 - Map sourceMetadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = sourceMetadata.get("objectId").toString(); - String sourceFolderId = sourceMetadata.get("folderId").toString(); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - // Move to target1 - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - target1ChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify in target1 - List> target1Metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); - - // Second move: Target1 -> Target2 - String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); - Map target1AttMetadata = - api.fetchMetadata( - appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); - String target1ObjectId = target1AttMetadata.get("objectId").toString(); - String target1FolderId = target1AttMetadata.get("folderId").toString(); - - moveObjectIds.clear(); - moveObjectIds.add(target1ObjectId); - - // Move to target2 - api.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - target2ChapterID, - target1FolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify final state - List> target2Metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); - assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); - - target1Metadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); - - List> sourceFinalMetadata = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, target1BookID); - api.deleteEntity(appUrl, bookEntityName, target2BookID); - } - - @Test - @Order(75) - void testMoveAttachmentsWithoutSDMRole() throws IOException { - System.out.println("Test (75): Move attachments fails without SDM role"); - - String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (sourceBookID.equals("Could not create entity")) { - fail("Could not create source book"); - } - - String sourceChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // Create temp file - ClassLoader classLoader = getClass().getClassLoader(); - File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - File tempFile = - File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); - tempFile.deleteOnExit(); - java.nio.file.Files.copy( - originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", sourceChapterID); - 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - String attachmentId = createResponse.get(1); - - api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // Get object ID and folder ID - Map metadata = - api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - String objectId = metadata.get("objectId").toString(); - String sourceFolderId = metadata.get("folderId").toString(); - - // Create target with no role user - String targetBookID = - apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (targetBookID.equals("Could not create entity")) { - fail("Could not create target book"); - } - - String targetChapterID = - apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // Save target before move - apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - List moveObjectIds = new ArrayList<>(); - moveObjectIds.add(objectId); - - String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - boolean moveFailed = false; - String errorMessage = null; - - try { - Map moveResult = - apiNoRoles.moveAttachment( - appUrl, - chapterEntityName, - facet[0], - targetChapterID, - sourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null || moveResult.containsKey("error")) { - moveFailed = true; - errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; - } - } catch (Exception e) { - moveFailed = true; - errorMessage = e.getMessage(); - } - - assertTrue(moveFailed, "Move should fail without SDM role"); - System.out.println("Move correctly failed without SDM role: " + errorMessage); - - // Verify source still has attachment - List> sourceMetadataAfter = - api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); - - // Cleanup - api.deleteEntity(appUrl, bookEntityName, sourceBookID); - api.deleteEntity(appUrl, bookEntityName, targetBookID); - } - - @Test - @Order(76) - void testRenameChapterAttachmentWithExtensionChange() throws IOException { - System.out.println( - "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); - - // Step 1: Create a new book and chapter - String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (newBookID.equals("Could not create entity")) { - fail("Could not create book"); - } - String newChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - if (newChapterID.equals("Could not create entity")) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not create chapter"); - } - String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - if (!saveResponse.equals("Saved")) { - fail("Could not save book: " + saveResponse); - } - - // Step 2: Upload a PDF attachment to each facet in the chapter - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", newChapterID); - 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, bookEntityName, srvpath, newBookID); - if (!"Entity in draft mode".equals(editResponse)) { - fail("Could not put book in draft mode for PDF upload"); - } - - String[] facetAttachmentIDs = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - facetAttachmentIDs[i] = - CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - if (facetAttachmentIDs[i] == null) { - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - } - } - - // Step 3: Save the book - String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - if (!savedAfterUpload.equals("Saved")) { - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not save book after PDF upload: " + savedAfterUpload); - } - - // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt - for (int i = 0; i < facet.length; i++) { - String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - if (!"Entity in draft mode".equals(editDraftResponse)) { - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not put book in draft mode for rename on facet: " + facet[i]); - } - - String renameResponse = - api.renameAttachment( - appUrl, - chapterEntityName, - facet[i], - newChapterID, - facetAttachmentIDs[i], - "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - api.deleteEntity(appUrl, bookEntityName, newBookID); - fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - } - - // Step 6: Save and validate the extension change warning message - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } - - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - } - - // Clean up - api.deleteEntity(appUrl, bookEntityName, newBookID); - } - - @Test - @Order(77) - void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { - System.out.println( - "Test (77) : Upload chapter attachment in draft, rename changing extension before save across all facets - should return extension change warning"); - - for (int i = 0; i < facet.length; i++) { - // Step 1: Create a new book and chapter draft (do NOT save) - String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - if (newBookID.equals("Could not create entity")) { - fail("Could not create book for facet: " + facet[i]); - } - String newChapterID = - api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - if (newChapterID.equals("Could not create entity")) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not create chapter for facet: " + facet[i]); - } - - // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft (unsaved) - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", newChapterID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String facetAttachmentID = - CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - if (facetAttachmentID == null) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - } - - // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not saved - String renameResponse = - api.renameAttachment( - appUrl, - chapterEntityName, - facet[i], - newChapterID, - facetAttachmentID, - "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - } - - // Step 4: Save the book — should receive extension change warning, not "Saved" - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } - - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - - // Clean up - api.deleteEntity(appUrl, bookEntityName, newBookID); - } - } + // @Test + // @Order(7) + // void testRenameChapterAttachments() { + // System.out.println("Test (7) : Rename single attachment, reference, and footnote in + // chapter"); + // Boolean testStatus = true; + + // try { + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + // if ("Entity in draft mode".equals(response)) { + // String[] name = {"sample123", "reference123", "footnote123"}; + // for (int i = 0; i < facet.length; i++) { + // // Read the facet to ensure it exists + // response = + // api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], + // name[i]); + // if (!"Renamed".equals(response)) { + // testStatus = false; + // System.out.println(facet[i] + " was not renamed: " + response); + // } + // } + // // Save book draft if everything is renamed + // if (testStatus) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if (!"Saved".equals(response)) { + // testStatus = false; + // System.out.println("Book draft was not saved: " + response); + // } + // } else { + // // Attempt save despite potential rename failures + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // } + // } else { + // testStatus = false; + // System.out.println("Book was not put into draft mode: " + response); + // } + // } catch (Exception e) { + // testStatus = false; + // System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); + // } + + // if (!testStatus) { + // fail("There was an error during the rename test process for chapter."); + // } + // } + + // @Test + // @Order(8) + // void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { + // System.out.println("Test (8): Create chapter attachments with unsupported characters"); + // 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("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, bookEntityName, srvpath, bookID); + // if (!"Entity in draft mode".equals(response)) { + // fail("Book not in draft mode: " + response); + // return; + // } + + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", chapterID); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); + + // if (!"Attachment created".equals(createResponse.get(0))) { + // fail("Could not create attachment in chapter facet: " + facet[i]); + // return; + // } + + // String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE + // response = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); + + // System.out.println("Rename response for chapter " + facet[i] + ": " + response); + // } + + // // Save should fail + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + // // ---------------- PARSE JSON ---------------- + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // String message = root.path("error").path("message").asText(); + + // // ---------------- NORMALIZE MESSAGE ---------------- + // // 1. Normalize smart quotes + // // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared + // message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); + + // // ---------------- EXPECTED MESSAGE (EXACT) ---------------- + // String expectedMessage = + // "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" + // + "Table: attachments\n" + // + "Page: IntegrationTestEntity"; + + // if (message.equals(expectedMessage)) { + + // for (int i = 0; i < facet.length; i++) { + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + + // if (!testStatus) { + // fail("Test for unsupported characters in chapter attachments failed"); + // } + // } + + // @Test + // @Order(9) + // void testRenameSingleDuplicateInChapter() throws IOException { + // System.out.println( + // "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); + // Boolean testStatus = false; + // int counter = 0; + + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Edit entity response: " + response); + + // if ("Entity in draft mode".equals(response)) { + // // To create a duplicate within the same facet, we need to rename ID2[i] to + // // the same name as an existing file in that facet. After test 7, the existing files are: + // // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) + // // We rename ID2[i] (sample123.txt from test 8) to these names which already exist + // String[] duplicateNames = {"sample123", "reference123", "footnote123"}; + // String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; + + // // Try to rename to duplicate file names (names that already exist in each facet) + // for (int i = 0; i < facet.length; i++) { + // response = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); + // System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); + // if ("Renamed".equals(response)) { + // counter++; + // } + // } + // System.out.println("Renamed count: " + counter); + + // if (counter == facet.length) { + // // Try to save - should fail with duplicate error + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Save response (expecting error): " + response); + + // // Parse JSON response to check for duplicate error + // ObjectMapper mapper = new ObjectMapper(); + // try { + // JsonNode root = mapper.readTree(response); + // String message = root.path("error").path("message").asText(); + + // if (message.contains("already exists")) { + // System.out.println("Duplicate error detected as expected: " + message); + // counter = 0; + // // Rename with valid different names + // for (int i = 0; i < facet.length; i++) { + // response = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); + // System.out.println("Rename " + facet[i] + " to valid name: " + response); + // if ("Renamed".equals(response)) { + // counter++; + // } + // } + + // if (counter == facet.length) { + // // Save should now succeed + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Final save response: " + response); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } else { + // System.out.println("Unexpected error message: " + message); + // } + // } catch (Exception e) { + // // Response might not be JSON if save succeeded (shouldn't happen with duplicates) + // System.out.println("Response was not JSON error: " + response); + // // If save succeeded unexpectedly, we still need to ensure book is saved + // if ("Saved".equals(response)) { + // System.out.println( + // "Save succeeded unexpectedly - duplicates might be in different facets"); + // } + // } + // } + // } else { + // System.out.println("Book was not put into draft mode: " + response); + // } + + // if (!testStatus) { + // fail("Duplicate rename test failed for chapter"); + // } + // } + + // @Test + // @Order(10) + // void testRenameToValidateNamesInChapter() throws IOException { + // System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); + // Boolean testStatus = false; + + // // Create a new book and chapter for this test + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // bookID3 = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); + // if (!"Could not create entity".equals(chapterResponse)) { + // chapterID3 = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID3); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); + // } + + // String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; + + // boolean allRenamed = true; + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); + // if (!"Renamed".equals(response1)) { + // allRenamed = false; + // System.out.println( + // "Failed to rename " + // + facet[i] + // + " to valid name " + // + validNames[i] + // + ": " + // + response1); + // } + // } + + // if (allRenamed) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } + // } + + // if (!testStatus) { + // fail("Could not rename chapter attachments to valid names"); + // } + // } + + // @Test + // @Order(11) + // void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { + // System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); + // boolean testStatus = true; + + // try { + // String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Edit entity response: " + response); + + // if (response.equals("Entity in draft mode")) { + // String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; + // for (int i = 0; i < facet.length; i++) { + // response = + // apiNoRoles.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); + // System.out.println("Rename response for " + facet[i] + ": " + response); + // if (!"Renamed".equals(response)) { + // testStatus = false; + // } + // } + + // if (testStatus) { + // // Save should fail with permission error + // response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // System.out.println("Save response (expecting permission error): " + response); + + // // The expected error should indicate no permissions to update + // String expected = + // "[{\"code\":\"\",\"message\":\"Could not update the following + // files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update + // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3}]"; + + // // Check if response contains permission error + // if (!response.equals(expected) + // && !response.contains("do not have the required permissions")) { + // System.out.println("Expected permission error but got: " + response); + // testStatus = false; + // } else { + // System.out.println("Got expected permission error"); + // } + // } else { + // // Some renames failed - save to release draft + // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // } + // } else { + // System.out.println("Could not edit entity: " + response); + // testStatus = false; + // } + // } catch (Exception e) { + // System.out.println("Exception: " + e.getMessage()); + // testStatus = false; + // } + + // if (!testStatus) { + // fail("Chapter attachment got renamed without SDM roles."); + // } + // } + + // @Test + // @Order(12) + // void testDeleteSingleChapterAttachment() throws IOException { + // System.out.println( + // "Test (12) : Delete single attachment, reference, and footnote from chapter"); + // Boolean testStatus = false; + // int deleteCounter = 0; + + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + // if (response.equals("Deleted")) deleteCounter++; + // } + // if (deleteCounter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + // if (response.equals("Saved")) { + // int verifyCounter = 0; + // for (int i = 0; i < facet.length; i++) { + // response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + // if (response.equals("Could not read Attachment")) verifyCounter++; + // } + // if (verifyCounter == facet.length) { + // testStatus = true; + // } else { + // fail( + // "Could not verify all deleted chapter facets. Verified: " + // + verifyCounter + // + "/" + // + facet.length); + // } + // } else { + // fail("Could not save book after deleting chapter attachments"); + // } + // } else { + // fail( + // "Could not delete all chapter attachments. Deleted: " + // + deleteCounter + // + "/" + // + facet.length); + // } + // } else { + // fail("Could not edit book to draft mode"); + // } + + // if (!testStatus) { + // fail("Test failed to delete chapter attachments"); + // } + // } + + // @Test + // @Order(13) + // void testUploadBlockedMimeTypeToChapter() throws IOException { + // System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); + // Boolean testStatus = false; + + // // Create new book and chapter + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // bookID4 = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); + // if (!"Could not create entity".equals(chapterResponse)) { + // chapterID4 = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = + // new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID4); + // postData.put("mimeType", "application/rtf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // boolean allBlocked = true; + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID4, 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)) { + // allBlocked = false; + // System.out.println( + // "Chapter facet " + // + facet[i] + // + " incorrectly accepted blocked mimeType: " + // + actualResponse); + // } + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); + // if ("Saved".equals(response) && allBlocked) { + // testStatus = true; + // } + // } + // } + + // if (!testStatus) { + // fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); + // } + // } + + // @Test + // @Order(14) + // void testDeleteBookAndChapter() { + // System.out.println("Test (14) : Delete book (and its chapters)"); + // Boolean testStatus = false; + // // Delete books (chapters are deleted automatically as they're composition) + // String response = api.deleteEntity(appUrl, bookEntityName, bookID); + // String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); + // String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); + // String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); + // if (response.equals("Entity Deleted") + // && response2.equals("Entity Deleted") + // && response3.equals("Entity Deleted") + // && response4.equals("Entity Deleted")) testStatus = true; + // if (!testStatus) fail("Could not delete books"); + // } + + // @Test + // @Order(15) + // void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { + // System.out.println( + // "Test (15) : Rename & Update secondary property in chapter before book is saved"); + // System.out.println("Creating book and chapter"); + + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (!response.equals("Could not create entity")) { + // bookID5 = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); + // if (!chapterResponse.equals("Could not create entity")) { + // chapterID5 = chapterResponse; + + // System.out.println("Creating attachment, reference, and footnote in chapter"); + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID5); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // boolean allCreated = true; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); + // if (tempID[i] == null || tempID[i].isEmpty()) { + // System.out.println("Failed to create attachment for facet: " + facet[i]); + // allCreated = false; + // } + // } + + // System.out.println("Attachments, References, and Footnotes created in chapter"); + // System.out.println( + // "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + + // tempID[2]); + + // if (!allCreated) { + // fail("Could not create all attachments for test 15"); + // } + + // // Reset counter for this test + // counter = 0; + + // // Use valid dropdown value for customProperty1 + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + // for (int i = 0; i < facet.length; i++) { + // System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + // System.out.println("Rename response for " + facet[i] + ": " + response1); + + // // Update customProperty1 (String - dropdown value) + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); + + // // Update customProperty2 (Integer) + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + + // // Update customProperty5 (DateTime) - using customProperty5 like Books test + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + + // // Update customProperty6 (Boolean) + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + // // Check all updates succeeded + // if ("Renamed".equals(response1) + // && "Updated".equals(updateSecondaryPropertyResponse1) + // && "Updated".equals(updateSecondaryPropertyResponse2) + // && "Updated".equals(updateSecondaryPropertyResponse3) + // && "Updated".equals(updateSecondaryPropertyResponse4)) { + // counter++; + // } else { + // System.out.println( + // "Update failed for " + // + facet[i] + // + ": rename=" + // + response1 + // + ", dropdown=" + // + updateSecondaryPropertyResponse1 + // + ", int=" + // + updateSecondaryPropertyResponse2 + // + ", datetime=" + // + updateSecondaryPropertyResponse3 + // + ", bool=" + // + updateSecondaryPropertyResponse4); + // } + // } + + // System.out.println("Counter after all facets: " + counter); + // if (counter == facet.length) { + // // Save the book (not the chapter) + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // System.out.println("Save response: " + response); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } else { + // System.out.println( + // "Counter is less than " + facet.length + ", not saving. Counter: " + counter); + // } + // } + // } + + // if (!testStatus) { + // fail( + // "Could not update secondary properties in chapter before book save. Counter: " + + // counter); + // } + // } + + // @Test + // @Order(16) + // void testUploadNAttachmentsToChapter() throws IOException { + // System.out.println("Test (16) : Upload N attachments to chapter"); + // Boolean testStatus = false; + // counter = 0; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); + + // for (int j = 0; j < 5; j++) { + // // Create temp file with unique name per iteration + // File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); + // Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID5); + // 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, bookEntityName, srvpath, bookID5); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + // String check = facetResponse.get(0); + // if (check.equals("Attachment created")) { + // counter++; + // } else { + // System.out.println( + // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + // } + // } + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if (!response.equals("Saved")) { + // System.out.println( + // "Failed to save book after creating attachments in chapter: " + response); + // } + // } else { + // System.out.println("Could not edit book draft: " + response); + // } + // tempFile.delete(); + // } + + // if (counter == 15) { // 5 iterations * 3 facets + // testStatus = true; + // } + + // if (!testStatus) { + // fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); + // } + // } + + // @Test + // @Order(17) + // void testDiscardDraftWithoutChapterAttachments() { + // System.out.println("Test (17) : Discard book draft without chapter attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // // Create chapter but don't add attachments + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft without chapter attachments was not discarded properly"); + // } + // } + + // @Test + // @Order(18) + // void testDiscardDraftWithChapterAttachments() throws IOException { + // System.out.println("Test (18) : Discard book draft with chapter attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // // Create chapter + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // 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 chapter + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + // String check = facetResponse.get(0); + // if (!check.equals("Attachment created")) { + // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + // } + // } + + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft with chapter attachments was not discarded properly"); + // } + // } + + // @Test + // @Order(19) + // void testUploadChapterAttachmentWithoutSDMRole() throws IOException { + // System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); + // Boolean testStatus = true; + + // String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, + // tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // try { + // List createResponse = + // apiNoRoles.createAttachment( + // appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); + // String check = createResponse.get(0); + + // if (check.equals("Attachment created")) { + // testStatus = false; + // } + // } catch (Exception e) { + // // Expected to fail + // testStatus = true; + // } + + // apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // } + // } + + // if (!testStatus) { + // fail("Chapter attachment was uploaded without SDM roles"); + // } + // } + + // @Test + // @Order(20) + // void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { + // System.out.println( + // "Test (20): Rename & Update secondary property in chapter after book is saved"); + // Boolean testStatus = false; + // counter = 0; // Reset counter for this test + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // System.out.println("Editing book, response: " + response); + + // if (response.equals("Entity in draft mode")) { + // // Use unique names that won't conflict with existing attachments + // String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; + // Integer secondaryPropertyInt = 42; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // System.out.println("Renaming and updating secondary properties for chapter attachment"); + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // // Get the first attachment ID from the chapter + // try { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); + // if (!metadata.isEmpty()) { + // tempID[i] = (String) metadata.get(0).get("ID"); + // } + // } catch (IOException e) { + // fail("Could not fetch metadata for chapter: " + e.getMessage()); + // } + + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if (response.equals("Saved")) { + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for chapter attachment"); + // } + // } + // } + // if (!testStatus) fail("Could not update secondary properties in chapter after book is + // saved"); + // } + + // @Test + // @Order(21) + // void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException + // { + // System.out.println( + // "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); + // System.out.println("Creating book and chapter"); + // Boolean testStatus = false; + // int localCounter = 0; + // int createCounter = 0; + + // // Create new book and chapter for this test + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // if (tempID[i] != null) { + // createCounter++; + // } + // } + + // // Only proceed if all facets were created successfully + // if (createCounter == facet.length) { + // // Prepare test data + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + + // "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], + // invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // localCounter++; + // } + // } + + // if (localCounter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + // // Fetch metadata and verify values weren't updated due to invalid property + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, + // tempID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + + // // Parse JSON response and check for expected error messages + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved with expected invalid property errors"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for chapter attachment is unsuccessful"); + // } + // } else { + // System.out.println( + // "Not all facets updated successfully. localCounter: " + localCounter); + // } + // } else { + // System.out.println( + // "Not all facets created successfully. createCounter: " + createCounter); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // } + // } + // if (!testStatus) + // fail("Could not update invalid secondary property in chapter before book is saved"); + // } + + // @Test + // @Order(22) + // void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { + // System.out.println( + // "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); + // System.out.println("Creating book and chapter"); + // Boolean testStatus = false; + // int localCounter = 0; + // int createCounter = 0; + + // // Create new book and chapter + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] tempID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // tempID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // if (tempID[i] != null) { + // createCounter++; + // } + // } + + // // Only proceed if all facets were created successfully + // if (createCounter != facet.length) { + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // fail("Not all facets created successfully. createCounter: " + createCounter); + // } + + // // Save the book first + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (!response.equals("Saved")) { + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // fail("Could not save book initially"); + // } + + // // Now edit to update with invalid property + // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (response.equals("Entity in draft mode")) { + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt = 12; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testidinvalid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + + // "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], + // invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // localCounter++; + // } + // } + + // if (localCounter == facet.length) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, + // tempID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + + // // Parse JSON response and check for expected error messages + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved with expected invalid property errors"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for chapter attachment is unsuccessful"); + // } + // } else { + // System.out.println( + // "Not all facets updated successfully. localCounter: " + localCounter); + // } + // } + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // } + // } + // if (!testStatus) + // fail("Could not update invalid secondary property in chapter after book is saved"); + // } + + // @Test + // @Order(23) + // void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { + // System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); + + // boolean testStatus = false; + + // // Reuse bookID5 and chapterID5 + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + + // if (response.equals("Entity in draft mode")) { + // ClassLoader classLoader = getClass().getClassLoader(); + + // // Use temp files with unique names to avoid duplicate name errors + // File originalPdf = + // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + // File originalTxt = + // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); + + // File file1 = File.createTempFile("test23_pdf_", ".pdf"); + // File file2 = File.createTempFile("test23_txt_", ".txt"); + // Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); + // Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", chapterID5); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // Map postData2 = new HashMap<>(postData1); + // postData2.put("up__ID", chapterID5); + // postData2.put("mimeType", "text/plain"); + + // boolean allCreated = true; + // String[] tempID1 = new String[facet.length]; + // String[] tempID2 = new String[facet.length]; + + // for (int i = 0; i < facet.length; i++) { + // List response1 = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); + // List response2 = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); + + // if (response1.get(0).equals("Attachment created") + // && response2.get(0).equals("Attachment created")) { + // tempID1[i] = response1.get(1); // to keep one + // tempID2[i] = response2.get(1); // will delete this one + // } else { + // System.out.println("Failed to create attachments for facet " + facet[i]); + // System.out.println("Response 1: " + response1.get(0)); + // System.out.println("Response 2: " + response2.get(0)); + // allCreated = false; + // break; + // } + + // String deleteResponse = + // api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); + // if (!"Deleted".equals(deleteResponse)) { + // allCreated = false; + // break; + // } + // } + + // file1.delete(); + // file2.delete(); + + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } else { + // System.out.println("Could not edit book: " + response); + // } + + // if (!testStatus) { + // fail("Failed to upload multiple chapter facet entries, delete one per facet and save + // book"); + // } + // } + + // @Test + // @Order(24) + // void testUpdateChapterEntityDraft() throws IOException { + // System.out.println("Test (24): Update chapter in book draft with new facet content"); + // boolean testStatus = false; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + // // Use unique temp file name to avoid duplicates + // File tempFile = File.createTempFile("test24_sample_", ".pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", chapterID5); + // 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, bookEntityName, srvpath, bookID5); + // if (response.equals("Entity in draft mode")) { + // boolean allCreated = true; + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + // String check = facetResponse.get(0); + // if (!check.equals("Attachment created")) { + // allCreated = false; + // System.out.println( + // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + // } + // } + + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } else { + // System.out.println("Could not edit book: " + response); + // } + + // tempFile.delete(); + + // if (!testStatus) { + // fail("Failed to update chapter entity draft with new attachments"); + // } + // } + + // @Test + // @Order(25) + // void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + // throws IOException { + // System.out.println( + // "Test (25): Rename & Update secondary properties for multiple chapter attachments after + // book is saved"); + // System.out.println("Creating book and chapter with multiple attachments"); + + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!"Could not create entity".equals(chapterResponse)) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create PDF attachments + // postData.put("mimeType", "application/pdf"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String[] pdfID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // pdfID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create TXT attachments + // postData.put("mimeType", "application/txt"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // String[] txtID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // txtID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create EXE attachments + // postData.put("mimeType", "application/exe"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // String[] exeID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // exeID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Save book first + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (!"Saved".equals(response)) { + // fail("Could not save book initially"); + // } + + // // Edit book to update chapter attachments + // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (response.equals("Entity in draft mode")) { + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // // Update PDF properties + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String renameResp = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + // String upd3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + // String upd4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + + // if ("Renamed".equals(renameResp) + // && "Updated".equals(upd1) + // && "Updated".equals(upd2) + // && "Updated".equals(upd3) + // && "Updated".equals(upd4)) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // PDF"); + // } + // } + + // // Update TXT properties (only boolean) + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + // String upd = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + // if ("Updated".equals(upd)) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // TXT"); + // } + // } + + // // Update EXE properties (dropdown and int) + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValueExe = integrationTestUtils.getDropDownValue(); + // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyDropdownExe = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + // RequestBody bodyIntExe = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], + // bodyDropdownExe); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // EXE"); + // } + // } + + // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (response.equals("Saved")) { + // System.out.println("Book saved"); + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for chapter + // attachments"); + // } + // } + // } + // api.deleteEntity(appUrl, bookEntityName, tempBookID); + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property in chapter after book is saved"); + // } + // } + + // @Test + // @Order(26) + // void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() + // throws IOException { + // System.out.println( + // "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter + // facets before book is saved"); + // System.out.println("Creating book and chapter"); + + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (!"Could not create entity".equals(response)) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!"Could not create entity".equals(chapterResponse)) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create PDF attachments + // postData.put("mimeType", "application/pdf"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String[] pdfID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // pdfID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create TXT attachments + // postData.put("mimeType", "application/txt"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // String[] txtID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // txtID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // // Create EXE attachments + // postData.put("mimeType", "application/exe"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // String[] exeID = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // exeID[i] = + // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, + // file); + // } + + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample1234.pdf"; + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + + // // Update PDF properties + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String renameResp = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + // String upd3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + // String upd4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + // String updInvalid = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], + // invalidPropertyPDF); + + // if ("Renamed".equals(renameResp) + // && "Updated".equals(upd1) + // && "Updated".equals(upd2) + // && "Updated".equals(upd3) + // && "Updated".equals(upd4) + // && "Updated".equals(updInvalid)) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // PDF"); + // } + // } + + // // Update TXT properties + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + // String upd = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + // if ("Updated".equals(upd)) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // TXT"); + // } + // } + + // // Update EXE properties + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValueExe = integrationTestUtils.getDropDownValue(); + // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyDropdownExe = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + // RequestBody bodyIntExe = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // EXE"); + // } + // } + + // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + + // // Verify PDF metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); + // assertEquals(expectedNames[0], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertNull(metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } + + // // Verify TXT metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); + // assertEquals(expectedNames[1], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertTrue((Boolean) metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } + + // // Verify EXE metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); + // assertEquals(expectedNames[2], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); + // assertEquals(1234, metadata.get("customProperty2")); + // } + + // // Parse JSON response and check for expected error messages + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved with expected invalid property errors"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessful for invalid properties and successful for valid + // attachments"); + // } + // } + // } + // } + + // if (!testStatus) { + // fail("Could not update secondary property before book is saved"); + // } + // } + + // @Test + // @Order(27) + // void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + // throws IOException { + // System.out.println( + // "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter + // attachments after book is saved"); + + // // Reuse bookID5 and chapterID5 + // System.out.println("Editing book with bookID5: " + bookID5); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // System.out.println("Edit entity response: " + response); + + // if (response.equals("Entity in draft mode")) { + // // Fetch existing attachments from the chapter + // List> attachmentsMeta = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); + // List> referencesMeta = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); + // List> footnotesMeta = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); + + // System.out.println("Attachments count: " + attachmentsMeta.size()); + // System.out.println("References count: " + referencesMeta.size()); + // System.out.println("Footnotes count: " + footnotesMeta.size()); + + // if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) + // { + // String[] pdfID = new String[facet.length]; + // String[] txtID = new String[facet.length]; + // String[] exeID = new String[facet.length]; + + // pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); + // pdfID[1] = (String) referencesMeta.get(0).get("ID"); + // pdfID[2] = (String) footnotesMeta.get(0).get("ID"); + + // txtID[0] = (String) attachmentsMeta.get(1).get("ID"); + // txtID[1] = (String) referencesMeta.get(1).get("ID"); + // txtID[2] = (String) footnotesMeta.get(1).get("ID"); + + // exeID[0] = (String) attachmentsMeta.get(2).get("ID"); + // exeID[1] = (String) referencesMeta.get(2).get("ID"); + // exeID[2] = (String) footnotesMeta.get(2).get("ID"); + + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // // PDF + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); + + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); + + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); + + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); + + // String updateSecondaryPropertyResponse5 = + // api.updateInvalidSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated") + // && updateSecondaryPropertyResponse5.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // PDF"); + // } + // } + + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); + // String updateSecondaryPropertyResponseTXT1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // TXT"); + // } + // } + + // Integer secondaryPropertyInt3 = 12; + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // for (int i = 0; i < facet.length; i++) { + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); + + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); + + // if (updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " + // EXE"); + // } + // } + + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // // Note: Don't verify specific filenames since previous tests may have changed them + // System.out.println("Save response: " + response); + + // // Parse JSON response to check for invalid secondary property errors in all three + // tables + // ObjectMapper mapper = new ObjectMapper(); + // JsonNode root = mapper.readTree(response); + // boolean hasAttachmentsError = false; + // boolean hasReferencesError = false; + // boolean hasFootnotesError = false; + // if (root.isArray()) { + // for (JsonNode node : root) { + // String message = node.path("message").asText(); + // if (message.contains("id1") && message.contains("Table: attachments")) { + // hasAttachmentsError = true; + // } + // if (message.contains("id1") && message.contains("Table: references")) { + // hasReferencesError = true; + // } + // if (message.contains("id1") && message.contains("Table: footnotes")) { + // hasFootnotesError = true; + // } + // } + // } + // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + // System.out.println("Book saved"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessful for invalid Secondary properties and successful for + // valid property attachments"); + // } else { + // System.out.println("Save response did not match expected: " + response); + // } + // } else { + // System.out.println("Not enough attachments in facets - need at least 3 per facet"); + // } + // } + // } else { + // System.out.println( + // "Could not edit book - it may be stuck in draft mode from a previous test"); + // } + // if (!testStatus) { + // fail("Could not update secondary property before book is saved"); + // } + // } + + // // Tests 28 and 29 removed - chapters have no attachment limit + + // // Tests 28-29 skipped - chapters have no attachment limit + + // @Test + // @Order(30) + // void testDiscardBookDraftWithoutChapterAttachments() { + // System.out.println("Test (30) : Discard book draft without adding chapter attachments"); + // Boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!chapterResponse.equals("Could not create entity")) { + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if (response.equals("Entity Draft Deleted")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft with chapter was not discarded properly"); + // } + // } + + // @Test + // @Order(31) + // void testDiscardBookDraftWithChapterAttachments() throws IOException { + // System.out.println("Test (31): Discard book draft with chapter attachments"); + // boolean testStatus = false; + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (!"Could not create entity".equals(chapterResponse)) { + // String tempChapterID = chapterResponse; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = + // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", tempChapterID); + // 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 < facet.length; i++) { + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + // if ("Attachment created".equals(createResponse.get(0))) { + // System.out.println("Attachment created in chapter facet: " + facet[i]); + // } else { + // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + // } + // } + + // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Book draft with chapter attachments was not discarded properly"); + // } + // } + + // // Tests 32-34 covered in tests 19, 23, 24 + // // Tests 37-41 skipped - copy with notes/secondary properties not applicable + + // @Test + // @Order(42) + // void testCreateLinkSuccessInChapter() throws IOException { + // System.out.println("Test (42): Create link in chapter"); + // List attachments = new ArrayList<>(); + + // // Create book and chapter for link testing + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (response.equals("Could not create entity")) { + // fail("Could not create book"); + // } + // String createLinkBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + // if (chapterResponse.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + // String createLinkChapterID = chapterResponse; + + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // for (String facetName : facet) { + // String createLinkResponse1 = + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + // String createLinkResponse2 = + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", + // linkUrl); + // if (!createLinkResponse1.equals("Link created successfully") + // || !createLinkResponse2.equals("Link created successfully")) { + // fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); + // } + // } + + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // for (String facetName : facet) { + // attachments = + // api + // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) + // .stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String openAttachmentResponse; + // for (String attachment : attachments) { + // openAttachmentResponse = + // api.openAttachment( + // appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open created link in chapter facet : " + facetName); + // } + // } + // } + // api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + // } + + // @Test + // @Order(43) + // void testCreateLinkDifferentChapter() throws IOException { + // System.out.println("Test (43): Create link with same name in different chapter"); + + // // Create new book and chapter + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (response.equals("Could not edit entity")) { + // fail("Could not create book"); + // } + // String tempBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + // if (chapterResponse.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + // String tempChapterID = chapterResponse; + + // String linkName = "sample"; + // String linkUrl = "https://example.com"; + // for (String facetName : facet) { + // String createResponse = + // api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); + // if (!createResponse.equals("Link created successfully")) { + // fail("Could not create link in different chapter with same name"); + // } + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + // if (!response.equals("Saved")) { + // fail("Could not save book"); + // } + + // response = api.deleteEntity(appUrl, bookEntityName, tempBookID); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(44) + // void testCreateLinkFailureInChapter() throws IOException { + // System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if ("Could not create entity".equals(response)) { + // fail("Could not create book"); + // } + // String createLinkBookID = response; + + // response = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + // if ("Could not create entity".equals(response)) { + // fail("Could not create chapter"); + // } + // String createLinkChapterID = response; + + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + + // ObjectMapper mapper = new ObjectMapper(); + + // for (String facetName : facet) { + + // // Create initial link for this facet first (so duplicate test works) + // response = + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + // if (!"Link created successfully".equals(response)) { + // fail("Could not create initial link for facet: " + facetName); + // } + + // /* ---------- INVALID URL ---------- */ + // try { + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); + // fail("Expected invalid URL error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // assertEquals("400018", error.path("code").asText()); + // assertTrue( + // error.path("message").asText().contains("expected pattern"), + // "Unexpected message: " + error.path("message").asText()); + // } + + // /* ---------- INVALID NAME ---------- */ + // try { + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // createLinkChapterID, + // "sample//", + // "https://example.com"); + // fail("Expected invalid name error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); + + // assertEquals("500", error.path("code").asText()); + // assertTrue( + // message.contains("contains unsupported characters") + // && message.contains("Rename and try again"), + // "Unexpected message: " + message); + // } + + // /* ---------- EMPTY NAME & URL ---------- */ + // try { + // api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); + // fail("Expected missing value error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // assertEquals("409008", error.path("code").asText()); + // assertEquals("Provide the missing value.", error.path("message").asText()); + // } + + // /* ---------- DUPLICATE NAME ---------- */ + // try { + // api.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + // fail("Expected duplicate name error"); + // } catch (IOException e) { + // JsonNode error = + // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + // assertEquals("500", error.path("code").asText()); + // assertEquals( + // "An object named \"sample\" already exists. Rename the object and try again.", + // error.path("message").asText()); + // } + // } + + // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + // if (!"Saved".equals(response)) { + // fail("Could not save book"); + // } + + // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + // if (!"Entity Deleted".equals(response)) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(45) + // void testCreateLinkNoSDMRolesInChapter() throws IOException { + // System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); + + // String createLinkBookNoRoles = + // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (createLinkBookNoRoles.equals("Could not edit entity")) { + // fail("Could not create book"); + // } + + // String createLinkChapterNoRoles = + // apiNoRoles.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); + // if (createLinkChapterNoRoles.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // for (String facetName : facet) { + // String linkName = "sample27"; + // String linkUrl = "https://example.com"; + // try { + // apiNoRoles.createLink( + // appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); + // if (!response.equals("Saved")) { + // fail("Could not save book"); + // } + + // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(46) + // void testDeleteLinkInChapter() throws IOException { + // System.out.println("Test (46): Delete link in chapter"); + // List> attachments = new ArrayList<>(); + + // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (response.equals("Could not create entity")) { + // fail("Could not create book"); + // } + // String deleteLinkBookID = response; + + // String chapterResponse = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); + // if (chapterResponse.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + // String deleteLinkChapterID = chapterResponse; + + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink( + // appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for chapter facet : " + facetName); + // } + // } + + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // for (String facetName : facet) { + // attachments.add( + // api + // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + // .stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } + + // String editEntityResponse = + // api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // int index = 0; + // for (String facetName : facet) { + // String deleteLinkResponse = + // api.deleteAttachment( + // appUrl, + // chapterEntityName, + // facetName, + // deleteLinkChapterID, + // attachments.get(index).get(0)); + // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + // if (!deleteLinkResponse.equals("Deleted")) { + // fail("Could not delete created link"); + // } + // index += 1; + // } + + // saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // index = 0; + // attachments.clear(); + // for (String facetName : facet) { + // attachments.add( + // api + // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + // .stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // System.out.println( + // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + // if (attachments.get(index).size() != 0) { + // fail("Link wasn't deleted"); + // } + // index += 1; + // } + + // response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete book"); + // } + // } + + // @Test + // @Order(35) + // void testCopyAttachmentsToNewChapterInSameBook() throws IOException { + // System.out.println( + // "Test (35): Copy attachments from one chapter to another new chapter in the same book"); + + // // Create source book and chapter with attachments + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Load original files for copying content + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(new ArrayList<>()); + // } + + // // Create attachments in all facets - each upload needs a unique filename + // int fileCounter = 0; + // for (int i = 0; i < facet.length; i++) { + // boolean useTxt = (i == 1); // Use txt for references facet + // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + // File originalFile = useTxt ? originalTxt : originalPdf; + // String extension = useTxt ? ".txt" : ".pdf"; + + // for (int j = 0; j < 2; j++) { // Create 2 attachments per facet + // // Create unique temp file for EACH upload to avoid duplicate filename errors + // fileCounter++; + // File tempFile = + // File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, + // tempFile); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // System.out.println("Created attachment ID: " + createResponse.get(1)); + // } else { + // System.out.println("Failed to create attachment: " + createResponse.get(0)); + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + // } + + // // Fetch object IDs from source attachments + // List objectIds = new ArrayList<>(); + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + // if (metadata.containsKey("objectId")) { + // objectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // } + + // // Save the source book + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Create target chapter in the SAME book + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source book for adding target chapter"); + // } + + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (targetChapterID.equals("Could not create entity")) { + // fail("Could not create target chapter in same book"); + // } + + // // Copy attachments to target chapter + // int objectIdIndex = 0; + // for (String facetName : facet) { + // List facetObjectIds = + // objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // facetObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + // } + + // // Fetch and wait for copied attachments + // List> copiedMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // for (Map meta : copiedMetadata) { + // String copiedId = (String) meta.get("ID"); + // } + // objectIdIndex += 2; + // } + + // // Save the book with new chapter + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book after copying attachments"); + // } + + // // Verify attachments were copied - read them + // for (String facetName : facet) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.size() != 2) { + // fail("Expected 2 attachments in facet " + facetName + ", found " + + // targetMetadata.size()); + // } + // for (Map meta : targetMetadata) { + // String attachmentId = (String) meta.get("ID"); + // String readResponse = + // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // attachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment in facet: " + facetName); + // } + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // } + + // @Test + // @Order(36) + // void testCopyAttachmentsToChapterInDifferentBook() throws IOException { + // System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); + + // // Create Book1 with source chapter and attachments + // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create Book2 with target chapter + // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Load original files for copying content + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(new ArrayList<>()); + // } + + // // Create attachments in all facets of source chapter - each upload needs unique filename + // int fileCounter = 0; + // for (int i = 0; i < facet.length; i++) { + // boolean useTxt = (i == 1); // Use txt for references facet + // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + // File originalFile = useTxt ? originalTxt : originalPdf; + // String extension = useTxt ? ".txt" : ".pdf"; + + // for (int j = 0; j < 2; j++) { + // // Create unique temp file for EACH upload to avoid duplicate filename errors + // fileCounter++; + // File tempFile = + // File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // System.out.println("Created attachment ID: " + createResponse.get(1)); + // } else { + // System.out.println("Failed to create attachment: " + createResponse.get(0)); + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + // } + + // // Fetch object IDs + // sourceObjectIds.clear(); + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // } + + // // Save Book1 + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Copy attachments from Book1's chapter to Book2's chapter + // if (sourceObjectIds.size() == 6) { + // int objectIdIndex = 0; + // for (String facetName : facet) { + // List facetObjectIds = + // sourceObjectIds.subList( + // objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, + // facetObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + // } + + // objectIdIndex += 2; + // } + + // // Save Book2 + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book after copying attachments"); + // } + + // // Verify attachments were copied + // for (String facetName : facet) { + // List> targetMetadata = + // api.fetchEntityMetadata( + // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); + // if (targetMetadata.size() != 2) { + // fail("Expected 2 attachments in facet " + facetName + ", found " + + // targetMetadata.size()); + // } + // for (Map meta : targetMetadata) { + // String attachmentId = (String) meta.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, + // attachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment in facet: " + facetName); + // } + // } + // } + + // // Cleanup - delete both books after verification + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // } else { + // fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); + // } + // } + + // @Test + // @Order(37) + // void testCopyAttachmentsWithNotePreserved() throws IOException { + // System.out.println("Test (37): Copy attachments with note field preserved"); + + // // Create source book and chapter + // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create attachments with notes in source chapter + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] sourceAttachmentIds = new String[facet.length]; + // String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); + + // for (int i = 0; i < facet.length; i++) { + // // Create unique temp file for each facet + // File tempFile = + // File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), + // ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // sourceAttachmentIds[i] = createResponse.get(1); + + // // Update note field using RequestBody + // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + // String noteResponse = + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // noteBody); + // if (!noteResponse.equals("Updated")) { + // fail("Could not update note for attachment in facet: " + facet[i]); + // } + // System.out.println("Note updated for facet: " + facet[i]); + // } + + // // Save source book + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Verify notes were saved in source + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // if (!testNote.equals(metadata.get("note"))) { + // fail("Note not saved correctly in source for facet: " + facet[i]); + // } + // } + + // // Create target book and chapter + // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, + // srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Get object IDs and copy attachments + // for (int i = 0; i < facet.length; i++) { + // Map sourceMetadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // String objectId = sourceMetadata.get("objectId").toString(); + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // System.out.println("Attachment copied to facet: " + facet[i]); + // } + + // // Save target book + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify notes were preserved in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], + // copyAttachmentTargetChapter); + // if (targetMetadata.isEmpty()) { + // fail("No attachments found in target facet: " + facet[i]); + // } + // Map copiedAttachment = targetMetadata.get(0); + // String copiedNote = (String) copiedAttachment.get("note"); + // if (!testNote.equals(copiedNote)) { + // fail( + // "Note not preserved after copy in facet: " + // + facet[i] + // + ". Expected: " + // + testNote + // + ", Got: " + // + copiedNote); + // } + // System.out.println("Note preserved in target facet: " + facet[i]); + // } + + // System.out.println("Test 37 passed - notes preserved during copy"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // copyAttachmentSourceBook = null; + // copyAttachmentTargetBook = null; + // } + + // @Test + // @Order(38) + // void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { + // System.out.println("Test (38): Copy attachments with secondary properties preserved"); + + // // Use entities from test 37 or create new ones if needed + // boolean sourceBookJustCreated = false; + // boolean targetBookJustCreated = false; + + // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + // copyAttachmentSourceBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + // sourceBookJustCreated = true; + // } + + // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + // copyAttachmentTargetBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + // targetBookJustCreated = true; + // } + + // // If source book was just created, save it first before we can edit it + // if (sourceBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created source book"); + // } + // } + + // // If target book was just created, save it first + // if (targetBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created target book"); + // } + // } + + // // Edit source book + // String editResponse = + // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source book"); + // } + + // // Create new attachments with secondary properties + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] sourceAttachmentIds = new String[facet.length]; + // Boolean testBooleanProp = true; + // Integer testIntegerProp = 12345; + + // for (int i = 0; i < facet.length; i++) { + // // Create unique temp file + // File tempFile = + // File.createTempFile( + // "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // sourceAttachmentIds[i] = createResponse.get(1); + + // // Update secondary properties using RequestBody (customProperty6 - Boolean, + // customProperty2 - + // // Integer) + // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + // String boolResponse = + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // boolBody); + // if (!boolResponse.equals("Updated")) { + // System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); + // } + + // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + // String intResponse = + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // intBody); + // if (!intResponse.equals("Updated")) { + // System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); + // } + + // System.out.println("Secondary properties updated for facet: " + facet[i]); + // } + + // // Save source book + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Edit target book + // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // // Copy attachments to target + // for (int i = 0; i < facet.length; i++) { + // Map sourceMetadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // String objectId = sourceMetadata.get("objectId").toString(); + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); + // } + + // // Save target book + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify secondary properties were preserved in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], + // copyAttachmentTargetChapter); + + // // Find the attachment we just copied (most recent one) + // boolean found = false; + // for (Map attachment : targetMetadata) { + // Object boolProp = attachment.get("customProperty6"); + // Object intProp = attachment.get("customProperty2"); + + // if (boolProp != null && intProp != null) { + // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { + // found = true; + // System.out.println("Secondary properties preserved in target facet: " + facet[i]); + // break; + // } + // } + // } + // if (!found) { + // System.out.println( + // "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); + // } + // } + + // System.out.println("Test 38 passed - secondary properties checked during copy"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // copyAttachmentSourceBook = null; + // copyAttachmentTargetBook = null; + // } + + // @Test + // @Order(39) + // void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { + // System.out.println( + // "Test (39): Copy attachments with both note and secondary properties preserved"); + + // // Use entities from previous tests or create new ones + // boolean sourceBookJustCreated = false; + // boolean targetBookJustCreated = false; + + // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + // copyAttachmentSourceBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentSourceBook.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + // copyAttachmentSourceChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + // if (copyAttachmentSourceChapter.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + // sourceBookJustCreated = true; + // } + + // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + // copyAttachmentTargetBook = + // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (copyAttachmentTargetBook.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + // copyAttachmentTargetChapter = + // api.createEntityDraft( + // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + // if (copyAttachmentTargetChapter.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + // targetBookJustCreated = true; + // } + + // // If source book was just created, save it first before we can edit it + // if (sourceBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created source book"); + // } + // } + + // // If target book was just created, save it first + // if (targetBookJustCreated) { + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save newly created target book"); + // } + // } + + // // Edit source book + // String editResponse = + // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source book"); + // } + + // // Create new attachments with both note and secondary properties + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyAttachmentSourceChapter); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String[] sourceAttachmentIds = new String[facet.length]; + // String testNote = "Combined test note - " + System.currentTimeMillis(); + // Boolean testBooleanProp = true; + // Integer testIntegerProp = 99999; + + // for (int i = 0; i < facet.length; i++) { + // // Create unique temp file + // File tempFile = + // File.createTempFile( + // "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // List createResponse = + // api.createAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // srvpath, + // postData, + // tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // sourceAttachmentIds[i] = createResponse.get(1); + + // // Update note using RequestBody + // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // noteBody); + + // // Update secondary properties using RequestBody + // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // boolBody); + + // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + // api.updateSecondaryProperty( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i], + // intBody); + + // System.out.println("Note and secondary properties updated for facet: " + facet[i]); + // } + + // // Save source book + // String saveResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Edit target book + // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // // Copy attachments to target + // for (int i = 0; i < facet.length; i++) { + // Map sourceMetadata = + // api.fetchMetadata( + // appUrl, + // chapterEntityName, + // facet[i], + // copyAttachmentSourceChapter, + // sourceAttachmentIds[i]); + // String objectId = sourceMetadata.get("objectId").toString(); + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + + // String copyResponse = + // api.copyAttachment( + // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // System.out.println("Attachment with note and properties copied to facet: " + facet[i]); + // } + + // // Save target book + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, + // copyAttachmentTargetBook); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify note and secondary properties were preserved in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], + // copyAttachmentTargetChapter); + + // boolean noteFound = false; + // boolean propsFound = false; + + // for (Map attachment : targetMetadata) { + // String copiedNote = (String) attachment.get("note"); + // Object boolProp = attachment.get("customProperty6"); + // Object intProp = attachment.get("customProperty2"); + + // if (testNote.equals(copiedNote)) { + // noteFound = true; + // System.out.println("Note preserved in target facet: " + facet[i]); + // } + + // if (boolProp != null && intProp != null) { + // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { + // propsFound = true; + // System.out.println("Secondary properties preserved in target facet: " + facet[i]); + // } + // } + // } + + // if (!noteFound) { + // System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); + // } + // if (!propsFound) { + // System.out.println( + // "Warning: Secondary properties may not be preserved in facet: " + facet[i]); + // } + // } + + // // Cleanup - delete both books + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + + // // Reset static variables + // copyAttachmentSourceBook = null; + // copyAttachmentTargetBook = null; + // copyAttachmentSourceChapter = null; + // copyAttachmentTargetChapter = null; + + // System.out.println("Test 39 passed - both note and secondary properties checked during + // copy"); + // } + + // @Test + // @Order(40) + // void testCopyAttachmentsWithInvalidObjectId() throws IOException { + // System.out.println("Test (40): Copy attachments with invalid object ID should fail"); + + // // Create independent test entities (don't rely on previous tests) + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create test book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create test chapter"); + // } + + // // Save the book first so it's not in draft mode + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save test book"); + // } + + // // Now edit it to test copy with invalid object IDs + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit test book"); + // } + + // // Try to copy with invalid object ID + // for (String facetName : facet) { + // try { + // List invalidObjectIds = new ArrayList<>(); + // invalidObjectIds.add("invalidObjectId123"); + // invalidObjectIds.add("anotherInvalidId456"); + // api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, + // invalidObjectIds); + // fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); + // } catch (IOException e) { + // // Expected - copy should fail with invalid object ID + // System.out.println( + // "Expected error received for invalid object ID in facet " + // + facetName + // + ": " + // + e.getMessage()); + // } + // } + + // // Save and cleanup + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + + // // Also cleanup test 36 entities if they exist + // if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { + // try { + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + // } catch (Exception e) { + // // Ignore - may already be deleted + // } + // } + // if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { + // try { + // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + // } catch (Exception e) { + // // Ignore - may already be deleted + // } + // } + // } + + // @Test + // @Order(41) + // void testCopyAttachmentsToExistingChapter() throws IOException { + // System.out.println( + // "Test (41): Copy attachments to an existing chapter that already has attachments"); + + // // Create Book1 with source chapter + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create Book2 with target chapter that has existing attachments + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (targetBookID.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + // if (targetChapterID.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Create temp files with unique names to avoid duplicate filename errors + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test41_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create attachment in source chapter + // List sourceObjIds = new ArrayList<>(); + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create source attachment"); + // } + // String attachmentId = createResponse.get(1); + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + // sourceObjIds.add(metadata.get("objectId").toString()); + // } + + // // Create existing attachment in target chapter + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", targetChapterID); + // postData.put("mimeType", "text/plain"); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create existing target attachment"); + // } + // String attachmentId = createResponse.get(1); + // } + + // // Save both books + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Edit target book and copy attachments + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // // Copy from source to target (target already has 1 attachment per facet) + // for (int i = 0; i < facet.length; i++) { + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjIds.get(i)); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, + // objectIdsToCopy); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to facet: " + facet[i]); + // } + // } + + // // Save target book + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) + // for (String facetName : facet) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.size() != 2) { + // fail( + // "Expected 2 attachments in facet " + // + facetName + // + " (1 existing + 1 copied), found " + // + targetMetadata.size()); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // // ============= LINK RENAME TESTS (47-49) ============= + + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (47): Rename link in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links in all facets + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit and rename links + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // String renameResponse = + // api.renameAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); + // if (!renameResponse.equals("Renamed")) { + // fail("Could not rename link in facet: " + facetName); + // } + // } + + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book after renaming links"); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(48) + // void testRenameLinkDuplicate() throws IOException { + // System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create two links in all facets + // for (String facetName : facet) { + // String createLinkResponse1 = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "link1", + // "https://www.example1.com"); + // String createLinkResponse2 = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "link2", + // "https://www.example2.com"); + // if (!createLinkResponse1.equals("Link created successfully") + // || !createLinkResponse2.equals("Link created successfully")) { + // fail("Could not create links in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit and try to rename link2 to link1 (duplicate) + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.size() < 2) { + // fail("Expected 2 links in facet: " + facetName); + // } + + // // Find link2 and rename to link1 + // for (Map attachment : attachments) { + // if ("link2".equals(attachment.get("fileName"))) { + // String linkId = (String) attachment.get("ID"); + // api.renameAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); + // break; + // } + // } + // } + + // // Save should fail with duplicate error + // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // ObjectMapper mapper = new ObjectMapper(); + // try { + // JsonNode errorJson = mapper.readTree(saveError); + // String errorMessage = errorJson.path("error").path("message").asText(); + // if (!errorMessage.contains("already exists")) { + // fail("Expected duplicate error but got: " + saveError); + // } + // } catch (Exception e) { + // if (!saveError.contains("already exists")) { + // fail("Expected duplicate error but got: " + saveError); + // } + // } + + // // Cleanup + // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(49) + // void testRenameLinkUnsupportedCharacters() throws IOException { + // System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links in all facets + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit and rename with unsupported characters + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // api.renameAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); + // } + + // // Save should fail with unsupported characters error + // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveError.contains("unsupported characters")) { + // fail("Expected unsupported characters error but got: " + saveError); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // // ============= LINK EDIT TESTS (50-53) ============= + + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (50): Edit existing link URL in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links in all facets + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit links + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // String editLinkResponse = + // api.editLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // linkId, + // "https://www.editedexample.com"); + // if (!editLinkResponse.equals("Link edited successfully")) { + // fail("Could not edit link in facet: " + facetName); + // } + // } + + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book after editing links"); + // } + + // // Verify links open successfully + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // for (Map attachment : attachments) { + // String linkId = (String) attachment.get("ID"); + // String openResponse = + // api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); + // if (!openResponse.equals("Attachment opened successfully")) { + // fail("Could not open edited link in facet: " + facetName); + // } + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(51) + // void testEditLinkFailureInvalidURL() throws IOException { + // System.out.println("Test (51): Edit link with invalid URL fails in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create links + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // try { + // api.editLink( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, + // "https://editedexample"); + // fail("Edit link should have failed with invalid URL in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected error received for invalid URL in facet " + facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(52) + // void testEditLinkFailureEmptyURL() throws IOException { + // System.out.println("Test (52): Edit link with empty URL fails in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // try { + // api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); + // fail("Edit link should have failed with empty URL in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected error received for empty URL in facet " + facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // "sample", + // "https://www.example.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, + // testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // for (String facetName : facet) { + // List> attachments = + // apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + // if (attachments.isEmpty()) { + // fail("No links found in facet: " + facetName); + // } + + // String linkId = (String) attachments.get(0).get("ID"); + // try { + // apiNoRoles.editLink( + // appUrl, chapterEntityName, facetName, testChapterID, linkId, + // "https://www.edited.com"); + // fail("Edit link should have failed without SDM roles in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected permission error received in facet " + facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // // ============= COPY LINK TESTS (54-58) ============= + + // @Test + // @Order(54) + // void testCopyLinkSuccessNewChapter() throws IOException { + // System.out.println("Test (54): Copy link from one chapter to another new chapter"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // if (sourceChapterID.equals("Could not create entity") + // || targetChapterID.equals("Could not create entity")) { + // fail("Could not create chapters"); + // } + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // // Create links in source chapter + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch object IDs + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // // Copy links to target chapter + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link for facet " + facetName + ": " + copyResponse); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify link type and URL + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.isEmpty()) { + // fail("No links found in target chapter for facet: " + facetName); + // } + + // Map copiedLink = targetMetadata.get(0); + // String receivedUrl = (String) copiedLink.get("linkUrl"); + // assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); + + // objectIdIndex++; + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(55) + // void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { + // System.out.println("Test (55): Copy invalid link object ID to chapter fails"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // for (String facetName : facet) { + // try { + // List invalidObjectIds = new ArrayList<>(); + // invalidObjectIds.add("incorrectObjectId"); + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // invalidObjectIds); + // fail("Copy should have thrown error for invalid object ID in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected error received for invalid object ID in facet " + + // facetName); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(56) + // void testCopyLinkToExistingChapter() throws IOException { + // System.out.println("Test (56): Copy link to existing chapter that has attachments"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // // Create links in source chapter + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sourceLink" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in source chapter for facet: " + facet[i]); + // } + // } + + // // Create existing links in target chapter + // for (int i = 0; i < facet.length; i++) { + // String linkName = "existingLink" + i; + // String createLinkResponse = + // api.createLink( + // appUrl, + // chapterEntityName, + // facet[i], + // targetChapterID, + // linkName, + // "https://www.existing.com"); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create existing link in target chapter for facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch source object IDs + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // // Copy links to target chapter + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link for facet " + facetName); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify target has 2 links (existing + copied) + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.size() != 2) { + // fail( + // "Expected 2 links in target chapter facet " + // + facetName + // + ", found " + // + targetMetadata.size()); + // } + + // objectIdIndex++; + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(57) + // void testCopyLinkNoSDMRoles() throws IOException { + // System.out.println("Test (57): Copy link fails due to no SDM roles"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch object IDs + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // // Try to copy with no SDM roles + // int objectIdIndex = 0; + // for (String facetName : facet) { + // try { + // // Use normal api to put book in draft mode + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // // Use apiNoRoles to attempt copy (should fail) + // apiNoRoles.copyAttachment( + // appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + // fail("Copy should have failed without SDM roles in facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Expected permission error in facet " + facetName); + // // Discard draft to clean up for next iteration + // api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); + // } + // objectIdIndex++; + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(58) + // void testCopyLinkFromDraftChapter() throws IOException { + // System.out.println("Test (58): Copy link from draft chapter to another chapter"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // String linkUrl = "https://www.example.com"; + // List linkObjectIds = new ArrayList<>(); + + // // Create links in source chapter (NOT saved yet - draft mode) + // for (int i = 0; i < facet.length; i++) { + // String linkName = "draftLink" + i; + // String createLinkResponse = + // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, + // linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + + // // Save target book only + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch object IDs from draft + // for (int i = 0; i < facet.length; i++) { + // List> metadata = + // api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); + // for (Map meta : metadata) { + // if (meta.containsKey("objectId")) { + // linkObjectIds.add(meta.get("objectId").toString()); + // } + // } + // } + + // if (linkObjectIds.size() != facet.length) { + // fail("Could not fetch all object IDs from draft"); + // } + + // // Copy links from draft to target + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link from draft for facet " + facetName); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify link was copied + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.isEmpty()) { + // fail("No links found in target chapter for facet: " + facetName); + // } + + // objectIdIndex++; + // } + + // // Cleanup + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= + + // @Test + // @Order(59) + // void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { + // System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // if (sourceChapterID.equals("Could not create entity") + // || targetChapterID.equals("Could not create entity")) { + // fail("Could not create chapters"); + // } + + // // Create temp files with unique names + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test59_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List sourceObjectIds = new ArrayList<>(); + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(new ArrayList<>()); + // } + + // // Create attachments in source chapter (still in draft) + // for (int i = 0; i < facet.length; i++) { + // postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); + // File file = i == 1 ? tempTxt : tempPdf; + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + + // // Fetch object IDs from draft + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // Map metadata = + // api.fetchMetadataDraft( + // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } + // } + + // // Save target book only + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Copy attachments from draft to target + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // subListToCopy); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment from draft for facet " + facetName); + // } + + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book"); + // } + + // // Verify attachment was copied + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + // if (targetMetadata.isEmpty()) { + // fail("No attachments found in target chapter for facet: " + facetName); + // } + + // // Read attachment to verify + // String attachmentId = (String) targetMetadata.get(0).get("ID"); + // String readResponse = + // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, + // attachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment in facet: " + facetName); + // } + + // objectIdIndex++; + // } + + // // Cleanup + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // // ============= CHANGELOG TESTS (60-64) ============= + + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println("Test (60): View changelog for newly created attachment in chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String facetName = facet[i]; + + // // Create book and chapter + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = + // File.createTempFile( + // "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } + + // String attachmentId = createResponse.get(1); + + // // Fetch changelog + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); + + // // Cleanup + // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + // } + // } + + // @Test + // @Order(61) + // void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { + // System.out.println("Test (61): Changelog after modifying note and custom property in + // chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String facetName = facet[i]; + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = + // File.createTempFile( + // "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Update note + // String notesValue = "Test note for changelog verification"; + // RequestBody updateNotesBody = + // RequestBody.create( + // MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); + + // // Update custom property + // RequestBody bodyInt = + // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": + // 12345}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); + + // // Save + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // // Fetch changelog + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // int numItems = (int) changelogResponse.get("numItems"); + // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + // } + + // @Test + // @Order(62) + // void testChangelogAfterRenamingAttachment() throws IOException { + // System.out.println("Test (62): Changelog after renaming attachment in chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String facetName = facet[i]; + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = + // File.createTempFile( + // "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Rename attachment + // String renameResponse = + // api.renameAttachment( + // appUrl, + // chapterEntityName, + // facetName, + // testChapterID, + // attachmentId, + // "renamed_file.txt"); + // if (!renameResponse.equals("Renamed")) { + // fail("Could not rename attachment"); + // } + + // // Save + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book"); + // } + + // // Edit to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit book"); + // } + + // // Fetch changelog + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // int numItems = (int) changelogResponse.get("numItems"); + // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + // } + + // @Test + // @Order(63) + // void testChangelogForCopiedAttachment() throws IOException { + // System.out.println("Test (63): Changelog for copied attachment in chapter"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), + // ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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 in source + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Save both books + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Get object ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + + // // Copy to target + // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target book"); + // } + + // List objectIds = new ArrayList<>(); + // objectIds.add(objectId); + // String copyResponse = + // api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); + + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment"); + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Fetch changelog for copied attachment + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + // String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); + + // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // Map changelogResponse = + // api.fetchChangelog( + // appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // int numItems = (int) changelogResponse.get("numItems"); + // assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(64) + // void testChangelogForNewChapter() throws IOException { + // System.out.println("Test (64): Changelog for attachment in newly created chapter"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // if (testChapterID.equals("Could not create entity")) { + // fail("Could not create chapter"); + // } + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), + // ".txt"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalFile.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", testChapterID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String attachmentId = createResponse.get(1); + + // // Fetch changelog before saving + // Map changelogResponse = + // api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + // assertEquals( + // 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); + + // @SuppressWarnings("unchecked") + // List> changeLogs = + // (List>) changelogResponse.get("changeLogs"); + // assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); + + // // Cleanup + // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + // } + + // // ============= MOVE ATTACHMENT TESTS (65-75) ============= + + // @Test + // @Order(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println("Test (65): Move attachments from source chapter to target chapter"); + + // for (int i = 0; i < facet.length; i++) { + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create temp files + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), + // tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List sourceAttachmentIds = new ArrayList<>(); + // File[] files = {tempPdf, tempTxt}; + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source chapter"); + // } + // } + + // // Save source book + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save source book"); + // } + + // // Get object IDs and folder ID + // List moveObjectIds = new ArrayList<>(); + // String sourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, + // attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (sourceFolderId == null && metadata.containsKey("folderId")) { + // sourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } + + // // Create target book and chapter + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (targetBookID.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + // if (targetChapterID.equals("Could not create entity")) { + // fail("Could not create target chapter"); + // } + + // // Save target book before moving attachments (moveAttachments requires Active entity) + // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target book before move"); + // } + + // // Move attachments to Active entity + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } + + // // Verify + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadata.size(), + // "Target should have all attachments after move"); + + // List> sourceMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // } + // } + + // @Test + // @Order(66) + // void testMoveAttachmentsToChapterWithDuplicate() throws IOException { + // System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Create attachment in source with specific name + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, + // originalPdf); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create source attachment"); + // } + // String sourceAttachmentId = createResponse.get(1); + + // // Create attachment in target with SAME name (duplicate) + // postData.put("up__ID", targetChapterID); + // createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, + // originalPdf); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create target attachment"); + // } + + // // Save both + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Get source object ID and folder ID + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, + // sourceAttachmentId); + // String objectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move to saved target + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Move should handle duplicate - attachment stays in source + + // // Verify source still has attachment (duplicate not moved) + // List> sourceMetadataAfter = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertTrue( + // sourceMetadataAfter.size() >= 1, + // "Source should still have attachment when duplicate exists in target"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(67) + // void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { + // System.out.println("Test (67): Move attachments with notes and secondary properties"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // if (sourceChapterID.equals("Could not create entity")) { + // fail("Could not create source chapter"); + // } + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // // Add note and secondary property + // String testNote = "Test note for move"; + // RequestBody noteBody = + // RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + + // "\"}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); + + // RequestBody propBody = + // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); + // api.updateSecondaryProperty( + // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); + + // // Save source + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get object ID and folder ID + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // // Create target + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Save target before move + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null"); + // } + + // // Verify note was preserved + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + // if (!targetMetadata.isEmpty()) { + // String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); + // Map movedMetadata = + // api.fetchMetadata( + // appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); + + // // Note should be preserved + // if (movedMetadata.containsKey("note")) { + // assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); + // } + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(68) + // void testMoveAttachmentsPartialFailure() throws IOException { + // System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get real object ID and folder ID + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String realObjectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // // Create target + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Save target before move + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Try to move with mix of valid and invalid object IDs + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(realObjectId); + // moveObjectIds.add("invalidObjectId123"); + + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Should handle partial failure + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(69) + // void testMoveAttachmentsEmptyList() throws IOException { + // System.out.println("Test (69): Move attachments with empty object ID list"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Try to move with empty list + // List emptyObjectIds = new ArrayList<>(); + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + + // try { + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // "someFolderId", + // emptyObjectIds, + // targetFacet, + // sourceFacet); + // // Should either fail or do nothing + // } catch (Exception e) { + // System.out.println("Expected: Move with empty list handled: " + e.getMessage()); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(70) + // void testMoveAttachmentsToSameChapter() throws IOException { + // System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String testChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // 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"); + + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Get object ID and folder ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String folderId = metadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move to same chapter + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // testChapterID, + // folderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Should handle gracefully - attachment stays in place + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Verify attachment still exists + // List> metadataAfter = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); + // assertEquals(1, metadataAfter.size(), "Attachment should still exist"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(71) + // void testMoveAttachmentsBetweenFacets() throws IOException { + // System.out.println("Test (71): Move attachments between different facets in chapters"); + + // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (testBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // // Create in attachments facet + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // // Get object ID and folder ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String sourceFolderId = metadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move from attachments to references facet + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[1], // references facet + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify moved to different facet + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); + // assertTrue( + // targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, testBookID); + // } + + // @Test + // @Order(72) + // void testMoveMultipleAttachments() throws IOException { + // System.out.println("Test (72): Move multiple attachments at once between chapters"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // // Create multiple temp files + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + // String uniqueSuffix = "_test72_" + System.currentTimeMillis(); + // File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); + // File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); + // tempPdf.deleteOnExit(); + // tempTxt.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempPdf.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + // java.nio.file.Files.copy( + // originalTxt.toPath(), tempTxt.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List sourceAttachmentIds = new ArrayList<>(); + // File[] files = {tempPdf, tempTxt}; + // String[] mimeTypes = {"application/pdf", "text/plain"}; + + // for (int i = 0; i < files.length; i++) { + // postData.put("mimeType", mimeTypes[i]); + // List createResponse = + // api.createAttachment( + // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get object IDs + // List moveObjectIds = new ArrayList<>(); + // String sourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (sourceFolderId == null) { + // sourceFolderId = metadata.get("folderId").toString(); + // } + // } + + // // Create target + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // // Save target before move + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Move all at once + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify all moved + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadata.size(), + // "All attachments should be moved to target"); + + // List> sourceMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(73) + // void testMoveAttachmentsAllFacets() throws IOException { + // System.out.println("Test (73): Move attachments from all facets between chapters"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || targetBookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String targetChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // // Create attachment in each facet + // for (int i = 0; i < facet.length; i++) { + // String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); + // File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), + // tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facet[i]); + // } + // } + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // // Move from each facet + // for (int i = 0; i < facet.length; i++) { + // List> sourceMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + // if (sourceMetadata.isEmpty()) { + // continue; + // } + + // String attachmentId = (String) sourceMetadata.get(0).get("ID"); + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String sourceFolderId = metadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + // } + + // // Verify all facets have attachments in target + // for (int i = 0; i < facet.length; i++) { + // List> targetMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + // assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + + // facet[i]); + // } + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(74) + // void testChainMoveAttachments() throws IOException { + // System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + // if (sourceBookID.equals("Could not create entity") + // || target1BookID.equals("Could not create entity") + // || target2BookID.equals("Could not create entity")) { + // fail("Could not create books"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + // String target1ChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); + // String target2ChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), + // ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); + + // // First move: Source -> Target1 + // Map sourceMetadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = sourceMetadata.get("objectId").toString(); + // String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // // Move to target1 + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // target1ChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify in target1 + // List> target1Metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + // assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); + + // // Second move: Target1 -> Target2 + // String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); + // Map target1AttMetadata = + // api.fetchMetadata( + // appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); + // String target1ObjectId = target1AttMetadata.get("objectId").toString(); + // String target1FolderId = target1AttMetadata.get("folderId").toString(); + + // moveObjectIds.clear(); + // moveObjectIds.add(target1ObjectId); + + // // Move to target2 + // api.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // target2ChapterID, + // target1FolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify final state + // List> target2Metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); + // assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); + + // target1Metadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + // assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); + + // List> sourceFinalMetadata = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, target1BookID); + // api.deleteEntity(appUrl, bookEntityName, target2BookID); + // } + + // @Test + // @Order(75) + // void testMoveAttachmentsWithoutSDMRole() throws IOException { + // System.out.println("Test (75): Move attachments fails without SDM role"); + + // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (sourceBookID.equals("Could not create entity")) { + // fail("Could not create source book"); + // } + + // String sourceChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // // Create temp file + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + // File tempFile = + // File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); + // tempFile.deleteOnExit(); + // java.nio.file.Files.copy( + // originalPdf.toPath(), tempFile.toPath(), + // java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", sourceChapterID); + // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + // String attachmentId = createResponse.get(1); + + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // // Get object ID and folder ID + // Map metadata = + // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // String sourceFolderId = metadata.get("folderId").toString(); + + // // Create target with no role user + // String targetBookID = + // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (targetBookID.equals("Could not create entity")) { + // fail("Could not create target book"); + // } + + // String targetChapterID = + // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, + // targetBookID); + + // // Save target before move + // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // List moveObjectIds = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + // boolean moveFailed = false; + // String errorMessage = null; + + // try { + // Map moveResult = + // apiNoRoles.moveAttachment( + // appUrl, + // chapterEntityName, + // facet[0], + // targetChapterID, + // sourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null || moveResult.containsKey("error")) { + // moveFailed = true; + // errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; + // } + // } catch (Exception e) { + // moveFailed = true; + // errorMessage = e.getMessage(); + // } + + // assertTrue(moveFailed, "Move should fail without SDM role"); + // System.out.println("Move correctly failed without SDM role: " + errorMessage); + + // // Verify source still has attachment + // List> sourceMetadataAfter = + // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + // assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); + + // // Cleanup + // api.deleteEntity(appUrl, bookEntityName, sourceBookID); + // api.deleteEntity(appUrl, bookEntityName, targetBookID); + // } + + // @Test + // @Order(76) + // void testRenameChapterAttachmentWithExtensionChange() throws IOException { + // System.out.println( + // "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all + // facets - should return extension change warning"); + + // // Step 1: Create a new book and chapter + // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (newBookID.equals("Could not create entity")) { + // fail("Could not create book"); + // } + // String newChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + // if (newChapterID.equals("Could not create entity")) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not create chapter"); + // } + // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save book: " + saveResponse); + // } + + // // Step 2: Upload a PDF attachment to each facet in the chapter + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", newChapterID); + // 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, bookEntityName, srvpath, newBookID); + // if (!"Entity in draft mode".equals(editResponse)) { + // fail("Could not put book in draft mode for PDF upload"); + // } + + // String[] facetAttachmentIDs = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // facetAttachmentIDs[i] = + // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + // if (facetAttachmentIDs[i] == null) { + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + // } + // } + + // // Step 3: Save the book + // String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // if (!savedAfterUpload.equals("Saved")) { + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not save book after PDF upload: " + savedAfterUpload); + // } + + // // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt + // for (int i = 0; i < facet.length; i++) { + // String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // if (!"Entity in draft mode".equals(editDraftResponse)) { + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not put book in draft mode for rename on facet: " + facet[i]); + // } + + // String renameResponse = + // api.renameAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // newChapterID, + // facetAttachmentIDs[i], + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + // } + + // // Step 6: Save and validate the extension change warning message + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } + + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + // } + + // // Clean up + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // } + + // @Test + // @Order(77) + // void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { + // System.out.println( + // "Test (77) : Upload chapter attachment in draft, rename changing extension before save + // across all facets - should return extension change warning"); + + // for (int i = 0; i < facet.length; i++) { + // // Step 1: Create a new book and chapter draft (do NOT save) + // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + // if (newBookID.equals("Could not create entity")) { + // fail("Could not create book for facet: " + facet[i]); + // } + // String newChapterID = + // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + // if (newChapterID.equals("Could not create entity")) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not create chapter for facet: " + facet[i]); + // } + + // // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft + // (unsaved) + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", newChapterID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // String facetAttachmentID = + // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + // if (facetAttachmentID == null) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + // } + + // // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not + // saved + // String renameResponse = + // api.renameAttachment( + // appUrl, + // chapterEntityName, + // facet[i], + // newChapterID, + // facetAttachmentID, + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + // } + + // // Step 4: Save the book — should receive extension change warning, not "Saved" + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } + + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + + // // Clean up + // api.deleteEntity(appUrl, bookEntityName, newBookID); + // } + // } @Test @Order(78) 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 b231aa985..0b98fc280 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 @@ -383,6678 +383,6891 @@ void testUploadSingleTXT() throws IOException { } } - @Test - @Order(5) - void testUploadSingleEXE() throws IOException { - System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); - Boolean testStatus = false; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.exe").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID); - postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables - 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, entityID); - if (response.equals("Entity in draft mode")) { - // Creation of attachment, reference and footnote - for (int i = 0; i < facet.length; i++) { - ID3[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID, postData, file); - } - testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); - } - if (!testStatus) { - fail("Could not upload sample.exe " + response); - } - } - - @Test - @Order(6) - void testUploadPDFDuplicate() throws IOException { - System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID); - 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, entityID); - if ("Entity in draft mode".equals(response)) { - Boolean allFacetsFailedCorrectly = true; - for (int i = 0; i < facet.length; i++) { - List facetResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, file); - allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); - } - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (!allFacetsFailedCorrectly) { - fail("One or more facets were incorrectly accepted as new."); - } - } else { - fail("Entity could not be edited to draft mode."); - } - } - - @Test - @Order(7) - void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { - System.out.println( - "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and footnote"); - Boolean testStatus = false; - // Create a new entity draft - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID2 = response; - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - - if ("Saved".equals(response)) { - response = api.checkEntity(appUrl, entityName, entityID2); - if ("Entity exists".equals(response)) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Could not create entity"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID2); - 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 draft mode - response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - if ("Entity in draft mode".equals(response)) { - // Create attachment, reference, and footnote - for (int i = 0; i < facet.length; i++) { - ID4[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID2, postData, file); - } - // Verify and save - testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); - } - if (!testStatus) { - fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); - } - } - - @Test - @Order(8) - void testRenameEntities() { - System.out.println("Test (8) : Rename single attachment, reference, and footnote"); - Boolean testStatus = true; - - try { - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - - if ("Entity in draft mode".equals(response)) { - String[] name = {"sample123", "reference123", "footnote123"}; - for (int i = 0; i < facet.length; i++) { - // Read the facet to ensure it exists - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); - if (!"Renamed".equals(response)) { - testStatus = false; - System.out.println(facet[i] + " was not renamed: " + response); - } - } - // Save entity draft if everything is renamed - if (testStatus) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (!"Saved".equals(response)) { - testStatus = false; - System.out.println("Entity draft was not saved: " + response); - } - } else { - // Attempt save despite potential rename failures - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } else { - testStatus = false; - System.out.println("Entity was not put into draft mode: " + response); - } - } catch (Exception e) { - testStatus = false; - System.out.println("Exception during renaming entities: " + e.getMessage()); - } - - if (!testStatus) { - fail("There was an error during the rename test process."); - } - } - - @Test - @Order(9) - void testCreateEntitiesWithUnsupportedCharacter() throws IOException { - System.out.println("Test (9): Create attachments with unsupported characters"); - 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("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, entityID); - if (!"Entity in draft mode".equals(response)) { - fail("Entity not in draft mode: " + response); - return; - } - - for (int i = 0; i < facet.length; i++) { - postData.put("up__ID", entityID); - List createResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, tempFile); - - String check = createResponse.get(0); - if (!"Attachment created".equals(check)) { - System.out.println("Failed to create attachment for facet: " + facet[i]); - continue; - } - - String restrictedName = "a/\\bc.pdf"; - response = - api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); - } - - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - if (response.equals(expected)) { - api.deleteEntityDraft(appUrl, entityName, entityID); - testStatus = true; - } - - if (!testStatus) { - fail("Facets renamed with restricted characters were not correctly rejected."); - } - } - - @Test - @Order(10) - void testRenameEntitiesWithUnsupportedCharacter() { - System.out.println("Test (10) : Rename attachments with unsupported characters"); - Boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - if (response.equals("Renamed")) counter++; - } - if (counter >= 2) { - counter = -1; // Reset counter for the next check - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - if (response.equals(expected)) { - for (int i = 0; i < facet.length; i++) { - response = - api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], "sample.pdf"); - } - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment was renamed with unsupported characters"); - } - } - - @Test - @Order(11) - void testRenameMultipleEntityComponents() { - System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); - boolean testStatus = true; - - String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (!"Entity in draft mode".equals(draftResponse)) { - fail("Entity is not in draft mode."); - return; - } - String[] name = {"sample1234", "reference1234", "footnote1234"}; - String[] name2 = {"sample12345", "reference12345", "footnote12345"}; - for (int i = 0; i < facet.length; i++) { - // Read the facet to ensure it exists - testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); - testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); - } - // Save the draft if all renames succeeded - if (testStatus) { - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (!"Saved".equals(saveResponse)) { - fail("Entity draft was not saved after renaming."); - } - } else { - // Save draft even if renaming failed to preserve state - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - fail("One or more components were not renamed."); - } - } - - @Test - @Order(12) - void testRenameSingleDuplicate() { - System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); - Boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String[] name = {"sample1234", "reference1234", "footnote1234"}; - String[] name2 = {"sample123456", "reference123456", "footnote123456"}; - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - if (response.equals("Renamed")) counter++; - } - if (counter >= 2) { - counter = -1; // Reset counter for the next check - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - String.format( - "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", - name[1], name[0], name[2]); - if (response.equals(expected)) { - for (int i = 0; i < facet.length; i++) { - // Attempt to rename again with a different name - response = - api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); - if (response.equals("Renamed")) counter++; - } - } - if (counter >= 2) { - // If all renames were successful, save the draft - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - testStatus = true; - } - } else { - testStatus = false; - fail("Attachment was renamed"); - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - } - - @Test - @Order(13) - void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { - System.out.println( - "Test (13) : Rename multiple files out of which one file name contains unsupported characters"); - boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String[] names = {"summary_1234", "reference_4567", "note/invalid"}; - - if (response.equals("Entity in draft mode")) { - int successCount = 0; - for (int i = 0; i < facet.length; i++) { - response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], names[i]); - if (response.equals("Renamed")) successCount++; - } - - if (successCount >= 2) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - response = - api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); - 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 with unsupported characters"); - } - } - - @Test - @Order(14) - void testRenameToValidateNames() throws IOException { - System.out.println("Test (14) : Rename attachments to validate names"); - String[] generatedIDs = new String[3]; - String[] duplicateIDs = new String[1]; - boolean testStatus = false, allRenamedSuccessfully = true; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID3 = response; - - String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; - String duplicateName = "duplicateName.pdf"; + // @Test + // @Order(5) + // void testUploadSingleEXE() throws IOException { + // System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); + // Boolean testStatus = false; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.exe").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables + // 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, entityID); + // if (response.equals("Entity in draft mode")) { + // // Creation of attachment, reference and footnote + // for (int i = 0; i < facet.length; i++) { + // ID3[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID, postData, file); + // } + // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); + // } + // if (!testStatus) { + // fail("Could not upload sample.exe " + response); + // } + // } - 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(6) + // void testUploadPDFDuplicate() throws IOException { + // System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // 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, entityID); + // if ("Entity in draft mode".equals(response)) { + // Boolean allFacetsFailedCorrectly = true; + // for (int i = 0; i < facet.length; i++) { + // List facetResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, + // file); + // allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); + // } + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!allFacetsFailedCorrectly) { + // fail("One or more facets were incorrectly accepted as new."); + // } + // } else { + // fail("Entity could not be edited to draft mode."); + // } + // } - // Creation of attachment, reference and footnote - for (int i = 0; i < facet.length; i++) { - File file = new File(classLoader.getResource("sample2.pdf").getFile()); - generatedIDs[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - response = - api.renameAttachment( - appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); - allRenamedSuccessfully &= "Renamed".equals(response); - } - File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Creating duplicate name for last facet - duplicateIDs[0] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[2], entityID3, postData, file); - String response2 = - api.renameAttachment( - appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); - - if (allRenamedSuccessfully && "Renamed".equals(response2)) { - 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: references\\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: footnotes\\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(7) + // void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { + // System.out.println( + // "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and + // footnote"); + // Boolean testStatus = false; + // // Create a new entity draft + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID2 = response; + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + + // if ("Saved".equals(response)) { + // response = api.checkEntity(appUrl, entityName, entityID2); + // if ("Entity exists".equals(response)) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Could not create entity"); + // } - @Test - @Order(15) - void testRenameEntitiesWithoutSDMRole() throws IOException { - System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); - boolean testStatus = true; - try { - String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); - if ("Entity in draft mode".equals(apiResponse)) { - String[] name = {"sample456", "reference456", "footnote456"}; - for (int i = 0; i < facet.length; i++) { - apiResponse = - apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); - if (!"Renamed".equals(apiResponse)) { - testStatus = false; - } - } - if (testStatus) { - apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (!apiResponse.equals(expected)) { - testStatus = false; - } - } else { - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - } catch (Exception e) { - testStatus = false; - } - if (!testStatus) { - fail("Attachment got renamed without SDM roles."); - } - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID2); + // 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 draft mode + // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + // if ("Entity in draft mode".equals(response)) { + // // Create attachment, reference, and footnote + // for (int i = 0; i < facet.length; i++) { + // ID4[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID2, postData, file); + // } + // // Verify and save + // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); + // } + // if (!testStatus) { + // fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); + // } + // } - @Test - @Order(16) - void testDeleteSingleAttachment() throws IOException { - System.out.println("Test (16) : Delete single attachment, reference, and footnote"); - Boolean testStatus = false; - counter = -1; + // @Test + // @Order(8) + // void testRenameEntities() { + // System.out.println("Test (8) : Rename single attachment, reference, and footnote"); + // Boolean testStatus = true; + + // try { + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + + // if ("Entity in draft mode".equals(response)) { + // String[] name = {"sample123", "reference123", "footnote123"}; + // for (int i = 0; i < facet.length; i++) { + // // Read the facet to ensure it exists + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], + // name[i]); + // if (!"Renamed".equals(response)) { + // testStatus = false; + // System.out.println(facet[i] + " was not renamed: " + response); + // } + // } + // // Save entity draft if everything is renamed + // if (testStatus) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!"Saved".equals(response)) { + // testStatus = false; + // System.out.println("Entity draft was not saved: " + response); + // } + // } else { + // // Attempt save despite potential rename failures + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } else { + // testStatus = false; + // System.out.println("Entity was not put into draft mode: " + response); + // } + // } catch (Exception e) { + // testStatus = false; + // System.out.println("Exception during renaming entities: " + e.getMessage()); + // } - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - if (response.equals("Deleted")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - counter = -1; // Reset counter for the next check - if (response.equals("Saved")) { - for (int i = 0; i < facet.length; i++) { - response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - if (response.equals("Could not read Attachment")) counter++; - } - if (counter >= 2) testStatus = true; - else fail("Could not read deleted facets"); - } else { - fail("Could not save entity after deletion"); - } - } - } + // if (!testStatus) { + // fail("There was an error during the rename test process."); + // } + // } - @Test - @Order(17) - void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { - System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); - Boolean testStatus = false; + // @Test + // @Order(9) + // void testCreateEntitiesWithUnsupportedCharacter() throws IOException { + // System.out.println("Test (9): Create attachments with unsupported characters"); + // boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Entity in draft mode")) { - for (int i = 0; i < facet.length; i++) { - String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; - } - } - if (counter >= 2) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - if (response.equals("Saved")) { - for (int i = 0; i < facet.length; i++) { - String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - if (response1.equals("Could not read " + facet[i]) - && response2.equals("Could not read " + facet[i])) { - counter++; - } - } - if (counter >= 2) testStatus = true; - else fail("Could not read deleted facets"); - } else fail("Could not save entity after deletion"); - } + // 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("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, entityID); + // if (!"Entity in draft mode".equals(response)) { + // fail("Entity not in draft mode: " + response); + // return; + // } - @Test - @Order(18) - void testUploadBlockedMimeType() throws IOException { - System.out.println("Test (18) : Upload blocked mimeType .rtf"); - Boolean testStatus = false; + // for (int i = 0; i < facet.length; i++) { + // postData.put("up__ID", entityID); + // List createResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, + // tempFile); - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID2 = response; + // String check = createResponse.get(0); + // if (!"Attachment created".equals(check)) { + // System.out.println("Failed to create attachment for facet: " + facet[i]); + // continue; + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + // String restrictedName = "a/\\bc.pdf"; + // response = + // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); + // } - 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"); + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" + // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // if (response.equals(expected)) { + // api.deleteEntityDraft(appUrl, entityName, entityID); + // testStatus = true; + // } - boolean allBlocked = true; - for (int i = 0; i < facet.length; i++) { - List createResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, file); + // if (!testStatus) { + // fail("Facets renamed with restricted characters were not correctly rejected."); + // } + // } - 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.\"}}"; + // @Test + // @Order(10) + // void testRenameEntitiesWithUnsupportedCharacter() { + // System.out.println("Test (10) : Rename attachments with unsupported characters"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + // if (response.equals("Renamed")) counter++; + // } + // if (counter >= 2) { + // counter = -1; // Reset counter for the next check + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains + // unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" + // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // if (response.equals(expected)) { + // for (int i = 0; i < facet.length; i++) { + // response = + // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], + // "sample.pdf"); + // } + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment was renamed with unsupported characters"); + // } + // } - if (!expectedJson.equals(actualResponse)) { - allBlocked = false; - System.out.println( - "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); - } - } + // @Test + // @Order(11) + // void testRenameMultipleEntityComponents() { + // System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); + // boolean testStatus = true; + + // String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!"Entity in draft mode".equals(draftResponse)) { + // fail("Entity is not in draft mode."); + // return; + // } + // String[] name = {"sample1234", "reference1234", "footnote1234"}; + // String[] name2 = {"sample12345", "reference12345", "footnote12345"}; + // for (int i = 0; i < facet.length; i++) { + // // Read the facet to ensure it exists + // testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); + // testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); + // } + // // Save the draft if all renames succeeded + // if (testStatus) { + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (!"Saved".equals(saveResponse)) { + // fail("Entity draft was not saved after renaming."); + // } + // } else { + // // Save draft even if renaming failed to preserve state + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // fail("One or more components were not renamed."); + // } + // } - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if ("Saved".equals(response) && allBlocked) { - testStatus = true; - } - } + // @Test + // @Order(12) + // void testRenameSingleDuplicate() { + // System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String[] name = {"sample1234", "reference1234", "footnote1234"}; + // String[] name2 = {"sample123456", "reference123456", "footnote123456"}; + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + // if (response.equals("Renamed")) counter++; + // } + // if (counter >= 2) { + // counter = -1; // Reset counter for the next check + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // String.format( + // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already + // exists. Rename the object and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named + // \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An + // object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: + // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", + // name[1], name[0], name[2]); + // if (response.equals(expected)) { + // for (int i = 0; i < facet.length; i++) { + // // Attempt to rename again with a different name + // response = + // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); + // if (response.equals("Renamed")) counter++; + // } + // } + // if (counter >= 2) { + // // If all renames were successful, save the draft + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } else { + // testStatus = false; + // fail("Attachment was renamed"); + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // } - if (!testStatus) { - fail("Attachment got uploaded with blocked .rtf MIME type"); - } - } + // @Test + // @Order(13) + // void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { + // System.out.println( + // "Test (13) : Rename multiple files out of which one file name contains unsupported + // characters"); + // boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String[] names = {"summary_1234", "reference_4567", "note/invalid"}; + + // if (response.equals("Entity in draft mode")) { + // int successCount = 0; + // for (int i = 0; i < facet.length; i++) { + // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], + // names[i]); + // if (response.equals("Renamed")) successCount++; + // } - @Test - @Order(19) - void testDeleteEntity() { - System.out.println("Test (19) : Delete entity"); - Boolean testStatus = false; - String response = api.deleteEntity(appUrl, entityName, entityID); - String response2 = api.deleteEntity(appUrl, entityName, entityID2); - if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = true; - if (!testStatus) fail("Could not delete entity"); - } + // if (successCount >= 2) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // response = + // api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) testStatus = true; + // } + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } - @Test - @Order(20) - void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - System.out.println("Test (20) : Rename & Update secondary property before entity is saved"); - System.out.println("Creating entity"); + // if (!testStatus) { + // fail("Attachment was renamed with unsupported characters"); + // } + // } - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // @Test + // @Order(14) + // void testRenameToValidateNames() throws IOException { + // System.out.println("Test (14) : Rename attachments to validate names"); + // String[] generatedIDs = new String[3]; + // String[] duplicateIDs = new String[1]; + // boolean testStatus = false, allRenamedSuccessfully = true; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID3 = response; - if (!response.equals("Could not create entity")) { - entityID3 = response; + // String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; + // String duplicateName = "duplicateName.pdf"; - System.out.println("Creating attachment, reference, and footnote"); + // 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"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // // Creation of attachment, reference and footnote + // for (int i = 0; i < facet.length; i++) { + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // generatedIDs[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // response = + // api.renameAttachment( + // appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); + // allRenamedSuccessfully &= "Renamed".equals(response); + // } + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // // Creating duplicate name for last facet + // duplicateIDs[0] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[2], entityID3, postData, file); + // String response2 = + // api.renameAttachment( + // appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); + + // if (allRenamedSuccessfully && "Renamed".equals(response2)) { + // 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: references\\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: footnotes\\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; + // } + // } - 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(15) + // void testRenameEntitiesWithoutSDMRole() throws IOException { + // System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); + // boolean testStatus = true; + // try { + // String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if ("Entity in draft mode".equals(apiResponse)) { + // String[] name = {"sample456", "reference456", "footnote456"}; + // for (int i = 0; i < facet.length; i++) { + // apiResponse = + // apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], + // name[i]); + // if (!"Renamed".equals(apiResponse)) { + // testStatus = false; + // } + // } + // if (testStatus) { + // apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "[{\"code\":\"\",\"message\":\"Could not update the following files. + // \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update + // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not + // update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required + // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (!apiResponse.equals(expected)) { + // testStatus = false; + // } + // } else { + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // } catch (Exception e) { + // testStatus = false; + // } + // if (!testStatus) { + // fail("Attachment got renamed without SDM roles."); + // } + // } - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // @Test + // @Order(16) + // void testDeleteSingleAttachment() throws IOException { + // System.out.println("Test (16) : Delete single attachment, reference, and footnote"); + // Boolean testStatus = false; + // counter = -1; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + // if (response.equals("Deleted")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // counter = -1; // Reset counter for the next check + // if (response.equals("Saved")) { + // for (int i = 0; i < facet.length; i++) { + // response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + // if (response.equals("Could not read Attachment")) counter++; + // } + // if (counter >= 2) testStatus = true; + // else fail("Could not read deleted facets"); + // } else { + // fail("Could not save entity after deletion"); + // } + // } + // } - System.out.println("Attachments, References, and Footnotes created"); + // @Test + // @Order(17) + // void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { + // System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Entity in draft mode")) { + // for (int i = 0; i < facet.length; i++) { + // String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + // String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + // if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; + // } + // } + // if (counter >= 2) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // if (response.equals("Saved")) { + // for (int i = 0; i < facet.length; i++) { + // String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + // String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + // if (response1.equals("Could not read " + facet[i]) + // && response2.equals("Could not read " + facet[i])) { + // counter++; + // } + // } + // if (counter >= 2) testStatus = true; + // else fail("Could not read deleted facets"); + // } else fail("Could not save entity after deletion"); + // } - // Use valid dropdown value for customProperty1 - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // @Test + // @Order(18) + // void testUploadBlockedMimeType() throws IOException { + // System.out.println("Test (18) : Upload blocked mimeType .rtf"); + // Boolean testStatus = false; - String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID2 = response; - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - - // Update customProperty1 (String - dropdown value) - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - - // Update customProperty2 (Integer) - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - - // Update customProperty5 (DateTime) - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - - // Update customProperty6 (Boolean) - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - counter++; - } - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - if (counter >= 2) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - } - if (response.equals("Saved")) { - testStatus = 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"); - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } + // boolean allBlocked = true; + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, + // file); - @Test - @Order(21) - void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { - System.out.println("Test (21): Rename & Update secondary property after entity is saved"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - System.out.println("Editing entity"); + // 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 (response.equals("Entity in draft mode")) { - // Sample secondary properties - String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; - Integer secondaryPropertyInt = 42; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // if (!expectedJson.equals(actualResponse)) { + // allBlocked = false; + // System.out.println( + // "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); + // } + // } - System.out.println("Renaming and updating secondary properties for attachment"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Saved")) { - testStatus = true; - System.out.println("Renamed & updated Secondary properties for attachment"); - } - // Clean up - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); - } - if (!testStatus) fail("Could not update secondary properties after entity is saved"); - } + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if ("Saved".equals(response) && allBlocked) { + // testStatus = true; + // } + // } - @Test - @Order(22) - void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - System.out.println( - "Test (22): 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 (response != "Could not create entity") { - entityID3 = response; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // if (!testStatus) { + // fail("Attachment got uploaded with blocked .rtf MIME type"); + // } + // } - 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(19) + // void testDeleteEntity() { + // System.out.println("Test (19) : Delete entity"); + // Boolean testStatus = false; + // String response = api.deleteEntity(appUrl, entityName, entityID); + // String response2 = api.deleteEntity(appUrl, entityName, entityID2); + // if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = + // true; + // if (!testStatus) fail("Could not delete entity"); + // } - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } - // Prepare test data - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testid"; + // @Test + // @Order(20) + // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + // System.out.println("Test (20) : Rename & Update secondary property before entity is saved"); + // System.out.println("Creating entity"); - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, or footnote"); - } + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - @Test - @Order(23) - void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { - System.out.println( - "Test (23): Rename & Update invalid secondary property after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; + // if (!response.equals("Could not create entity")) { + // entityID3 = response; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Entity in draft mode")) { - String name1 = "sample.pdf"; - Integer secondaryPropertyInt = 12; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testidinvalid"; + // System.out.println("Creating attachment, reference, and footnote"); - for (int i = 0; i < facet.length; i++) { - // Rename and update secondary properties - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for invalid ID - String updateSecondaryPropertyResponse4 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - } - if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals("sample.pdf", FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for attachment, reference, footnote is unsuccessfull"); - } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (!deleteEntityResponse.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } - if (!testStatus) - fail( - "Could not update secondary property after entity is saved for attachment, reference, or footnote"); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - @Test - @Order(24) - void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (24): Rename & Update valid secondary properties for multiple facets 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; + // 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"); - System.out.println("Entity created"); - ClassLoader classLoader = getClass().getClassLoader(); + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - 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"); + // System.out.println("Attachments, References, and Footnotes created"); + + // // Use valid dropdown value for customProperty1 + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + + // // Update customProperty1 (String - dropdown value) + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + + // // Update customProperty2 (Integer) + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + + // // Update customProperty5 (DateTime) + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + + // // Update customProperty6 (Boolean) + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // counter++; + // } + // } - System.out.println("Creating attachment, reference, and footnote PDF"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // if (counter >= 2) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // } + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } - System.out.println("Creating attachment, reference, and footnote TXT"); - file = new File(classLoader.getResource("sample.txt").getFile()); - postData.put("mimeType", "application/txt"); - for (int i = 0; i < facet.length; i++) { - ID2[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - System.out.println("Creating attachment, reference, and footnote EXE"); - file = new File(classLoader.getResource("sample.exe").getFile()); - postData.put("mimeType", "application/exe"); - for (int i = 0; i < facet.length; i++) { - ID3[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } - Boolean Updated1[] = new Boolean[3]; - Boolean Updated2[] = new Boolean[3]; - Boolean Updated3[] = new Boolean[3]; - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // PDF - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } + // @Test + // @Order(21) + // void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { + // System.out.println("Test (21): Rename & Update secondary property after entity is saved"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // System.out.println("Editing entity"); + + // if (response.equals("Entity in draft mode")) { + // // Sample secondary properties + // String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; + // Integer secondaryPropertyInt = 42; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + // System.out.println("Renaming and updating secondary properties for attachment"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Saved")) { + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for attachment"); + // } + // // Clean up + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); + // } + // if (!testStatus) fail("Could not update secondary properties after entity is saved"); + // } - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - // 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, facet[i], entityID3, ID2[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // @Test + // @Order(22) + // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + // System.out.println( + // "Test (22): 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 (response != "Could not create entity") { + // entityID3 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - for (int i = 0; i < facet.length; i++) { - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponseEXE3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); - - if (updateSecondaryPropertyResponseEXE1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated") - && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - 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"); - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } + // 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(25) - void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { - System.out.println( - "Test (25): Rename & Update valid secondary properties for multiple facets after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Entity in draft mode")) { - Boolean Updated1[] = new Boolean[3]; - Boolean Updated2[] = new Boolean[3]; - Boolean Updated3[] = new Boolean[3]; - - String name1 = "sample1.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } + // // Prepare test data + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, + // or footnote"); + // } - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - // 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, facet[i], entityID3, ID2[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // @Test + // @Order(23) + // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { + // System.out.println( + // "Test (23): 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.equals("Entity in draft mode")) { + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt = 12; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testidinvalid"; + + // for (int i = 0; i < facet.length; i++) { + // // Rename and update secondary properties + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for invalid ID + // String updateSecondaryPropertyResponse4 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + // } + // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals("sample.pdf", FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for attachment, reference, footnote is + // unsuccessfull"); + // } + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (!deleteEntityResponse.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } + // if (!testStatus) + // fail( + // "Could not update secondary property after entity is saved for attachment, reference, + // or footnote"); + // } - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - for (int i = 0; i < facet.length; i++) { - // Update secondary properties for Drop down - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponseEXE3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); - - if (updateSecondaryPropertyResponseEXE1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated") - && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } + // @Test + // @Order(24) + // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (24): Rename & Update valid secondary properties for multiple facets 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; - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - 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"); - } - } + // System.out.println("Entity created"); + // ClassLoader classLoader = getClass().getClassLoader(); - @Test - @Order(26) - void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (26): Rename & Update invalid and valid secondary properties for multiple facets before entity is saved"); - System.out.println("Creating entity"); + // 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"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // System.out.println("Creating attachment, reference, and footnote PDF"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - if (!"Could not create entity".equals(response)) { - entityID3 = response; - System.out.println("Entity created"); + // System.out.println("Creating attachment, reference, and footnote TXT"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // postData.put("mimeType", "application/txt"); + // for (int i = 0; i < facet.length; i++) { + // ID2[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID3); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // System.out.println("Creating attachment, reference, and footnote EXE"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // postData.put("mimeType", "application/exe"); + // for (int i = 0; i < facet.length; i++) { + // ID3[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } + // Boolean Updated1[] = new Boolean[3]; + // Boolean Updated2[] = new Boolean[3]; + // Boolean Updated3[] = new Boolean[3]; + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // // PDF + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } - // Create PDF attachments - postData.put("mimeType", "application/pdf"); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - for (int i = 0; i < facet.length; i++) { - ID[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // // 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, facet[i], entityID3, ID2[i], + // bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - // Create TXT attachments - postData.put("mimeType", "application/txt"); - file = new File(classLoader.getResource("sample.txt").getFile()); - for (int i = 0; i < facet.length; i++) { - ID2[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // for (int i = 0; i < facet.length; i++) { + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponseEXE3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyDate); + + // if (updateSecondaryPropertyResponseEXE1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated") + // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // 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"); + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - // Create EXE attachments - postData.put("mimeType", "application/exe"); - file = new File(classLoader.getResource("sample.exe").getFile()); - for (int i = 0; i < facet.length; i++) { - ID3[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - } + // @Test + // @Order(25) + // void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { + // System.out.println( + // "Test (25): Rename & Update valid secondary properties for multiple facets after entity + // is saved"); + // System.out.println("Editing entity"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Entity in draft mode")) { + // Boolean Updated1[] = new Boolean[3]; + // Boolean Updated2[] = new Boolean[3]; + // Boolean Updated3[] = new Boolean[3]; + + // String name1 = "sample1.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } - Boolean[] Updated1 = new Boolean[3]; - Boolean[] Updated2 = new Boolean[3]; - Boolean[] Updated3 = new Boolean[3]; + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // // 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, facet[i], entityID3, ID2[i], + // bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - String name1 = "sample1234.pdf"; - String dropdownValue = - integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // for (int i = 0; i < facet.length; i++) { + // // Update secondary properties for Drop down + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponseEXE3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyDate); + + // if (updateSecondaryPropertyResponseEXE1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated") + // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } - // Update PDF properties - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String renameResp = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - String upd2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - String upd3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - String upd4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - String updInvalid = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - if ("Renamed".equals(renameResp) - && "Updated".equals(upd1) - && "Updated".equals(upd2) - && "Updated".equals(upd3) - && "Updated".equals(upd4) - && "Updated".equals(updInvalid)) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // 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"); + // } + // } - // Update TXT properties - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - String upd = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], bodyBool); - if ("Updated".equals(upd)) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // @Test + // @Order(26) + // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (26): Rename & Update invalid and valid secondary properties for multiple facets + // before entity is saved"); + // System.out.println("Creating entity"); - // Update EXE properties - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValueExe = integrationTestUtils.getDropDownValue(); - String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - for (int i = 0; i < facet.length; i++) { - RequestBody bodyDropdownExe = - RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - RequestBody bodyIntExe = - RequestBody.create( - MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - String upd1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); - String upd2 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); - - if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } + // if (!"Could not create entity".equals(response)) { + // entityID3 = response; + // System.out.println("Entity created"); - if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID3); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + // // Create PDF attachments + // postData.put("mimeType", "application/pdf"); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - // Verify PDF metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals(expectedNames[0], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertNull(metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } + // // Create TXT attachments + // postData.put("mimeType", "application/txt"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID2[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - // Verify TXT metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - assertEquals(expectedNames[1], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertNull(metadata.get("customProperty1_code")); - assertNull(metadata.get("customProperty2")); - assertTrue((Boolean) metadata.get("customProperty6")); - assertNull(metadata.get("customProperty5")); - } + // // Create EXE attachments + // postData.put("mimeType", "application/exe"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // for (int i = 0; i < facet.length; i++) { + // ID3[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + // } - // Verify EXE metadata - for (int i = 0; i < facet.length; i++) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - assertEquals(expectedNames[2], metadata.get("fileName")); - assertNull(metadata.get("customProperty3")); - assertNull(metadata.get("customProperty4")); - assertEquals( - dropdownValueExe, - metadata.get("customProperty1_code")); // Adjust expected value if needed - assertEquals(1234, metadata.get("customProperty2")); - } + // Boolean[] Updated1 = new Boolean[3]; + // Boolean[] Updated2 = new Boolean[3]; + // Boolean[] Updated3 = new Boolean[3]; + + // String name1 = "sample1234.pdf"; + // String dropdownValue = + // integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + + // // Update PDF properties + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String renameResp = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // String upd2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // String upd3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // String upd4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + // String updInvalid = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + // if ("Renamed".equals(renameResp) + // && "Updated".equals(upd1) + // && "Updated".equals(upd2) + // && "Updated".equals(upd3) + // && "Updated".equals(upd4) + // && "Updated".equals(updInvalid)) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } - 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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update unsuccessful for invalid properties and successful for valid attachments"); - } - } - } + // // Update TXT properties + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + // String upd = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], + // bodyBool); + // if ("Updated".equals(upd)) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } + // // Update EXE properties + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValueExe = integrationTestUtils.getDropDownValue(); + // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + // for (int i = 0; i < facet.length; i++) { + // RequestBody bodyDropdownExe = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + // RequestBody bodyIntExe = + // RequestBody.create( + // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + // String upd1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); + // String upd2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); + + // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } - @Test - @Order(27) - void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (27): 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.equals("Entity in draft mode")) { - Boolean Updated1[] = new Boolean[3]; - Boolean Updated2[] = new Boolean[3]; - Boolean Updated3[] = new Boolean[3]; - String name1 = "sample.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - String dropdownValue = integrationTestUtils.getDropDownValue(); - System.out.println("drop down value is: " + dropdownValue); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // PDF - System.out.println("Renaming and updating secondary properties for PDF"); - for (int i = 0; i < facet.length; i++) { - String response1 = - api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // Update secondary properties for String - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // Update secondary properties for LocalDateTime - RequestBody bodyDate = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); - // Update invalid secondary property - String updateSecondaryPropertyResponse5 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - if (response1.equals("Renamed") - && updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponse2.equals("Updated") - && updateSecondaryPropertyResponse3.equals("Updated") - && updateSecondaryPropertyResponse4.equals("Updated") - && updateSecondaryPropertyResponse5.equals("Updated")) { - Updated1[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - } - } - // TXT - System.out.println("Renaming and updating secondary properties for TXT"); - for (int i = 0; i < facet.length; i++) { - // 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, facet[i], entityID3, ID2[i], bodyBool); - if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - Updated2[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - } - } + // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + + // // Verify PDF metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals(expectedNames[0], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertNull(metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } - Integer secondaryPropertyInt3 = 12; - // EXE - System.out.println("Renaming and updating secondary properties for EXE"); - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - for (int i = 0; i < facet.length; i++) { - // Update secondary properties for String - System.out.println("drop down value is: " + dropdownValue1); - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); - - if (updateSecondaryPropertyResponse1.equals("Updated") - && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - Updated3[i] = true; - System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - } - } + // // Verify TXT metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + // assertEquals(expectedNames[1], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertNull(metadata.get("customProperty1_code")); + // assertNull(metadata.get("customProperty2")); + // assertTrue((Boolean) metadata.get("customProperty6")); + // assertNull(metadata.get("customProperty5")); + // } - if (Updated1[0] - && Updated1[1] - && Updated1[2] - && Updated2[0] - && Updated2[1] - && Updated2[2] - && Updated3[0] - && Updated3[1] - && Updated3[2]) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; - // for PDF - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - assertEquals(name[0], FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertNull(FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - // for TXT - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - assertEquals(name[1], FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertNull(FacetMetadata.get("customProperty1_code")); - assertNull(FacetMetadata.get("customProperty2")); - assertFalse((Boolean) FacetMetadata.get("customProperty6")); - assertNull(FacetMetadata.get("customProperty5")); - } - // for EXE - for (int i = 0; i < facet.length; i++) { - Map FacetMetadata = - api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - assertEquals(name[2], FacetMetadata.get("fileName")); - assertNull(FacetMetadata.get("customProperty3")); - assertNull(FacetMetadata.get("customProperty4")); - assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); - assertEquals(12, FacetMetadata.get("customProperty2")); - } + // // Verify EXE metadata + // for (int i = 0; i < facet.length; i++) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + // assertEquals(expectedNames[2], metadata.get("fileName")); + // assertNull(metadata.get("customProperty3")); + // assertNull(metadata.get("customProperty4")); + // assertEquals( + // dropdownValueExe, + // metadata.get("customProperty1_code")); // Adjust expected value if needed + // assertEquals(1234, metadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); - } - 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"); - } - } + // 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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessful for invalid properties and successful for valid + // attachments"); + // } + // } + // } - @Test - @Order(28) - void testNAttachments_NewEntity() throws IOException { - System.out.println( - "Test (28): 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, facet[0], entityID4, srvpath, postData1, file); - if (createResponse1.get(0).equals("Attachment created")) { - ID[0] = createResponse1.get(1); - System.out.println("Attachment created"); - } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - 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, facet[0], entityID4, srvpath, postData2, file); - if (createResponse2.get(0).equals("Attachment created")) { - ID2[0] = createResponse2.get(1); - System.out.println("Attachment created"); - } + // @Test + // @Order(27) + // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (27): 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.equals("Entity in draft mode")) { + // Boolean Updated1[] = new Boolean[3]; + // Boolean Updated2[] = new Boolean[3]; + // Boolean Updated3[] = new Boolean[3]; + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // System.out.println("drop down value is: " + dropdownValue); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // // PDF + // System.out.println("Renaming and updating secondary properties for PDF"); + // for (int i = 0; i < facet.length; i++) { + // String response1 = + // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // // Update secondary properties for String + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // // Update secondary properties for LocalDateTime + // RequestBody bodyDate = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyDate); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], + // bodyBool); + // // Update invalid secondary property + // String updateSecondaryPropertyResponse5 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + // if (response1.equals("Renamed") + // && updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponse2.equals("Updated") + // && updateSecondaryPropertyResponse3.equals("Updated") + // && updateSecondaryPropertyResponse4.equals("Updated") + // && updateSecondaryPropertyResponse5.equals("Updated")) { + // Updated1[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + // } + // } + // // TXT + // System.out.println("Renaming and updating secondary properties for TXT"); + // for (int i = 0; i < facet.length; i++) { + // // 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, facet[i], entityID3, ID2[i], + // bodyBool); + // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + // Updated2[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + // } + // } - 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, facet[0], entityID4, srvpath, postData3, file); - if (createResponse3.get(0).equals("Attachment created")) { - ID[0] = createResponse3.get(1); - System.out.println("Attachment created"); - } + // Integer secondaryPropertyInt3 = 12; + // // EXE + // System.out.println("Renaming and updating secondary properties for EXE"); + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // for (int i = 0; i < facet.length; i++) { + // // Update secondary properties for String + // System.out.println("drop down value is: " + dropdownValue1); + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], + // bodyInt); + + // if (updateSecondaryPropertyResponse1.equals("Updated") + // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + // Updated3[i] = true; + // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + // } + // } - 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, facet[0], entityID4, srvpath, postData3, file); - if (createResponse4.get(0).equals("Attachment created")) { - ID4[0] = createResponse4.get(1); - System.out.println("Attachment created"); - } + // if (Updated1[0] + // && Updated1[1] + // && Updated1[2] + // && Updated2[0] + // && Updated2[1] + // && Updated2[2] + // && Updated3[0] + // && Updated3[1] + // && Updated3[2]) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; + // // for PDF + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + // assertEquals(name[0], FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertNull(FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + // // for TXT + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + // assertEquals(name[1], FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertNull(FacetMetadata.get("customProperty1_code")); + // assertNull(FacetMetadata.get("customProperty2")); + // assertFalse((Boolean) FacetMetadata.get("customProperty6")); + // assertNull(FacetMetadata.get("customProperty5")); + // } + // // for EXE + // for (int i = 0; i < facet.length; i++) { + // Map FacetMetadata = + // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + // assertEquals(name[2], FacetMetadata.get("fileName")); + // assertNull(FacetMetadata.get("customProperty3")); + // assertNull(FacetMetadata.get("customProperty4")); + // assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); + // assertEquals(12, FacetMetadata.get("customProperty2")); + // } - 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, facet[0], entityID4, srvpath, postData3, file); - if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { - testStatus = true; - ID5[0] = 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"); - } - } + // 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: references\\nPage: + // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); + // } + // 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(29) - void testUploadNAttachments() throws IOException { - System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); + // @Test + // @Order(28) + // void testNAttachments_NewEntity() throws IOException { + // System.out.println( + // "Test (28): 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; - 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, facet[0], 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"); - } - } - } + // System.out.println("Entity created"); - @Test - @Order(30) - void testDiscardDraftWithoutAttachments() { - System.out.println("Test (30) : Discard draft without adding attachments"); - Boolean testStatus = false; + // System.out.println("Creating attachment PDF"); + // ClassLoader classLoader = getClass().getClassLoader(); - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID6 = response; - response = api.deleteEntityDraft(appUrl, entityName, entityID6); - if (response.equals("Entity Draft Deleted")) { - testStatus = true; - } - } - if (!testStatus) { - fail("Draft was not discarded properly"); - } - } + // 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"); - @Test - @Order(31) - void testDiscardDraftWithAttachments() throws IOException { - System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); - boolean testStatus = false; + // List createResponse1 = + // api.createAttachment(appUrl, entityName, facet[0], entityID4, srvpath, postData1, + // file); + // if (createResponse1.get(0).equals("Attachment created")) { + // ID[0] = createResponse1.get(1); + // System.out.println("Attachment created"); + // } - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID6 = response; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID6); - 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 < facet.length; i++) { - List createResponse = - api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, file); - if ("Attachment created".equals(createResponse.get(0))) { - System.out.println("Attachment created in facet: " + facet[i]); - } else { - System.out.println("Attachment creation failed in facet: " + facet[i]); - } - } - response = api.deleteEntityDraft(appUrl, entityName, entityID6); - if ("Entity Draft Deleted".equals(response)) { - testStatus = true; - } - } - if (!testStatus) { - fail("Draft with attachments was not discarded properly"); - } - } + // 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, facet[0], entityID4, srvpath, postData2, + // file); + // if (createResponse2.get(0).equals("Attachment created")) { + // ID2[0] = createResponse2.get(1); + // System.out.println("Attachment created"); + // } - @Test - @Order(32) - void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { - System.out.println("Test (32): Upload to all facets, delete one, and create 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, facet[0], entityID4, srvpath, postData3, + // file); + // if (createResponse3.get(0).equals("Attachment created")) { + // ID[0] = createResponse3.get(1); + // System.out.println("Attachment created"); + // } - boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // 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, facet[0], entityID4, srvpath, postData3, + // file); + // if (createResponse4.get(0).equals("Attachment created")) { + // ID4[0] = createResponse4.get(1); + // System.out.println("Attachment created"); + // } - if (!"Could not create entity".equals(response)) { - entityID5 = response; - ClassLoader classLoader = getClass().getClassLoader(); - - File file1 = - new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - File file2 = - new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); - - Map postData2 = new HashMap<>(postData1); - postData2.put("up__ID", entityID5); - postData2.put("mimeType", "text/plain"); - postData2.put("createdAt", new Date().toString()); - postData2.put("createdBy", "test@test.com"); - postData2.put("modifiedBy", "test@test.com"); - - boolean allCreated = true; - for (int i = 0; i < facet.length; i++) { - List response1 = - api.createAttachment( - appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); - List response2 = - api.createAttachment( - appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); - - if (response1.get(0).equals("Attachment created") - && response2.get(0).equals("Attachment created")) { - ID4[i] = response1.get(1); // to keep one - ID5[i] = response2.get(1); // will delete this one - } else { - allCreated = false; - break; - } + // 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, facet[0], entityID4, srvpath, postData3, + // file); + // if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { + // testStatus = true; + // ID5[0] = 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"); + // } + // } - String deleteResponse = - api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); - if (!"Deleted".equals(deleteResponse)) { - allCreated = false; - break; - } - } + // @Test + // @Order(29) + // void testUploadNAttachments() throws IOException { + // System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); - if (allCreated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } + // 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, facet[0], 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"); + // } + // } + // } - if (!testStatus) { - fail("Failed to upload multiple facet entries, delete one per facet and create entity"); - } - } + // @Test + // @Order(30) + // void testDiscardDraftWithoutAttachments() { + // System.out.println("Test (30) : Discard draft without adding attachments"); + // Boolean testStatus = false; - @Test - @Order(33) - void testUpdateEntityDraft() throws IOException { - System.out.println("Test (33): Update entity draft with new facet content"); - boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID6 = response; + // response = api.deleteEntityDraft(appUrl, entityName, entityID6); + // if (response.equals("Entity Draft Deleted")) { + // testStatus = true; + // } + // } + // if (!testStatus) { + // fail("Draft was not discarded properly"); + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + // @Test + // @Order(31) + // void testDiscardDraftWithAttachments() throws IOException { + // System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); + // boolean testStatus = false; - File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID6 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - 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"); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID6); + // 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 < facet.length; i++) { + // List createResponse = + // api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, + // file); + // if ("Attachment created".equals(createResponse.get(0))) { + // System.out.println("Attachment created in facet: " + facet[i]); + // } else { + // System.out.println("Attachment creation failed in facet: " + facet[i]); + // } + // } + // response = api.deleteEntityDraft(appUrl, entityName, entityID6); + // if ("Entity Draft Deleted".equals(response)) { + // testStatus = true; + // } + // } + // if (!testStatus) { + // fail("Draft with attachments was not discarded properly"); + // } + // } - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); - if ("Entity in draft mode".equals(response)) { - boolean allCreated = true; + // @Test + // @Order(32) + // void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { + // System.out.println("Test (32): Upload to all facets, delete one, and create entity"); - for (int i = 0; i < facet.length; i++) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); - if (!"Attachment created".equals(createResponse.get(0))) { - allCreated = false; - } - } + // boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (allCreated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - if ("Saved".equals(response)) { - testStatus = true; - } - } - } - api.deleteEntity(appUrl, entityName, entityID5); - if (!testStatus) { - fail("Failed to update draft with new attachments for all facets"); - } - } + // if (!"Could not create entity".equals(response)) { + // entityID5 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + + // File file1 = + // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + // File file2 = + // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); + + // Map postData2 = new HashMap<>(postData1); + // postData2.put("up__ID", entityID5); + // postData2.put("mimeType", "text/plain"); + // postData2.put("createdAt", new Date().toString()); + // postData2.put("createdBy", "test@test.com"); + // postData2.put("modifiedBy", "test@test.com"); + + // boolean allCreated = true; + // for (int i = 0; i < facet.length; i++) { + // List response1 = + // api.createAttachment( + // appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); + // List response2 = + // api.createAttachment( + // appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); + + // if (response1.get(0).equals("Attachment created") + // && response2.get(0).equals("Attachment created")) { + // ID4[i] = response1.get(1); // to keep one + // ID5[i] = response2.get(1); // will delete this one + // } else { + // allCreated = false; + // break; + // } - @Test - @Order(34) - void testUploadAttachmentWithoutSDMRole() throws IOException { - System.out.println("Test (34): Upload attachment across facets without SDM role"); - boolean testStatus = true; - - String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID7 = response; - 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", 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 deleteResponse = + // api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); + // if (!"Deleted".equals(deleteResponse)) { + // allCreated = false; + // break; + // } + // } - for (int i = 0; i < facet.length; i++) { - List createResponse = - apiNoRoles.createAttachment( - appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); - String check = createResponse.get(0); - String expectedError = - "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions to upload attachments. Please contact your administrator for access.\"}}"; - if (!expectedError.equals(check)) { - testStatus = false; - } - } - } - api.deleteEntityDraft(appUrl, entityName, entityID7); - if (!testStatus) { - fail("Attachment uploaded without SDM role for one or more facets"); - } - } + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } - @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<>(); - for (int i = 0; i < 3; i++) { - attachments.add(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 (!testStatus) { + // fail("Failed to upload multiple facet entries, delete one per facet and create entity"); + // } + // } - for (int i = 0; i < facet.length; i++) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - copyAttachmentSourceEntity, - srvpath, - postData, - file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - } - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facet[i], 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"); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - if (sourceObjectIds.size() == 6) { - String copyResponse; - int i = 0; - for (String facetName : facet) { - if (i != 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } - copyResponse = - api.copyAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - i += 2; - if (copyResponse.equals("Attachments copied successfully")) { - // Fetch copied attachment IDs from target draft - List> copiedMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - List copiedAttachmentIds = - copiedMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - 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(33) + // void testUpdateEntityDraft() throws IOException { + // System.out.println("Test (33): Update entity draft with new facet content"); + // boolean testStatus = false; - @Test - @Order(36) - void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { - System.out.println( - "Test (36): Copy incorrect 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")) { - if (sourceObjectIds.size() == 6) { - int i = 0; - for (String facet : facet) { - try { - List currentFacetObjectIds = - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - currentFacetObjectIds.add("incorrectObjectId"); - if (currentFacetObjectIds.size() != 3) { - fail("Not enough object IDs to copy attachments for facet: " + facet); - } - api.copyAttachment( - appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, currentFacetObjectIds); - fail("Copy attachments did not throw an error"); - } catch (IOException e) { - i += 2; - } - } - 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"); - } - } + // 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 ("Entity in draft mode".equals(response)) { + // boolean allCreated = true; + + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); + // if (!"Attachment created".equals(createResponse.get(0))) { + // allCreated = false; + // } + // } - @Test - @Order(37) - void testCopyAttachmentWithNotesField() throws IOException { - System.out.println( - "Test (37): Create entity with attachments containing notes in multiple facets, copy to new entity and verify notes field"); - Boolean testStatus = false; + // if (allCreated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } + // } + // api.deleteEntity(appUrl, entityName, entityID5); + // if (!testStatus) { + // fail("Failed to update draft with new attachments for all facets"); + // } + // } - copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // @Test + // @Order(34) + // void testUploadAttachmentWithoutSDMRole() throws IOException { + // System.out.println("Test (34): Upload attachment across facets without SDM role"); + // boolean testStatus = true; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - String notesValue = "This is a test note for copy attachment verification"; - MediaType mediaType = MediaType.parse("application/json"); - - for (String facetName : facet) { - 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); + // String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID7 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - String sourceAttachmentId = createResponse.get(1); + // 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 jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); + // for (int i = 0; i < facet.length; i++) { + // List createResponse = + // apiNoRoles.createAttachment( + // appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); + // String check = createResponse.get(0); + // String expectedError = + // "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions + // to upload attachments. Please contact your administrator for access.\"}}"; + // if (!expectedError.equals(check)) { + // testStatus = false; + // } + // } + // } + // api.deleteEntityDraft(appUrl, entityName, entityID7); + // if (!testStatus) { + // fail("Attachment uploaded without SDM role for one or more facets"); + // } + // } - String updateResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateBody); + // @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<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(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 (!updateResponse.equals("Updated")) { - fail("Could not update attachment notes field in facet: " + facetName); - } - } + // for (int i = 0; i < facet.length; i++) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // copyAttachmentSourceEntity, + // srvpath, + // postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // } + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facet[i], 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"); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + // if (sourceObjectIds.size() == 6) { + // String copyResponse; + // int i = 0; + // for (String facetName : facet) { + // if (i != 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } + // copyResponse = + // api.copyAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + // i += 2; + // if (copyResponse.equals("Attachments copied successfully")) { + // // Fetch copied attachment IDs from target draft + // List> copiedMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // List copiedAttachmentIds = + // copiedMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // 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"); + // } + // } - List objectIdsToStore = new ArrayList<>(); - for (String facetName : facet) { - List> sourceAttachmentsMetadata = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); + // @Test + // @Order(36) + // void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (36): Copy incorrect 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")) { + // if (sourceObjectIds.size() == 6) { + // int i = 0; + // for (String facet : facet) { + // try { + // List currentFacetObjectIds = + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + // currentFacetObjectIds.add("incorrectObjectId"); + // if (currentFacetObjectIds.size() != 3) { + // fail("Not enough object IDs to copy attachments for facet: " + facet); + // } + // api.copyAttachment( + // appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, + // currentFacetObjectIds); + // fail("Copy attachments did not throw an error"); + // } catch (IOException e) { + // i += 2; + // } + // } + // 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 (sourceAttachmentsMetadata.isEmpty()) { - fail("No attachments found in source entity for facet: " + facetName); - } + // @Test + // @Order(37) + // void testCopyAttachmentWithNotesField() throws IOException { + // System.out.println( + // "Test (37): Create entity with attachments containing notes in multiple facets, copy to + // new entity and verify notes field"); + // Boolean testStatus = false; - Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); + // copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId for facet: " + facetName); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // String notesValue = "This is a test note for copy attachment verification"; + // MediaType mediaType = MediaType.parse("application/json"); - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - objectIdsToStore.add(sourceObjectId); - - 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 for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + sourceNoteValue); - } - } + // for (String facetName : facet) { + // 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"); - int startIndex = sourceObjectIds.size(); - sourceObjectIds.addAll(objectIdsToStore); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } - copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String sourceAttachmentId = createResponse.get(1); - int facetIndex = 0; - for (String facetName : facet) { - if (facetIndex > 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + // 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 in facet: " + facetName); + // } + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity for facet: " + facetName); - } + // List objectIdsToStore = new ArrayList<>(); + // for (String facetName : facet) { + // List> sourceAttachmentsMetadata = + // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity for facet: " + facetName); - } + // if (sourceAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in source entity for facet: " + facetName); + // } - facetIndex++; - } + // Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); - for (String facetName : facet) { - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId for facet: " + facetName); + // } - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity for facet: " + facetName); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // objectIdsToStore.add(sourceObjectId); + + // 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 for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + sourceNoteValue); + // } + // } - 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 for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + copiedNoteValue); - } + // int startIndex = sourceObjectIds.size(); + // sourceObjectIds.addAll(objectIdsToStore); - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment from target entity for facet: " + facetName); - } else { - testStatus = true; - } - } + // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - if (!testStatus) { - fail( - "Could not verify that notes field was copied from source to target attachment for all facets"); - } - } + // int facetIndex = 0; + // for (String facetName : facet) { + // if (facetIndex > 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } - @Test - @Order(38) - void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { - System.out.println( - "Test (38): Verify that secondary properties are preserved when copying attachments between entities across multiple facets"); - Boolean testStatus = false; + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // String copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample1.pdf").getFile()); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity for facet: " + facetName); + // } - List objectIdsToStore = new ArrayList<>(); + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity for facet: " + facetName); + // } - for (String facetName : facet) { - 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"); + // facetIndex++; + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // for (String facetName : facet) { + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity for facet: " + facetName); + // } - String sourceAttachmentId = createResponse.get(1); - - 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 for facet: " + facetName); - } + // 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 for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + copiedNoteValue); + // } - 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 for facet: " + facetName); - } - } + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // Save source entity to persist attachments before fetching metadata - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after creating attachments"); - } + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment from target entity for facet: " + facetName); + // } else { + // testStatus = true; + // } + // } - Integer customProperty2Value = 12345; - for (String facetName : facet) { - List> sourceAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + // if (!testStatus) { + // fail( + // "Could not verify that notes field was copied from source to target attachment for all + // facets"); + // } + // } - Map sourceAttachmentMetadata = - sourceAttachmentsMetadata.stream() - .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // @Test + // @Order(38) + // void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { + // System.out.println( + // "Test (38): Verify that secondary properties are preserved when copying attachments + // between entities across multiple facets"); + // 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 (sourceAttachmentMetadata == null) { - fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample1.pdf").getFile()); - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId for facet: " + facetName); - } + // List objectIdsToStore = new ArrayList<>(); - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - objectIdsToStore.add(sourceObjectId); - - 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 for facet " - + facetName - + ". Expected: true, Got: " - + sourceCustomProperty6); - } + // for (String facetName : facet) { + // 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 (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - int startIndex = sourceObjectIds.size(); - sourceObjectIds.addAll(objectIdsToStore); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } - int facetIndex = 0; - for (String facetName : facet) { - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity"); - } + // String sourceAttachmentId = createResponse.get(1); + + // 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 for facet: " + + // facetName); + // } - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + // 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 for facet: " + facetName); + // } + // } - String copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // // Save source entity to persist attachments before fetching metadata + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after creating attachments"); + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity for facet: " + facetName); - } + // Integer customProperty2Value = 12345; + // for (String facetName : facet) { + // List> sourceAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - // Fetch copied attachment IDs from target draft - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity for facet: " + facetName); - } + // Map sourceAttachmentMetadata = + // sourceAttachmentsMetadata.stream() + // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - facetIndex++; - } + // if (sourceAttachmentMetadata == null) { + // fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); + // } - for (String facetName : facet) { - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId for facet: " + facetName); + // } - Map copiedAttachmentMetadata = - targetAttachmentsMetadata.stream() - .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // objectIdsToStore.add(sourceObjectId); + + // 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 for facet " + // + facetName + // + ". Expected: true, Got: " + // + sourceCustomProperty6); + // } - if (copiedAttachmentMetadata == null) { - fail( - "Could not find the copied attachment with file in target entity for facet: " - + facetName); - } + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } + // } - Boolean copiedCustomProperty6 = - copiedAttachmentMetadata.get("customProperty6") != null - ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - : null; - Integer copiedCustomProperty2 = - copiedAttachmentMetadata.get("customProperty2") != null - ? (Integer) copiedAttachmentMetadata.get("customProperty2") - : null; - - if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - fail( - "DocumentInfoRecordBoolean was not properly copied for facet " - + facetName - + ". Expected: true, Got: " - + copiedCustomProperty6); - } + // int startIndex = sourceObjectIds.size(); + // sourceObjectIds.addAll(objectIdsToStore); - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // int facetIndex = 0; + // for (String facetName : facet) { + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity"); + // } - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment from target entity for facet: " + facetName); - } else { - testStatus = true; - } - } + // String copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - if (!testStatus) { - fail( - "Could not verify that all secondary properties were copied from source to target attachment for all facets"); - } - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity for facet: " + facetName); + // } - @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 across multiple facets"); - Boolean testStatus = false; + // // Fetch copied attachment IDs from target draft + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity for facet: " + facetName); + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // facetIndex++; + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample2.pdf").getFile()); - - String notesValue = "This attachment has both notes and secondary properties for testing"; - MediaType mediaType = MediaType.parse("application/json"); - Integer customProperty2Value = 99999; - List objectIdsToStore = new ArrayList<>(); - - for (String facetName : facet) { - 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); + // for (String facetName : facet) { + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in facet: " + facetName); - } + // Map copiedAttachmentMetadata = + // targetAttachmentsMetadata.stream() + // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - String sourceAttachmentId = createResponse.get(1); + // if (copiedAttachmentMetadata == null) { + // fail( + // "Could not find the copied attachment with file in target entity for facet: " + // + facetName); + // } - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + // Boolean copiedCustomProperty6 = + // copiedAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + // : null; + // Integer copiedCustomProperty2 = + // copiedAttachmentMetadata.get("customProperty2") != null + // ? (Integer) copiedAttachmentMetadata.get("customProperty2") + // : null; + + // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean was not properly copied for facet " + // + facetName + // + ". Expected: true, Got: " + // + copiedCustomProperty6); + // } - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateNotesBody); + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update attachment notes field for facet: " + facetName); - } + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - 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 for facet: " + facetName); - } + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment from target entity for facet: " + facetName); + // } else { + // testStatus = true; + // } + // } - 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 (!testStatus) { + // fail( + // "Could not verify that all secondary properties were copied from source to target + // attachment for all facets"); + // } + // } - if (!updateSecondaryPropertyResponse2.equals("Updated")) { - fail("Could not update attachment customProperty2 field for facet: " + facetName); - } - } + // @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 across multiple facets"); + // Boolean testStatus = false; + + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyCustomSourceEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity"); + // } - // Save source entity to persist attachments before fetching metadata and copying - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after creating attachments"); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - for (String facetName : facet) { - List> sourceAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + // String notesValue = "This attachment has both notes and secondary properties for testing"; + // MediaType mediaType = MediaType.parse("application/json"); + // Integer customProperty2Value = 99999; + // List objectIdsToStore = new ArrayList<>(); - Map sourceAttachmentMetadata = - sourceAttachmentsMetadata.stream() - .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // for (String facetName : facet) { + // 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 (sourceAttachmentMetadata == null) { - fail("Could not find attachment with file in facet: " + facetName); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId for facet: " + facetName); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in facet: " + facetName); + // } - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - objectIdsToStore.add(sourceObjectId); - - 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 for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + sourceNoteValue); - } + // String sourceAttachmentId = createResponse.get(1); - 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 for facet " - + facetName - + ". Expected: true, Got: " - + sourceCustomProperty6); - } + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } - } + // String updateNotesResponse = + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facetName, + // copyCustomSourceEntity, + // sourceAttachmentId, + // updateNotesBody); - int startIndex = sourceObjectIds.size(); - sourceObjectIds.addAll(objectIdsToStore); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update attachment notes field for facet: " + facetName); + // } - int facetIndex = 0; - for (String facetName : facet) { - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity"); - } + // 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 for facet: " + + // facetName); + // } - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + // 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 for facet: " + facetName); + // } + // } - String copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // // Save source entity to persist attachments before fetching metadata and copying + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after creating attachments"); + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity for facet: " + facetName); - } + // for (String facetName : facet) { + // List> sourceAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity for facet: " + facetName); - } + // Map sourceAttachmentMetadata = + // sourceAttachmentsMetadata.stream() + // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - facetIndex++; - } + // if (sourceAttachmentMetadata == null) { + // fail("Could not find attachment with file in facet: " + facetName); + // } - for (String facetName : facet) { - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId for facet: " + facetName); + // } - Map copiedAttachmentMetadata = - targetAttachmentsMetadata.stream() - .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // objectIdsToStore.add(sourceObjectId); + + // 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 for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + sourceNoteValue); + // } - if (copiedAttachmentMetadata == null) { - fail( - "Could not find the copied attachment with file in target entity for facet: " - + facetName); - } + // 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 for facet " + // + facetName + // + ". Expected: true, Got: " + // + sourceCustomProperty6); + // } - String copiedNoteValue = - copiedAttachmentMetadata.get("note") != null - ? copiedAttachmentMetadata.get("note").toString() - : null; - - if (!notesValue.equals(copiedNoteValue)) { - fail( - "Notes field was not properly copied for facet " - + facetName - + ". Expected: " - + notesValue - + ", Got: " - + copiedNoteValue); - } + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } + // } - Boolean copiedCustomProperty6 = - copiedAttachmentMetadata.get("customProperty6") != null - ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - : null; - Integer copiedCustomProperty2 = - copiedAttachmentMetadata.get("customProperty2") != null - ? (Integer) copiedAttachmentMetadata.get("customProperty2") - : null; - - if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - fail( - "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " - + facetName - + ". Expected: true, Got: " - + copiedCustomProperty6); - } - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied for facet " - + facetName - + ". Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // int startIndex = sourceObjectIds.size(); + // sourceObjectIds.addAll(objectIdsToStore); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment from target entity for facet: " + facetName); - } else { - testStatus = true; - } - } - api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); - api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); - if (!testStatus) { - fail( - "Could not verify that notes field and all secondary properties were copied from source to target attachment for all facets"); - } - } + // int facetIndex = 0; + // for (String facetName : facet) { + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target 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<>(); - for (int i = 0; i < 3; i++) { - attachments.add(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"), "sample3.pdf"); - Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); - File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - copyAttachmentSourceEntity, - srvpath, - postData, - file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - } - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); - attachmentsMetadata.add(fetchAttachmentMetadataResponse); - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } - } + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - sourceObjectIds.clear(); - for (Map metadata : attachmentsMetadata) { - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - if (sourceObjectIds.size() == 6) { - String copyResponse; - int i = 0; - for (String facetName : facet) { - if (i != 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } - List currentFacetObjectIds = - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - if (currentFacetObjectIds.size() != 2) { - fail("Not enough object IDs to copy attachments for facet: " + facet); - } - copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, currentFacetObjectIds); - i += 2; - if (copyResponse.equals("Attachments copied successfully")) { - // Fetch copied attachment IDs from target draft - List> copiedMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - List copiedAttachmentIds = - copiedMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - 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"); - } - } - } - } 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 copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - @Test - @Order(41) - void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { - System.out.println("Test (41): Copy attachments from one entity to another new entity"); - 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")) { - if (sourceObjectIds.size() == 6) { - int i = 0; - for (String facetName : facet) { - List currentFacetObjectIds = - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - currentFacetObjectIds.add("incorrectObjectId"); - if (currentFacetObjectIds.size() != 3) { - fail("Not enough object IDs to copy attachments for facet: " + facet); - } - try { - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error"); - } catch (IOException e) { - i += 2; - } - } - 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 (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity for facet: " + facetName); + // } - @Test - @Order(42) - void testCreateLinkSuccess() throws IOException { - System.out.println("Test (42): Create link in entity"); - List attachments = new ArrayList<>(); + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity for facet: " + facetName); + // } - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // facetIndex++; + // } - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - for (String facetName : facet) { - 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")) { - fail("Could not create links for facet : " + facetName + createLinkResponse1); - } - } + // for (String facetName : facet) { + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // Map copiedAttachmentMetadata = + // targetAttachmentsMetadata.stream() + // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - for (String facetName : facet) { - 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); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open created link in facet : " + facetName); - } - } - } - } + // if (copiedAttachmentMetadata == null) { + // fail( + // "Could not find the copied attachment with file in target entity for facet: " + // + facetName); + // } - @Test - @Order(43) - void testCreateLinkDifferentEntity() throws IOException { - System.out.println("Test (43): Create link with same name in different entity"); + // String copiedNoteValue = + // copiedAttachmentMetadata.get("note") != null + // ? copiedAttachmentMetadata.get("note").toString() + // : null; + + // if (!notesValue.equals(copiedNoteValue)) { + // fail( + // "Notes field was not properly copied for facet " + // + facetName + // + ". Expected: " + // + notesValue + // + ", Got: " + // + copiedNoteValue); + // } - String createLinkDifferentEntity = - api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkDifferentEntity.equals("Could not edit entity")) { - fail("Could not create entity"); - } + // Boolean copiedCustomProperty6 = + // copiedAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + // : null; + // Integer copiedCustomProperty2 = + // copiedAttachmentMetadata.get("customProperty2") != null + // ? (Integer) copiedAttachmentMetadata.get("customProperty2") + // : null; + + // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " + // + facetName + // + ". Expected: true, Got: " + // + copiedCustomProperty6); + // } + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied for facet " + // + facetName + // + ". Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - String linkName = "sample"; - String linkUrl = "https://example.com"; - for (String facetName : facet) { - 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"); - } - } + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment from target entity for facet: " + facetName); + // } else { + // testStatus = true; + // } + // } + // api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); + // api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); + // if (!testStatus) { + // fail( + // "Could not verify that notes field and all secondary properties were copied from source + // to target attachment for all facets"); + // } + // } - String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkDifferentEntity); - if (!response.equals("Saved")) { - fail("Could not save 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<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(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"), "sample3.pdf"); + // Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); + // File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // copyAttachmentSourceEntity, + // srvpath, + // postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // } + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); + // attachmentsMetadata.add(fetchAttachmentMetadataResponse); + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } + // } - response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete 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"); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + // if (sourceObjectIds.size() == 6) { + // String copyResponse; + // int i = 0; + // for (String facetName : facet) { + // if (i != 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } + // List currentFacetObjectIds = + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + // if (currentFacetObjectIds.size() != 2) { + // fail("Not enough object IDs to copy attachments for facet: " + facet); + // } + // copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, + // currentFacetObjectIds); + // i += 2; + // if (copyResponse.equals("Attachments copied successfully")) { + // // Fetch copied attachment IDs from target draft + // List> copiedMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // List copiedAttachmentIds = + // copiedMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // 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"); + // } + // } + // } + // } 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(44) - void testCreateLinkFailure() throws IOException { - System.out.println("Test (41): Create link fails due to invalid URL and name"); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (editEntityResponse.equals("Could not edit entity")) { - fail("Could not edit entity"); - } - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "example.com"; - try { - String response = - 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 < 6; i++) { - api.createLink( - appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + linkUrl); - } - System.out.println("Created 5 links in facet: " + facetName); - if (!facetName.equals("footnotes")) { - 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); - if (facetName.equals("references")) { - assertEquals("Cannot upload more than 5 attachments.", errorMessage); - } else if (facetName.equals("attachments")) { - assertEquals("Cannot upload more than 4 attachments.", errorMessage); - } - } - } + // @Test + // @Order(41) + // void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { + // System.out.println("Test (41): Copy attachments from one entity to another new entity"); + // 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")) { + // if (sourceObjectIds.size() == 6) { + // int i = 0; + // for (String facetName : facet) { + // List currentFacetObjectIds = + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + // currentFacetObjectIds.add("incorrectObjectId"); + // if (currentFacetObjectIds.size() != 3) { + // fail("Not enough object IDs to copy attachments for facet: " + facet); + // } + // try { + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error"); + // } catch (IOException e) { + // i += 2; + // } + // } + // 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 response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } + // @Test + // @Order(42) + // void testCreateLinkSuccess() throws IOException { + // System.out.println("Test (42): Create link in entity"); + // List attachments = new ArrayList<>(); - response = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - @Test - @Order(45) - void testCreateLinkNoSDMRoles() throws IOException { - System.out.println("Test (42): Create link fails due to no SDM roles assigned"); + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // for (String facetName : facet) { + // 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")) { + // fail("Could not create links for facet : " + facetName + createLinkResponse1); + // } + // } - String createLinkEntityNoSDMRoles = - apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { - fail("Could not create entity"); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - for (String facetName : facet) { - 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); - } - } + // for (String facetName : facet) { + // 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); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open created link in facet : " + facetName); + // } + // } + // } + // } - String response = - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } + // @Test + // @Order(43) + // void testCreateLinkDifferentEntity() throws IOException { + // System.out.println("Test (43): Create link with same name in different entity"); - response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } + // String createLinkDifferentEntity = + // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkDifferentEntity.equals("Could not edit entity")) { + // fail("Could not create entity"); + // } - @Test - @Order(46) - void testDeleteLink() throws IOException { - System.out.println("Test (43): Delete link in entity"); - List> attachments = new ArrayList<>(); + // String linkName = "sample"; + // String linkUrl = "https://example.com"; + // for (String facetName : facet) { + // 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 createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // String response = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkDifferentEntity); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } - for (String facetName : facet) { - 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 for facet : " + facetName); - } - } + // response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete 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 (41): Create link fails due to invalid URL and name"); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (editEntityResponse.equals("Could not edit entity")) { + // fail("Could not edit entity"); + // } + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "example.com"; + // try { + // String response = + // 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 < 6; i++) { + // api.createLink( + // appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + + // linkUrl); + // } + // System.out.println("Created 5 links in facet: " + facetName); + // if (!facetName.equals("footnotes")) { + // 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); + // if (facetName.equals("references")) { + // assertEquals("Cannot upload more than 5 attachments.", errorMessage); + // } else if (facetName.equals("attachments")) { + // assertEquals("Cannot upload more than 4 attachments.", errorMessage); + // } + // } + // } - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } + // String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!response.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"); - } + // response = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - int index = 0; - for (String facetName : facet) { - String deleteLinkResponse = - api.deleteAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); - System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - if (!deleteLinkResponse.equals("Deleted")) { - fail("Could not delete created link"); - } - index += 1; - } + // @Test + // @Order(45) + // void testCreateLinkNoSDMRoles() throws IOException { + // System.out.println("Test (42): Create link fails due to no SDM roles assigned"); - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // String createLinkEntityNoSDMRoles = + // apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { + // fail("Could not create entity"); + // } - index = 0; - attachments.clear(); - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - System.out.println( - "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - if (attachments.get(index).size() != 0) { - fail("Link wasn't deleted"); - } - index += 1; - } + // for (String facetName : facet) { + // 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 = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } + // String response = + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } - @Test - @Order(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (44): Rename link in entity"); - List> attachments = new ArrayList<>(); + // response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // @Test + // @Order(46) + // void testDeleteLink() throws IOException { + // System.out.println("Test (43): Delete link in entity"); + // List> attachments = new ArrayList<>(); - for (String facetName : facet) { - 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 createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // for (String facetName : facet) { + // 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 for facet : " + facetName); + // } + // } - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } + // String 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"); - } + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } - int index = 0; - for (String facetName : facet) { - successfullyRenamedAttachments.add(attachments.get(index).get(0)); - String renameLinkResponse = - api.renameAttachment( - appUrl, - entityName, - facetName, - createLinkEntity, - attachments.get(index).get(0), - "sampleRenamed"); - if (!renameLinkResponse.equals("Renamed")) { - fail("Could not Renamed created link"); - } - index += 1; - } + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - } + // int index = 0; + // for (String facetName : facet) { + // String deleteLinkResponse = + // api.deleteAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); + // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + // if (!deleteLinkResponse.equals("Deleted")) { + // fail("Could not delete created link"); + // } + // index += 1; + // } - @Test - @Order(48) - void testRenameLinkDuplicate() throws IOException { - System.out.println("Test (45): 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"); - } + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - int index = 0; - for (String facetName : facet) { - 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"); - } - } + // index = 0; + // attachments.clear(); + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // System.out.println( + // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + // if (attachments.get(index).size() != 0) { + // fail("Link wasn't deleted"); + // } + // index += 1; + // } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (saveResponse.equals("Could not save entity")) { - fail("Could not save entity"); - } + // String response = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } - index = 0; - List facetAttachments; - for (String facetName : facet) { - int lambdaIndex = index; - facetAttachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .filter( - item -> - !successfullyRenamedAttachments - .get(lambdaIndex) - .equals(item.get("ID"))) // skip unwanted filename - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - index += 1; - attachments.add(facetAttachments.get(0)); - } + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (44): Rename link in entity"); + // List> attachments = new ArrayList<>(); - System.out.println("Attachments to be renamed: " + attachments); - String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!response.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - index = 0; - for (String facetName : facet) { - api.renameAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(index), "sampleRenamed"); - index += 1; - } + // for (String facetName : facet) { + // 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 saveError = 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: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - ObjectMapper mapper = new ObjectMapper(); - assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); - if (!deleteEntityResponse.equals("Entity Draft Deleted")) { - fail("Entity draft not deleted"); - } - } + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } - @Test - @Order(49) - void testRenameLinkUnsupportedCharacters() throws IOException { - System.out.println( - "Test (46): Rename link in entity fails due to unsupported characters in name"); - List> attachments = new ArrayList<>(); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // int index = 0; + // for (String facetName : facet) { + // successfullyRenamedAttachments.add(attachments.get(index).get(0)); + // String renameLinkResponse = + // api.renameAttachment( + // appUrl, + // entityName, + // facetName, + // createLinkEntity, + // attachments.get(index).get(0), + // "sampleRenamed"); + // if (!renameLinkResponse.equals("Renamed")) { + // fail("Could not Renamed created link"); + // } + // index += 1; + // } - String linkName = "sample2"; - String linkUrl = "https://www.example.com"; + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } + // } - for (String facetName : facet) { - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link"); - } - } + // @Test + // @Order(48) + // void testRenameLinkDuplicate() throws IOException { + // System.out.println("Test (45): 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // int index = 0; + // for (String facetName : facet) { + // 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"); + // } + // } - for (String facetName : facet) { - attachments.add( - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList())); - } + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (saveResponse.equals("Could not save entity")) { + // 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"); - } + // index = 0; + // List facetAttachments; + // for (String facetName : facet) { + // int lambdaIndex = index; + // facetAttachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .filter( + // item -> + // !successfullyRenamedAttachments + // .get(lambdaIndex) + // .equals(item.get("ID"))) // skip unwanted filename + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // index += 1; + // attachments.add(facetAttachments.get(0)); + // } - int index = 0; - for (String facetName : facet) { - api.renameAttachment( - appUrl, - entityName, - facetName, - createLinkEntity, - attachments.get(index).get(0), - "sampleRenamed//"); - index += 1; - } + // System.out.println("Attachments to be renamed: " + attachments); + // String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!response.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - String error = - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - String expectedError = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - ObjectMapper mapper = new ObjectMapper(); - assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); + // index = 0; + // for (String facetName : facet) { + // api.renameAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(index), + // "sampleRenamed"); + // index += 1; + // } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!deleteEntityResponse.equals("Entity Deleted")) { - fail("Entity draft not deleted"); - } - } + // String saveError = 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: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named + // \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An + // object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: + // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // 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 (47): Edit existing link in entity"); - List> attachmentsPerFacet = new ArrayList<>(); + // @Test + // @Order(49) + // void testRenameLinkUnsupportedCharacters() throws IOException { + // System.out.println( + // "Test (46): Rename link in entity fails due to unsupported characters in name"); + // List> attachments = new ArrayList<>(); - editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (editLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - for (String facetName : facet) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facetName); - } - } + // String linkName = "sample2"; + // String linkUrl = "https://www.example.com"; - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // for (String facetName : facet) { + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } + // } - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - for (String facetName : facet) { - List attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // for (String facetName : facet) { + // attachments.add( + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList())); + // } - if (attachments.isEmpty()) { - fail("Could not find link in facet: " + facetName); - } - attachmentsPerFacet.add(attachments); - } + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - int index = 0; - for (String facetName : facet) { - String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); - } - index++; - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - - int verificationIndex = 0; - for (String facetName : facet) { - List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); - for (String attachmentId : attachmentsInFacet) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open edited link " + attachmentId + " in facet: " + facetName); - } - } - verificationIndex++; - } - } + // int index = 0; + // for (String facetName : facet) { + // api.renameAttachment( + // appUrl, + // entityName, + // facetName, + // createLinkEntity, + // attachments.get(index).get(0), + // "sampleRenamed//"); + // index += 1; + // } - @Test - @Order(51) - void testEditLinkFailureInvalidURL() throws IOException { - System.out.println("Test (48): Edit existing link with invalid url"); - List> attachmentsPerFacet = new ArrayList<>(); - - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String error = + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // String expectedError = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" + // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: + // attachments\\nPage: + // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // ObjectMapper mapper = new ObjectMapper(); + // assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); + + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!deleteEntityResponse.equals("Entity Deleted")) { + // fail("Entity draft not deleted"); + // } + // } - for (String facetName : facet) { - List attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (47): Edit existing link in entity"); + // List> attachmentsPerFacet = new ArrayList<>(); - if (attachments.isEmpty()) { - fail("Could not edit link in facet: " + facetName); - } - attachmentsPerFacet.add(attachments); - } + // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (editLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - int index = 0; - for (String facetName : facet) { - try { - String linkId = attachmentsPerFacet.get(index).get(0); - String updatedUrl = "https://editedexample"; - index++; - String editLinkResponse = - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - System.out.println("response " + editLinkResponse); - fail("Edit link did not throw an error for invalid url 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("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); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - } + // for (String facetName : facet) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facetName); + // } + // } - @Test - @Order(52) - void testEditLinkFailureEmptyURL() throws IOException { - System.out.println("Test (49): Edit existing link with an empty url"); - List> attachmentsPerFacet = new ArrayList<>(); - - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - for (String facetName : facet) { - List attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .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"); + // } - if (attachments.isEmpty()) { - fail("Could not edit link in facet: " + facetName); - } - attachmentsPerFacet.add(attachments); - } + // for (String facetName : facet) { + // List attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - int index = 0; - for (String facetName : facet) { - try { - String linkId = attachmentsPerFacet.get(index).get(0); - String updatedUrl = ""; - index++; - - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - fail("Edit link did not throw an error for empty url 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"); - String expected = "Provide the missing value."; - assertEquals("409008", errorCode); - assertEquals(expected, errorMessage); - } - } - api.deleteEntity(appUrl, entityName, editLinkEntity); - } + // if (attachments.isEmpty()) { + // fail("Could not find link in facet: " + facetName); + // } + // attachmentsPerFacet.add(attachments); + // } - @Test - @Order(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); + // int index = 0; + // for (String facetName : facet) { + // String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); + // } + // index++; + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + + // int verificationIndex = 0; + // for (String facetName : facet) { + // List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); + // for (String attachmentId : attachmentsInFacet) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open edited link " + attachmentId + " in facet: " + facetName); + // } + // } + // verificationIndex++; + // } + // } - Boolean testStatus = false; + // @Test + // @Order(51) + // void testEditLinkFailureInvalidURL() throws IOException { + // System.out.println("Test (48): Edit existing link with invalid url"); + // List> attachmentsPerFacet = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (editLinkEntity.equals("Could not create entity")) { - fail("Could not edit entity"); - } + // for (String facetName : facet) { + // List attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - for (String facetName : facet) { - String linkName = "sampleNoRole_" + facetName; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in facet: " + facetName); - } - } + // if (attachments.isEmpty()) { + // fail("Could not edit link in facet: " + facetName); + // } + // attachmentsPerFacet.add(attachments); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // int index = 0; + // for (String facetName : facet) { + // try { + // String linkId = attachmentsPerFacet.get(index).get(0); + // String updatedUrl = "https://editedexample"; + // index++; + // String editLinkResponse = + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // System.out.println("response " + editLinkResponse); + // fail("Edit link did not throw an error for invalid url 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("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); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // } - String editEntityResponse = - apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // @Test + // @Order(52) + // void testEditLinkFailureEmptyURL() throws IOException { + // System.out.println("Test (49): Edit existing link with an empty url"); + // List> attachmentsPerFacet = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - for (String facetName : facet) { - List attachments = - apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // for (String facetName : facet) { + // List 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 find link in facet: " + facetName); - } + // if (attachments.isEmpty()) { + // fail("Could not edit link in facet: " + facetName); + // } + // attachmentsPerFacet.add(attachments); + // } - String linkId = attachments.get(0); - String updatedUrl = "https://www.editedexample.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; - } - } - api.deleteEntity(appUrl, entityName, editLinkEntity); - if (!testStatus) { - fail("Link got edited without SDM roles"); - } - } + // int index = 0; + // for (String facetName : facet) { + // try { + // String linkId = attachmentsPerFacet.get(index).get(0); + // String updatedUrl = ""; + // index++; + + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // fail("Edit link did not throw an error for empty url 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"); + // String expected = "Provide the missing value."; + // assertEquals("409008", errorCode); + // assertEquals(expected, errorMessage); + // } + // } + // api.deleteEntity(appUrl, entityName, editLinkEntity); + // } - @Test - @Order(54) - void testCopyLinkSuccessNewEntity() throws IOException { - System.out.println("Test (51): Copy link from one entity to another new entity"); - List> attachmentsByFacet = new ArrayList<>(); - String linkUrl = "https://www.example.com"; - for (int i = 0; i < facet.length; i++) { - attachmentsByFacet.add(new ArrayList<>()); - } + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // Boolean testStatus = false; - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entities"); - } + // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (editLinkEntity.equals("Could not create entity")) { + // fail("Could not edit entity"); + // } - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } + // for (String facetName : facet) { + // String linkName = "sampleNoRole_" + facetName; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in facet: " + facetName); + // } + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - sourceObjectIds.clear(); - for (int i = 0; i < facet.length; i++) { - List objectIds = - api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - sourceObjectIds.addAll(objectIds); - } + // String editEntityResponse = + // apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - if (sourceObjectIds.size() != facet.length) { - fail( - "Could not fetch object Ids for all attachments. Expected: " - + facet.length - + ", Found: " - + sourceObjectIds.size()); - } + // for (String facetName : facet) { + // List attachments = + // apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft for facet: " + facetName); - } + // if (attachments.isEmpty()) { + // fail("Could not find link in facet: " + facetName); + // } - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + // String linkId = attachments.get(0); + // String updatedUrl = "https://www.editedexample.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; + // } + // } + // api.deleteEntity(appUrl, entityName, editLinkEntity); + // if (!testStatus) { + // fail("Link got edited without SDM roles"); + // } + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - } + // @Test + // @Order(54) + // void testCopyLinkSuccessNewEntity() throws IOException { + // System.out.println("Test (51): Copy link from one entity to another new entity"); + // List> attachmentsByFacet = new ArrayList<>(); + // String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // attachmentsByFacet.add(new ArrayList<>()); + // } - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity after copying attachments for facet " + facetName); - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - List> attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entities"); + // } - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch in facet " + facetName); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - System.out.println("Attachment type and URL validated for facet " + facetName); - - List attachments = - attachmentsMetadata.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (String attachment : attachments) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open copied link in facet: " + facetName); - } - } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - objectIdIndex++; - } + // sourceObjectIds.clear(); + // for (int i = 0; i < facet.length; i++) { + // List objectIds = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // sourceObjectIds.addAll(objectIds); + // } - String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - if (!deleteTargetResponse.equals("Entity Deleted")) { - fail("Could not delete target entity"); - } - } + // if (sourceObjectIds.size() != facet.length) { + // fail( + // "Could not fetch object Ids for all attachments. Expected: " + // + facet.length + // + ", Found: " + // + sourceObjectIds.size()); + // } - @Test - @Order(55) - void testCopyLinkUnsuccessfulNewEntity() throws IOException { - System.out.println( - "Test (52): Copy invalid type of link from one entity to another new entity"); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft for facet: " + facetName); + // } - if (!editResponse.equals("Entity in draft mode") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not edit source entity or create target entity"); - } + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - sourceObjectIds.add("incorrectObjectId"); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + // } - for (String facetName : facet) { - try { - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error for facet: " + facetName); - } catch (IOException e) { - System.out.println("Successfully caught expected error for facet: " + facetName); - } - } - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - } + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity after copying attachments for facet " + facetName); + // } - @Test - @Order(56) - void testCopyLinkFromNewEntityToExistingEntity() throws IOException { - System.out.println("Test (53): Copy link from a new entity to an existing target entity"); + // List> attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - List> attachmentsByFacet = new ArrayList<>(); - String linkUrl = "https://www.example.com"; - for (int i = 0; i < facet.length; i++) { - attachmentsByFacet.add(new ArrayList<>()); - } + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch in facet " + facetName); - for (int i = 0; i < facet.length; i++) { - String linkName = "newsample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + // System.out.println("Attachment type and URL validated for facet " + facetName); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // List attachments = + // attachmentsMetadata.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - sourceObjectIds.clear(); - for (String facetName : facet) { - List objectIds = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - sourceObjectIds.addAll(objectIds); - } + // for (String attachment : attachments) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open copied link in facet: " + facetName); + // } + // } - if (sourceObjectIds.isEmpty()) { - fail("Could not fetch object Ids for any attachments"); - } + // objectIdIndex++; + // } - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft for facet: " + facetName); - } + // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // if (!deleteTargetResponse.equals("Entity Deleted")) { + // fail("Could not delete target entity"); + // } + // } - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + // @Test + // @Order(55) + // void testCopyLinkUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (52): Copy invalid type of link from one entity to another new entity"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - } + // if (!editResponse.equals("Entity in draft mode") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not edit source entity or create target entity"); + // } - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity after copying attachments for facet " + facetName); - } + // sourceObjectIds.add("incorrectObjectId"); - List> attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // for (String facetName : facet) { + // try { + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error for facet: " + facetName); + // } catch (IOException e) { + // System.out.println("Successfully caught expected error for facet: " + facetName); + // } + // } + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, 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 (53): Copy link from a new entity to an existing target entity"); - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch in facet " + facetName); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - System.out.println("Attachment type and URL validated for facet " + facetName); - - List attachments = - attachmentsMetadata.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (String attachment : attachments) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open copied link in facet: " + facetName); - } - } + // List> attachmentsByFacet = new ArrayList<>(); + // String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // attachmentsByFacet.add(new ArrayList<>()); + // } - objectIdIndex++; - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - } + // for (int i = 0; i < facet.length; i++) { + // String linkName = "newsample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } - @Test - @Order(57) - void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - System.out.println( - "Test (54): Copy invalid type of link from new entity to existing target entity"); - String linkUrl = "https://www.example.com"; + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // sourceObjectIds.clear(); + // for (String facetName : facet) { + // List objectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // sourceObjectIds.addAll(objectIds); + // } - for (int i = 0; i < facet.length; i++) { - String linkName = "newsample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit entities"); - } - for (String facetName : facet) { - List sourceObjectIds = new ArrayList<>(); - sourceObjectIds.add("incorrectObjectId"); - try { - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error for facet: " + facetName); - } catch (IOException e) { - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - } + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Ids for any attachments"); + // } - @Test - @Order(58) - void testCopyLinkSuccessNewEntityDraft() throws IOException { - System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); - List> attachmentsByFacet = new ArrayList<>(); - String linkUrl = "https://www.example.com"; - for (int i = 0; i < facet.length; i++) { - attachmentsByFacet.add(new ArrayList<>()); - } + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft for facet: " + facetName); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entities"); - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + // } - for (int i = 0; i < facet.length; i++) { - String linkName = "sample" + i; - String createLinkResponse = - api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link for facet: " + facet[i]); - } - } + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity after copying attachments for facet " + facetName); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // List> attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - sourceObjectIds.clear(); - for (int i = 0; i < facet.length; i++) { - List objectIds = - api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - sourceObjectIds.addAll(objectIds); - } + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - if (sourceObjectIds.size() != facet.length) { - fail( - "Could not fetch object Ids for all attachments. Expected: " - + facet.length - + ", Found: " - + sourceObjectIds.size()); - } + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch in facet " + facetName); - int objectIdIndex = 0; - for (String facetName : facet) { - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft for facet: " + facetName); - } + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + // System.out.println("Attachment type and URL validated for facet " + facetName); - List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + // List attachments = + // attachmentsMetadata.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - } + // for (String attachment : attachments) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open copied link in facet: " + facetName); + // } + // } - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity after copying attachments for facet " + facetName); - } + // objectIdIndex++; + // } - List> attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // } - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // @Test + // @Order(57) + // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println( + // "Test (54): Copy invalid type of link from new entity to existing target entity"); + // String linkUrl = "https://www.example.com"; - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch in facet " + facetName); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - System.out.println("Attachment type and URL validated for facet " + facetName); - - List attachments = - attachmentsMetadata.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - for (String attachment : attachments) { - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open copied link in facet: " + facetName); - } - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - objectIdIndex++; - } + // for (int i = 0; i < facet.length; i++) { + // String linkName = "newsample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit entities"); + // } + // for (String facetName : facet) { + // List sourceObjectIds = new ArrayList<>(); + // sourceObjectIds.add("incorrectObjectId"); + // try { + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error for facet: " + facetName); + // } catch (IOException e) { + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - } + // @Test + // @Order(58) + // void testCopyLinkSuccessNewEntityDraft() throws IOException { + // System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); + // List> attachmentsByFacet = new ArrayList<>(); + // String linkUrl = "https://www.example.com"; + // for (int i = 0; i < facet.length; i++) { + // attachmentsByFacet.add(new ArrayList<>()); + // } - @Test - @Order(59) - void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { - System.out.println( - "Test (56): Copy attachments from one entity to another new entity draft mode"); - List> attachments = new ArrayList<>(); - for (int i = 0; i < 3; i++) { - attachments.add(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(); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - for (int i = 0; i < facet.length; i++) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - copyAttachmentSourceEntity, - srvpath, - postData, - file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.get(i).add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - } - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (int i = 0; i < attachments.size(); i++) { - for (String attachment : attachments.get(i)) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facet[i], 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 (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entities"); + // } - if (sourceObjectIds.size() == 6) { - String copyResponse; - int i = 0; - for (String facetName : facet) { - if (i != 0) { - String editResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } - } - copyResponse = - api.copyAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - i += 2; - if (copyResponse.equals("Attachments copied successfully")) { - // Fetch copied attachment IDs from target draft - List> copiedMetadataResponse = - api.fetchEntityMetadataDraft( - appUrl, entityName, facetName, copyAttachmentTargetEntity); - List copiedAttachmentIds = - copiedMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (saveEntityResponse.equals("Saved")) { - List> fetchEntityMetadataResponse; - fetchEntityMetadataResponse = - api.fetchEntityMetadataDraft( - 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); - } + // for (int i = 0; i < facet.length; i++) { + // String linkName = "sample" + i; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link for facet: " + facet[i]); + // } + // } - @Test - @Order(60) - void testViewChangelogForNewlyCreatedAttachment() throws IOException { - System.out.println( - "Test (60): View changelog for newly created attachment in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // Create a new entity for changelog test - changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); - assertNotEquals("Could not create entity", changelogEntityID[i]); - - // 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"); - - // Create attachment - Map postData = new HashMap<>(); - postData.put("up__ID", changelogEntityID[i]); - postData.put("mimeType", "text/plain"); - 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[i], srvpath, postData, file); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - changelogAttachmentID[i] = createResponse.get(1); + // sourceObjectIds.clear(); + // for (int i = 0; i < facet.length; i++) { + // List objectIds = + // api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], + // copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // sourceObjectIds.addAll(objectIds); + // } - assertEquals("Attachment created", status, "Attachment should be created successfully"); - assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); - assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); + // if (sourceObjectIds.size() != facet.length) { + // fail( + // "Could not fetch object Ids for all attachments. Expected: " + // + facet.length + // + ", Found: " + // + sourceObjectIds.size()); + // } - // Fetch changelog for the newly created attachment - Map changelogResponse = - api.fetchChangelog( - appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + // int objectIdIndex = 0; + // for (String facetName : facet) { + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft for facet: " + facetName); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // 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"); - } - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + // } - @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 in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // 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[i], - changelogAttachmentID[i], - 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[i], - changelogAttachmentID[i], - bodyInt); - assertEquals( - "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity after copying attachments for facet " + facetName); + // } - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // List> attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // Edit entity again to fetch changelog - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // Fetch changelog after modifications - Map changelogResponse = - api.fetchChangelog( - appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch in facet " + facetName); - assertNotNull(changelogResponse, "Changelog response should not be null"); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + // System.out.println("Attachment type and URL validated for facet " + facetName); - // 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)"); + // List attachments = + // attachmentsMetadata.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); + // for (String attachment : attachments) { + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open copied link in facet: " + facetName); + // } + // } - // 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)"); - - // Save the entity so test 62 can edit it - String saveResponseFinal = - api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); - } - } + // objectIdIndex++; + // } - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println( - "Test (62): Rename attachment and verify changelog increases with rename entry in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // Edit entity to put it in draft mode (entity was saved at end of test 61) - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - 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[i], - changelogAttachmentID[i], - newFileName); - assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); - - // Save entity after rename - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - - // Edit entity again and fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // Fetch changelog after rename - Map changelogAfterRename = - api.fetchChangelog( - appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - 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"); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // } - @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[i]); - } - } + // @Test + // @Order(59) + // void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { + // System.out.println( + // "Test (56): Copy attachments from one entity to another new entity draft mode"); + // List> attachments = new ArrayList<>(); + // for (int i = 0; i < 3; i++) { + // attachments.add(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 in all three facets"); - - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; - - // 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.pdf").getFile()); - assertTrue(file.exists(), "Sample file should exist"); - - // 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, 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"); + // sourceObjectIds.clear(); + + // for (int i = 0; i < facet.length; i++) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // copyAttachmentSourceEntity, + // srvpath, + // postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.get(i).add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // } + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (int i = 0; i < attachments.size(); i++) { + // for (String attachment : attachments.get(i)) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facet[i], 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 the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // if (sourceObjectIds.size() == 6) { + // String copyResponse; + // int i = 0; + // for (String facetName : facet) { + // if (i != 0) { + // String editResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } + // } + // copyResponse = + // api.copyAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + // i += 2; + // if (copyResponse.equals("Attachments copied successfully")) { + // // Fetch copied attachment IDs from target draft + // List> copiedMetadataResponse = + // api.fetchEntityMetadataDraft( + // appUrl, entityName, facetName, copyAttachmentTargetEntity); + // List copiedAttachmentIds = + // copiedMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (saveEntityResponse.equals("Saved")) { + // List> fetchEntityMetadataResponse; + // fetchEntityMetadataResponse = + // api.fetchEntityMetadataDraft( + // 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); + // } - // 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"); + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println( + // "Test (60): View changelog for newly created attachment in all three facets"); - // Fetch changelog after initial save - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; - assertNotNull(changelogResponse, "Changelog response should not be null"); + // // Create a new entity for changelog test + // changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); + // assertNotEquals("Could not create entity", changelogEntityID[i]); - // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + - // customProperty2) - assertEquals( - 3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); + // // 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"); - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); + // // Create attachment + // Map postData = new HashMap<>(); + // postData.put("up__ID", changelogEntityID[i]); + // 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 any modifications - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, changelogEntityID[i], srvpath, postData, file); + + // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + // String status = createResponse.get(0); + // changelogAttachmentID[i] = createResponse.get(1); + + // assertEquals("Attachment created", status, "Attachment should be created successfully"); + // assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); + // assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); + + // // Fetch changelog for the newly created attachment + // Map changelogResponse = + // api.fetchChangelog( + // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + // 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 again and 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 in all three facets"); + + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; + + // // 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[i], + // changelogAttachmentID[i], + // 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[i], + // changelogAttachmentID[i], + // bodyInt); + // assertEquals( + // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + + // // Save the entity + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + + // // Edit entity again to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after modifications + // Map changelogResponse = + // api.fetchChangelog( + // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + // 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)"); + + // // Save the entity so test 62 can edit it + // String saveResponseFinal = + // api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + // assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); + // } + // } - // Fetch changelog after second save - Map changelogAfterSecondSave = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // @Test + // @Order(62) + // void testChangelogAfterRenamingAttachment() throws IOException { + // System.out.println( + // "Test (62): Rename attachment and verify changelog increases with rename entry in all + // three facets"); + + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; + + // // Edit entity to put it in draft mode (entity was saved at end of test 61) + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // 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[i], + // changelogAttachmentID[i], + // newFileName); + // assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); + + // // Save entity after rename + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID[i]); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + + // // Edit entity again and fetch changelog + // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after rename + // Map changelogAfterRename = + // api.fetchChangelog( + // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + // 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[i]); + // } + // } - assertNotNull( - changelogAfterSecondSave, "Changelog response should not be null after second save"); + // @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 in all three facets"); - // 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"); + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; - @SuppressWarnings("unchecked") - List> changeLogsAfterSecondSave = - (List>) changelogAfterSecondSave.get("changeLogs"); - assertEquals( - 3, - changeLogsAfterSecondSave.size(), - "Should still have exactly 3 changelog entries after second save"); + // // 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); - // Clean up the entity - api.deleteEntity(appUrl, entityName, newEntityID); - } - } + // // 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(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 in all three facets"); + // // 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"); - for (int i = 0; i < 3; i++) { - String facetName = facet[i]; + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); - // 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); + // 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); + // } + // } - // 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(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 in all three facets"); - // 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"); + // for (int i = 0; i < 3; i++) { + // String facetName = facet[i]; - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // // 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); - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - String newAttachmentID = createResponse.get(1); + // // 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"); - 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 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"); - // Save the entity immediately without any modifications - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); - // 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"); + // 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); + // } + // } - // Save entity again without modifying the attachment - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // @Test + // @Order(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println( + // "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // Edit entity to fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Fetch changelog for the attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); + // 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())); - 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 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); - } - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println( - "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } - 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } + // // Save target before move + // String saveTargetBeforeMoveTest65 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveTest65.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all attachments after move"); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after + // move"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - // Save target before move - String saveTargetBeforeMoveTest65 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveTest65.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); - } + // @Test + // @Order(66) + // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + // System.out.println( + // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - sourceAttachmentIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all attachments after move"); + // 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> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, sourceMetadataAfterMove.size(), "Source entity should have no 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"); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } + // 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"); + + // File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + // List targetCreateResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // srvpath, + // targetPostData, + // duplicateFile); + + // if (!targetCreateResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment on target entity"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - 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"); + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + + // int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target should have duplicate skipped, other attachments moved"); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // int expectedSourceCount = + // sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); + // assertEquals( + // expectedSourceCount, + // sourceMetadataAfterMove.size(), + // "Source should have duplicate attachment remaining"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - List targetCreateResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - srvpath, - targetPostData, - duplicateFile); - - if (!targetCreateResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment on target entity"); - } + // @Test + // @Order(67) + // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + // System.out.println( + // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // 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> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // 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"); - int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target should have duplicate skipped, other attachments moved"); - - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - int expectedSourceCount = - sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); - assertEquals( - expectedSourceCount, - sourceMetadataAfterMove.size(), - "Source should have duplicate attachment remaining"); + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update notes 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"); + // Integer customProperty2Value = 54321; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + // if (!updateCustomPropertyResponse.equals("Updated")) { + // fail("Could not update custom property for attachment: " + attachmentId); + // } + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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); + // } + // } - 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update notes for attachment: " + attachmentId); - } - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - Integer customProperty2Value = 54321; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - - for (String attachmentId : sourceAttachmentIds) { - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); - if (!updateCustomPropertyResponse.equals("Updated")) { - fail("Could not update custom property for attachment: " + attachmentId); - } - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Save target before move + // String saveTargetBeforeMoveTest67 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveTest67.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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); - } - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all attachments after move"); + + // 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, facet[i], moveTargetEntity, + // targetAttachmentId); + + // 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); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // 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 target before move - String saveTargetBeforeMoveTest67 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveTest67.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); - } + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - sourceAttachmentIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all attachments after move"); - - 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, facet[i], moveTargetEntity, targetAttachmentId); - - 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); - } + // @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 (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); - } - } + // for (int i = 0; i < facet.length; i++) { + // 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, facet[i], moveSourceEntity); - assertEquals( - 0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); + // 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())); - 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(68) - public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - System.out.println( - "Test (68): Move valid attachments from Source Entity to Target Entity without sourceFacet"); + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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 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"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - 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()); - } - } + // 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 before move"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // 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"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // moveObjectIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all moved attachments"); + + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // String readResponse = + // api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read moved attachment from target entity"); + // } + // } - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // moveObjectIds.size(), + // sourceMetadataAfterMove.size(), + // "Source entity should still have attachments in UI when sourceFacet is not specified"); + + // for (Map metadata : sourceMetadataAfterMove) { + // String objectId = (String) metadata.get("objectId"); + // assertTrue( + // moveObjectIds.contains(objectId), + // "Source entity should still show attachment with objectId: " + objectId); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - moveObjectIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all moved attachments"); - - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - String readResponse = - api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read moved attachment from target entity"); - } - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - moveObjectIds.size(), - sourceMetadataAfterMove.size(), - "Source entity should still have attachments in UI when sourceFacet is not specified"); - - 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(69) + // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (69): Move attachments into existing Target Entity when duplicate exists without + // sourceFacet"); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // for (int i = 0; i < facet.length; i++) { + // 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"); + // 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())); - for (int i = 0; i < facet.length; i++) { - 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"); - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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 sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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 saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - 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()); - } - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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()); + // } + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target 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"); + // 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"); + + // List createTargetResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // srvpath, + // targetPostData, + // files.get(0)); + // if (!createTargetResponse.get(0).equals("Attachment created")) { + // fail("Could not create duplicate attachment in target entity"); + // } - List createTargetResponse = - api.createAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - srvpath, - targetPostData, - files.get(0)); - if (!createTargetResponse.get(0).equals("Attachment created")) { - fail("Could not create duplicate attachment in target entity"); - } + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int initialTargetCount = targetMetadataBeforeMove.size(); + + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int initialTargetCount = targetMetadataBeforeMove.size(); - - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int nonDuplicateCount = moveObjectIds.size() - 1; + // int expectedTargetCount = initialTargetCount + nonDuplicateCount; - int nonDuplicateCount = moveObjectIds.size() - 1; - int expectedTargetCount = initialTargetCount + nonDuplicateCount; + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target entity should have initial attachments plus non-duplicate moved attachments"); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have initial attachments plus non-duplicate moved attachments"); + // assertTrue( + // targetMetadataAfterMove.size() > initialTargetCount, + // "Target should have more attachments after move (non-duplicates added)"); - assertTrue( - targetMetadataAfterMove.size() > initialTargetCount, - "Target should have more attachments after move (non-duplicates added)"); + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // moveObjectIds.size(), + // sourceMetadataAfterMove.size(), + // "Source entity should still have all attachments in UI when sourceFacet is not + // specified"); - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - moveObjectIds.size(), - sourceMetadataAfterMove.size(), - "Source entity should still have all attachments in UI when sourceFacet is not specified"); + // 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); + // } - 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); - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // @Test + // @Order(70) + // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + // throws Exception { + // System.out.println( + // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - @Test - @Order(70) - public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - throws Exception { - System.out.println( - "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // 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())); - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update notes for attachment: " + attachmentId); - } - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - Integer customProperty2Value = 54321; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - - for (String attachmentId : sourceAttachmentIds) { - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); - if (!updateCustomPropertyResponse.equals("Updated")) { - fail("Could not update custom property for attachment: " + attachmentId); - } - } + // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update notes for attachment: " + attachmentId); + // } + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // Integer customProperty2Value = 54321; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + // if (!updateCustomPropertyResponse.equals("Updated")) { + // fail("Could not update custom property for attachment: " + attachmentId); + // } + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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); - } - } + // 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"); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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); + // } + // } - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - 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"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have " + expectedTargetCount + " attachments after move"); - - 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, facet[i], moveTargetEntity, targetAttachmentId); - - 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); - } + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - 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); - } - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target entity should have " + expectedTargetCount + " attachments after move"); + + // 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, facet[i], moveTargetEntity, + // targetAttachmentId); + + // 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); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - sourceCountBeforeMove, - sourceMetadataAfterMove.size(), - "Source entity should still have " - + sourceCountBeforeMove - + " attachments (without sourceFacet)"); - - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // 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"); + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // sourceCountBeforeMove, + // sourceMetadataAfterMove.size(), + // "Source entity should still have " + // + sourceCountBeforeMove + // + " attachments (without sourceFacet)"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - for (int i = 0; i < facet.length; i++) { - 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"); - 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - } - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - String validAttachmentId = sourceAttachmentIds.get(0); - Integer validCustomProperty2Value = 12345; - RequestBody validPropertyBody = - RequestBody.create( - "{\"customProperty2\": " + validCustomProperty2Value + "}", - MediaType.parse("application/json")); - - String validPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, validPropertyBody); - if (!validPropertyResponse.equals("Updated")) { - fail("Could not update valid property for attachment: " + validAttachmentId); - } + // 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())); - String invalidAttachmentId = sourceAttachmentIds.get(1); - RequestBody invalidPropertyBody = - RequestBody.create( - "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); - - api.updateSecondaryProperty( - appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, invalidPropertyBody); - - String undefinedAttachmentId = sourceAttachmentIds.get(2); - RequestBody undefinedPropertyBody = - RequestBody.create( - "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", - MediaType.parse("application/json")); - - api.updateSecondaryProperty( - appUrl, - entityName, - facet[i], - moveSourceEntity, - undefinedAttachmentId, - undefinedPropertyBody); - - 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"); - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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); - } - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); - } + // String validAttachmentId = sourceAttachmentIds.get(0); + // Integer validCustomProperty2Value = 12345; + // RequestBody validPropertyBody = + // RequestBody.create( + // "{\"customProperty2\": " + validCustomProperty2Value + "}", + // MediaType.parse("application/json")); + + // String validPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, + // validPropertyBody); + // if (!validPropertyResponse.equals("Updated")) { + // fail("Could not update valid property for attachment: " + validAttachmentId); + // } - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // String invalidAttachmentId = sourceAttachmentIds.get(1); + // RequestBody invalidPropertyBody = + // RequestBody.create( + // "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); + + // api.updateSecondaryProperty( + // appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, + // invalidPropertyBody); + + // String undefinedAttachmentId = sourceAttachmentIds.get(2); + // RequestBody undefinedPropertyBody = + // RequestBody.create( + // "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", + // MediaType.parse("application/json")); + + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facet[i], + // moveSourceEntity, + // undefinedAttachmentId, + // undefinedPropertyBody); + + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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 target before move - String saveTargetBeforeMoveResponseTest72 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - assertTrue( - targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - assertEquals( - sourceCountBeforeMove, - targetMetadataAfterMove.size(), - "All attachments should move (invalid properties are ignored)"); - - 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, facet[i], moveTargetEntity, targetAttachmentId); - - if (detailedMetadata.containsKey("customProperty2") - && detailedMetadata.get("customProperty2") != null) { - assertEquals( - validCustomProperty2Value, - detailedMetadata.get("customProperty2"), - "Valid customProperty2 should be preserved"); - } - } + // // Save target before move + // String saveTargetBeforeMoveResponseTest72 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterMove.size(), - "Source entity should have no attachments after move with sourceFacet"); + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + + // assertTrue( + // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after + // move"); + // assertEquals( + // sourceCountBeforeMove, + // targetMetadataAfterMove.size(), + // "All attachments should move (invalid properties are ignored)"); + + // 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, facet[i], moveTargetEntity, + // targetAttachmentId); + + // if (detailedMetadata.containsKey("customProperty2") + // && detailedMetadata.get("customProperty2") != null) { + // assertEquals( + // validCustomProperty2Value, + // detailedMetadata.get("customProperty2"), + // "Valid customProperty2 should be preserved"); + // } + // } - @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"); + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterMove.size(), + // "Source entity should have no attachments after move with sourceFacet"); - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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"); - 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); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // 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"); - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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 editSourceResponse = - api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!editSourceResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity back to draft mode"); - } + // 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); + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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 saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertTrue( - targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - assertEquals( - sourceCountBeforeMove, - targetMetadataAfterMove.size(), - "Target should have " + sourceCountBeforeMove + " attachments after move"); - - 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()); - } + // 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"); + // } - String saveSourceAfterMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceAfterMoveResponse.equals("Saved")) { - fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); - } + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], 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 target before move + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - @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"); + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertTrue( + // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after + // move"); + // assertEquals( + // sourceCountBeforeMove, + // targetMetadataAfterMove.size(), + // "Target should have " + sourceCountBeforeMove + " attachments after move"); + + // 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()); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // String saveSourceAfterMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceAfterMoveResponse.equals("Saved")) { + // fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); + // } - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], 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()); + // } - 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"); + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in source entity"); - } + // @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"); - String attachmentId = createResponse.get(1); - assertNotNull(attachmentId, "Attachment ID should not be null"); + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - List> metadataBeforeRename = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); - assertEquals( - "sample.txt", - metadataBeforeRename.get(0).get("fileName"), - "Original filename should be sample.txt"); - - String editSourceResponse = - api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!editSourceResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity to draft mode"); - } + // 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"); - String newFileName = "testEdited.txt"; - String renameResponse = - api.renameAttachment( - appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); - assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in source entity"); + // } - saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after rename: " + saveSourceResponse); - } + // String attachmentId = createResponse.get(1); + // assertNotNull(attachmentId, "Attachment ID should not be null"); - List> metadataAfterRename = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); - assertEquals( - newFileName, - metadataAfterRename.get(0).get("fileName"), - "Filename should be updated to " + newFileName); - - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); - moveObjectIds.add(objectId); - - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Save target before move - String saveTargetBeforeMoveResponseTest73 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); - } + // List> metadataBeforeRename = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); + // assertEquals( + // "sample.txt", + // metadataBeforeRename.get(0).get("fileName"), + // "Original filename should be sample.txt"); + + // String editSourceResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!editSourceResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity to draft mode"); + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // String newFileName = "testEdited.txt"; + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); + // assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); + // saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after rename: " + saveSourceResponse); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterMove.size(), - "Source entity should have no attachments after move with sourceFacet"); + // List> metadataAfterRename = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); + // assertEquals( + // newFileName, + // metadataAfterRename.get(0).get("fileName"), + // "Filename should be updated to " + newFileName); + + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); + // moveObjectIds.add(objectId); + + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // // Save target before move + // String saveTargetBeforeMoveResponseTest73 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); + // } - @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"); + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterMove.size(), + // "Source entity should have no attachments after move with sourceFacet"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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(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"); - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // for (int i = 0; i < facet.length; i++) { + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - int sourceCountInitial = sourceAttachmentIds.size(); - assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // 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"); - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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"); + // } + // } - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity 1"); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // 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"); - } + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult1 = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult1 == null) { - fail("Move operation from source to target 1 returned null result"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - List> target1MetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertTrue( - target1MetadataAfterMove.size() > 0, - "Target entity 1 should have attachments after move"); - assertEquals( - sourceCountInitial, - target1MetadataAfterMove.size(), - "Target 1 should have " + sourceCountInitial + " attachments"); - - 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()); - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - List> sourceMetadataAfterFirstMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterFirstMove.size(), - "Source entity should have no attachments after move to target 1"); + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity 1"); + // } - String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity2.equals("Could not create entity")) { - fail("Could not create target entity 2"); - } + // // 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"); + // } - // 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"); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult1 = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult1 == null) { + // fail("Move operation from source to target 1 returned null result"); + // } - List target1AttachmentIds = new ArrayList<>(); - for (Map metadata : target1MetadataAfterMove) { - String attachmentId = metadata.get("ID").toString(); - target1AttachmentIds.add(attachmentId); - } + // List> target1MetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertTrue( + // target1MetadataAfterMove.size() > 0, + // "Target entity 1 should have attachments after move"); + // assertEquals( + // sourceCountInitial, + // target1MetadataAfterMove.size(), + // "Target 1 should have " + sourceCountInitial + " attachments"); + + // 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()); + // } - moveObjectIds = new ArrayList<>(); - String target1FolderId = null; - for (String attachmentId : target1AttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - 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()); - } - } + // List> sourceMetadataAfterFirstMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterFirstMove.size(), + // "Source entity should have no attachments after move to target 1"); - assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); - - Map moveResult2 = - api.moveAttachment( - appUrl, - entityName, - facet[i], - moveTargetEntity2, - target1FolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult2 == null) { - fail("Move operation from target 1 to target 2 returned null result"); - } + // String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity2.equals("Could not create entity")) { + // fail("Could not create target entity 2"); + // } - List> target2MetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); - assertTrue( - target2MetadataAfterMove.size() > 0, - "Target entity 2 should have attachments after move"); - assertEquals( - sourceCountInitial, - target2MetadataAfterMove.size(), - "Target 2 should have " + sourceCountInitial + " attachments"); - - 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()); - } + // // 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"); + // } - List> target1MetadataAfterSecondMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - 0, - target1MetadataAfterSecondMove.size(), - "Target entity 1 should have no attachments after move to target 2"); + // List target1AttachmentIds = new ArrayList<>(); + // for (Map metadata : target1MetadataAfterMove) { + // String attachmentId = metadata.get("ID").toString(); + // target1AttachmentIds.add(attachmentId); + // } - api.deleteEntity(appUrl, entityName, moveTargetEntity2); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // moveObjectIds = new ArrayList<>(); + // String target1FolderId = null; + // for (String attachmentId : target1AttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // 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()); + // } + // } - @Test - @Order(75) - public void testMoveAttachmentsWithoutSDMRole() throws Exception { - System.out.println("Test (75): Move attachments when user does not have SDM Role"); + // assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); + + // Map moveResult2 = + // api.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // moveTargetEntity2, + // target1FolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult2 == null) { + // fail("Move operation from target 1 to target 2 returned null result"); + // } - for (int i = 0; i < facet.length; i++) { - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // List> target2MetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); + // assertTrue( + // target2MetadataAfterMove.size() > 0, + // "Target entity 2 should have attachments after move"); + // assertEquals( + // sourceCountInitial, + // target2MetadataAfterMove.size(), + // "Target 2 should have " + sourceCountInitial + " attachments"); + + // 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()); + // } - 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 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"); - - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facet[i], 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> target1MetadataAfterSecondMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // 0, + // target1MetadataAfterSecondMove.size(), + // "Target entity 1 should have no attachments after move to target 2"); - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // api.deleteEntity(appUrl, entityName, moveTargetEntity2); + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - int sourceCountInitial = sourceAttachmentIds.size(); - assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - moveObjectIds = new ArrayList<>(); - moveSourceFolderId = null; - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // @Test + // @Order(75) + // public void testMoveAttachmentsWithoutSDMRole() throws Exception { + // System.out.println("Test (75): Move attachments when user does not have SDM Role"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // for (int i = 0; i < facet.length; i++) { + // 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"); + // 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())); - moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity with no SDM role"); - } + // 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 before move - String saveTargetBeforeMoveResponse = - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facet[i], 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 sourceFacet = serviceName + "." + entityName + "." + facet[i]; - String targetFacet = serviceName + "." + entityName + "." + facet[i]; - Map moveResult = null; - boolean moveOperationFailed = false; - String errorMessage = null; - - try { - moveResult = - apiNoRoles.moveAttachment( - appUrl, - entityName, - facet[i], - 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(); - } + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - 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); + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + // moveObjectIds = new ArrayList<>(); + // moveSourceFolderId = null; + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - assertEquals( - sourceCountInitial, - sourceMetadataAfterMove.size(), - "Source should still have all attachments after failed move"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - assertEquals( - 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed move"); + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } - } + // moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity with no SDM role"); + // } - @Test - @Order(76) - void testRenameAttachmentWithExtensionChange() throws IOException { - System.out.println( - "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); + // // Save target before move + // String saveTargetBeforeMoveResponse = + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - // Step 1: Create a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (newEntityID.equals("Could not create entity")) { - fail("Could not create entity"); - } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!saveResponse.equals("Saved")) { - fail("Could not save new entity: " + saveResponse); - } + // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + // String targetFacet = serviceName + "." + entityName + "." + facet[i]; + // Map moveResult = null; + // boolean moveOperationFailed = false; + // String errorMessage = null; + + // try { + // moveResult = + // apiNoRoles.moveAttachment( + // appUrl, + // entityName, + // facet[i], + // 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 2: Upload a PDF attachment to each facet - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // 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); + + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + // assertEquals( + // sourceCountInitial, + // sourceMetadataAfterMove.size(), + // "Source should still have all attachments after failed move"); + + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + // assertEquals( + // 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed + // move"); + + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } + // } - 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"); + // @Test + // @Order(76) + // void testRenameAttachmentWithExtensionChange() throws IOException { + // System.out.println( + // "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - + // should return extension change warning"); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!"Entity in draft mode".equals(editResponse)) { - fail("Could not put entity in draft mode for PDF upload"); - } + // // Step 1: Create a new entity + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (newEntityID.equals("Could not create entity")) { + // fail("Could not create entity"); + // } + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save new entity: " + saveResponse); + // } - String[] facetAttachmentIDs = new String[facet.length]; - for (int i = 0; i < facet.length; i++) { - facetAttachmentIDs[i] = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - if (facetAttachmentIDs[i] == null) { - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not upload sample.pdf to facet: " + facet[i]); - } - } + // // Step 2: Upload a PDF attachment to each facet + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // 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"); + + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!"Entity in draft mode".equals(editResponse)) { + // fail("Could not put entity in draft mode for PDF upload"); + // } - // Step 3: Save the entity - String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!savedAfterUpload.equals("Saved")) { - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not save entity after PDF upload: " + savedAfterUpload); - } + // String[] facetAttachmentIDs = new String[facet.length]; + // for (int i = 0; i < facet.length; i++) { + // facetAttachmentIDs[i] = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + // if (facetAttachmentIDs[i] == null) { + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not upload sample.pdf to facet: " + facet[i]); + // } + // } - // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> .txt - for (int i = 0; i < facet.length; i++) { - String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!"Entity in draft mode".equals(editDraftResponse)) { - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not put entity in draft mode for rename on facet: " + facet[i]); - } + // // Step 3: Save the entity + // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!savedAfterUpload.equals("Saved")) { + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not save entity after PDF upload: " + savedAfterUpload); + // } - String renameResponse = - api.renameAttachment( - appUrl, - entityName, - facet[i], - newEntityID, - facetAttachmentIDs[i], - "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - } + // // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> + // .txt + // for (int i = 0; i < facet.length; i++) { + // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!"Entity in draft mode".equals(editDraftResponse)) { + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not put entity in draft mode for rename on facet: " + facet[i]); + // } - // Step 6: Save and validate the extension change warning message - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + // String renameResponse = + // api.renameAttachment( + // appUrl, + // entityName, + // facet[i], + // newEntityID, + // facetAttachmentIDs[i], + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + // } - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + // // Step 6: Save and validate the extension change warning message + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); + // } - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - } + // // Clean up + // api.deleteEntity(appUrl, entityName, newEntityID); + // } - // Clean up - api.deleteEntity(appUrl, entityName, newEntityID); - } + // @Test + // @Order(77) + // void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { + // System.out.println( + // "Test (77) : Upload attachment in draft, rename changing extension before save across all + // facets - should return extension change warning"); - @Test - @Order(77) - void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { - System.out.println( - "Test (77) : Upload attachment in draft, rename changing extension before save across all facets - should return extension change warning"); + // for (int i = 0; i < facet.length; i++) { + // // Step 1: Create a new entity draft (do NOT save it yet) + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (newEntityID.equals("Could not create entity")) { + // fail("Could not create entity for facet: " + facet[i]); + // } - for (int i = 0; i < facet.length; i++) { - // Step 1: Create a new entity draft (do NOT save it yet) - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (newEntityID.equals("Could not create entity")) { - fail("Could not create entity for facet: " + facet[i]); - } + // // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - 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"); - - String facetAttachmentID = - CreateandReturnFacetID( - appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - if (facetAttachmentID == null) { - api.deleteEntityDraft(appUrl, entityName, newEntityID); - fail("Could not upload sample.pdf to facet: " + facet[i]); - } + // 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"); - // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not saved - String renameResponse = - api.renameAttachment( - appUrl, entityName, facet[i], newEntityID, facetAttachmentID, "renamed_document.txt"); - if (!"Renamed".equals(renameResponse)) { - api.deleteEntityDraft(appUrl, entityName, newEntityID); - fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - } + // String facetAttachmentID = + // CreateandReturnFacetID( + // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + // if (facetAttachmentID == null) { + // api.deleteEntityDraft(appUrl, entityName, newEntityID); + // fail("Could not upload sample.pdf to facet: " + facet[i]); + // } - // Step 4: Save — should receive extension change warning, not "Saved" - String saveWithWarningResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + // // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not + // saved + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facet[i], newEntityID, facetAttachmentID, + // "renamed_document.txt"); + // if (!"Renamed".equals(renameResponse)) { + // api.deleteEntityDraft(appUrl, entityName, newEntityID); + // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + // } - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + // // Step 4: Save — should receive extension change warning, not "Saved" + // String saveWithWarningResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + + // facet[i]); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue( + // messagesNode.isArray(), + // "sap-messages response should be a JSON array for facet: " + facet[i]); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match for facet: " + facet[i]); + // break; + // } + // } + // } - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue( - messagesNode.isArray(), - "sap-messages response should be a JSON array for facet: " + facet[i]); - - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match for facet: " + facet[i]); - break; - } - } - } + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found for facet: " + // + facet[i] + // + ". Full response: " + // + saveWithWarningResponse); - assertTrue( - foundExtensionError, - "Expected extension change warning not found for facet: " - + facet[i] - + ". Full response: " - + saveWithWarningResponse); - - // Clean up - api.deleteEntity(appUrl, entityName, newEntityID); - } - } + // // Clean up + // api.deleteEntity(appUrl, entityName, newEntityID); + // } + // } @Test @Order(78) @@ -7491,7 +7704,7 @@ void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOExcepti // fail("Failed to parse error response for references facet: " + e.getMessage()); // } // } else { - // fail("Attachment got created in references facet with file size exceeding maximum // + // fail("Attachment got created in references facet with file size exceeding maximum // limit"); // } // } else { 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 444a63841..482340853 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 @@ -2,18 +2,14 @@ import static org.junit.jupiter.api.Assertions.*; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; 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.*; @@ -392,6073 +388,6237 @@ void testUploadAttachmentWithoutSDMRole() throws IOException { } } - @Test - @Order(7) - void testUploadSingleAttachmentPDFDuplicate() throws IOException { - System.out.println("Test (7) : Upload duplicate pdf"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Boolean testStatus = false; - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID); - 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, entityID); - if (response == "Entity in draft mode") { - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - testStatus = false; - } else { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - String expectedJson = - "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" already exists. Rename the object and try again.\"}}"; - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode actualJsonNode = objectMapper.readTree(check); - JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); - if (expectedJsonNode.equals(actualJsonNode)) { - testStatus = true; - } - } - } - } - if (!testStatus) { - fail("Attachment created"); - } - } - - @Test - @Order(8) - void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { - System.out.println("Test (8) : Upload duplicate pdf in different entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - entityID2 = response; - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if (response == "Saved") { - response = api.checkEntity(appUrl, entityName, entityID2); - if (response.equals("Entity exists")) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Could not create entity"); - } - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID2); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - if (response == "Entity in draft mode") { - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, file); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - attachmentID4 = createResponse.get(1); - response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, attachmentID4); - if (response.equals("OK")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if (response.equals("Saved")) { - response = api.readAttachment(appUrl, entityName, facetName, entityID2, attachmentID4); - - if (response.equals("OK")) { - testStatus = true; - } - } - } - } - } - if (!testStatus) { - fail("Could not upload sample.pdf " + response); - } - } - - @Test - @Order(9) - void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { - System.out.println("Test (9): Create attachment with restricted character in filename"); - - 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", entityID); - 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, entityID); - if (response.equals("Entity in draft mode")) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - attachmentID6 = createResponse.get(1); - - String restrictedFilename = "a/\\bc.pdf"; - response = - api.renameAttachment( - appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); - - if (response.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - api.renameAttachment( - appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if ("Saved".equals(response)) testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - } - if (!testStatus) { - fail("Attachment created with restricted character in filename"); - } - } - - @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); - - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } - - // Store objectId in array - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.isEmpty()) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(0, sourceObjectId); - } - - 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); - } - - // Create target entity - copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } - - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array - - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } - - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } - - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } - - // 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); - } - - // 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 (readResponse.equals("OK")) { - testStatus = true; - } - if (!testStatus) { - fail("Could not verify that notes field was copied from source to target attachment"); - } - } - - @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(7) + // void testUploadSingleAttachmentPDFDuplicate() throws IOException { + // System.out.println("Test (7) : Upload duplicate pdf"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Boolean testStatus = false; - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample1.pdf").getFile()); + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response == "Entity in draft mode") { + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // testStatus = false; + // } else { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // String expectedJson = + // "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" + // already exists. Rename the object and try again.\"}}"; + // ObjectMapper objectMapper = new ObjectMapper(); + // JsonNode actualJsonNode = objectMapper.readTree(check); + // JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); + // if (expectedJsonNode.equals(actualJsonNode)) { + // testStatus = true; + // } + // } + // } + // } + // if (!testStatus) { + // fail("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"); + // @Test + // @Order(8) + // void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { + // System.out.println("Test (8) : Upload duplicate pdf in different entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + // entityID2 = response; + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if (response == "Saved") { + // response = api.checkEntity(appUrl, entityName, entityID2); + // if (response.equals("Entity exists")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Could not create entity"); + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID2); + // 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); - } + // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + // if (response == "Entity in draft mode") { + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, + // file); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // attachmentID4 = createResponse.get(1); + // response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, + // attachmentID4); + // if (response.equals("OK")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if (response.equals("Saved")) { + // response = api.readAttachment(appUrl, entityName, facetName, entityID2, + // attachmentID4); + + // if (response.equals("OK")) { + // testStatus = true; + // } + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not upload sample.pdf " + response); + // } + // } - // 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(9) + // void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { + // System.out.println("Test (9): Create attachment with restricted character in filename"); - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity. Response: " + saveSourceResponse); - } + // boolean testStatus = false; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // Fetch attachment metadata to get objectId and verify secondary properties - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // 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 response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Entity in draft mode")) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // attachmentID6 = createResponse.get(1); + + // String restrictedFilename = "a/\\bc.pdf"; + // response = + // api.renameAttachment( + // appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); + + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // api.renameAttachment( + // appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if ("Saved".equals(response)) testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // } + // if (!testStatus) { + // fail("Attachment created with restricted character in filename"); + // } + // } - // 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); - } + // @Test + // @Order(10) + // void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { + // System.out.println("Test (10): Upload attachments, delete one and create entity"); - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment. Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } + // 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); + // } - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target 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); - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Failed to create entity after deleting one attachment"); + // } + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // @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()); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // 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"); - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // 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); + // } - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in 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"); + // } + // } - // 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); + // @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 (copiedAttachmentMetadata == null) { - fail("Could not find the copied attachment with file 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"); + // } + // } - 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); - } + // @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 customProperty2 - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied. Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // @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); + // } + // } - // 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 (!testStatus) { + // fail("Multiple renames should have failed due to one unsupported characters"); + // } + // } - 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(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(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; + // @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 editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // @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"); + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // @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"); + // } + // } - 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(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"); + // } + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // @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 (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } + // @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"); + // } + // } - String sourceAttachmentId = createResponse.get(1); + // @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"); + // } + // } - // 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); + // @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"); + // } + // } - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateNotesBody); + // @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 (!updateNotesResponse.equals("Updated")) { - fail("Could not update attachment notes field"); - } + // @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"); + // } - // 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); - } + // 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"); + // } - // 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); - } + // 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"); + // } - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity. Response: " + saveSourceResponse); - } + // 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"); + // } + // } - // Fetch attachment metadata to get objectId and verify notes and secondary properties - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // @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; + // } - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // 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 sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.size() < 3) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(2, sourceObjectId); - } + // 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; + // } - 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 (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"); + // } + // } - 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); - } + // @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"); + // } - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment. Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } + // 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"); + // } - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit 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"); + // } - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array + // 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"); + // } + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // @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; + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // 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; + // } - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // 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; + // } - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // 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 (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } + // @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"); + // } - // 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); + // 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 (copiedAttachmentMetadata == null) { - fail("Could not find the copied attachment with fil 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", 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"); + // } - // 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); - } + // 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"); + // } - // 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); - } + // 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"); + // } + // } - // Verify customProperty2 - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied. Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // @Test + // @Order(32) + // void testUploadNAttachments() throws IOException { + // System.out.println("Test (32): Upload maximum 4 attachments in an exsisting 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 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"); + // } + // } + // } - 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(33) + // void testDiscardDraftWithoutAttachments() { + // System.out.println("Test (33) : Discard draft without adding attachments"); - @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()); - } - } + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - sourceObjectIds.clear(); - for (Map metadata : attachmentsMetadata) { - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } + // if (response.equals("Could not create entity")) { + // 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"); - } - } + // response = api.deleteEntityDraft(appUrl, entityName, response); + // if (!response.equals("Entity Draft Deleted")) { + // fail("Draft was not discarded properly"); + // } + // } - @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"); - } - } + // @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(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(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(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.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(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(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(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"); - } - } + // @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(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"); - } - } + // // 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(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (47): Rename link in entity"); - List attachments = new ArrayList<>(); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } - 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 sourceAttachmentId = createResponse.get(1); - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save 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); - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String updateResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, + // updateBody); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // if (!updateResponse.equals("Updated")) { + // fail("Could not update attachment notes field"); + // } - 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"); + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - } + // // Fetch attachment metadata to get objectId + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - @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"); - } + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - 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"); - } + // // Store objectId in array + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.isEmpty()) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(0, sourceObjectId); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save 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); + // } - editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // // Create target entity + // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomTargetEntity.equals("Could not create entity")) { + // fail("Could not create target 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"); - } - } + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array - @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<>(); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - String 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); + // } - 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"); - } + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - 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"); - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target 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"); - } - } + // // 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(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 notes field was 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(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"); + // } - 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("sample1.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); - testStatus = true; - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!testStatus) { - fail("Could not edit link with an invalid URL"); - } - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create 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 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); + // } - 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"); - } - } + // // 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(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity. Response: " + saveSourceResponse); + // } - Boolean testStatus = false; - List attachments = new ArrayList<>(); + // // Fetch attachment metadata to get objectId and verify secondary properties + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - 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); - } + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - @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<>(); + // // Store objectId in array for reuse + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.size() < 2) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(1, sourceObjectId); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // // 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 (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(1)); // 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"); + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - 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"); - } + // // 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 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"); - } - } + // if (copiedAttachmentMetadata == null) { + // fail("Could not find the copied attachment with file in target 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"); + // 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); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // // Verify customProperty2 + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied. Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or 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); - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - List invalidObjectIds = Collections.singletonList("incorrectObjectId"); + // if (readResponse.equals("OK")) { + // testStatus = true; + // } + // if (!testStatus) { + // fail( + // "Could not verify that all secondary properties were copied from source to target + // attachment"); + // } + // } - 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()); - } + // @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 saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveResponse.equals("Saved")) { - fail("Could not save target entity after unsuccessful copy"); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - if (!deleteSourceResponse.equals("Entity Deleted")) { - fail("Could not delete source 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(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"); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - 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 (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save new source entity"); - } + // 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"); + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } + // // 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 sourceObjectIds = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // // 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 (sourceObjectIds.isEmpty()) { - fail("Could not fetch objectId from new source entity"); - } + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity. Response: " + saveSourceResponse); + // } - 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); - } + // // Fetch attachment metadata to get objectId and verify notes and secondary properties + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity after copying link"); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.size() < 3) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(2, sourceObjectId); + // } - 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 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); + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); + // 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); + // } - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment. Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } - System.out.println("Attachment type and URL validated successfully."); + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity"); + // } - String attachmentId = (String) copiedAttachment.get("ID"); - assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open the attachment"); - } + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - if (!deleteResponse.equals("Entity Deleted")) { - fail("Could not delete new source entity"); - } - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to 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"); + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create new source entity"); - } + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - 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 (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save new source 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); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } + // if (copiedAttachmentMetadata == null) { + // fail("Could not find the copied attachment with fil in target entity"); + // } - List invalidObjectIds = Collections.singletonList("invalidObjectId123"); + // // 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); + // } - 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 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); + // } - // 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"); - } + // // Verify customProperty2 + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied. Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - 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"); - } - } + // // 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(58) - void testCopyLinkSuccessNewEntityDraft() throws IOException { - System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); + // 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); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // @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 (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or 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"); + // } + // } - 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 (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"); + // } + // } - List sourceObjectIds = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // @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 (sourceObjectIds.isEmpty()) { - fail("Could not fetch object Id for link"); - } + // @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 copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link: " + copyResponse); - } + // @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"); + // } + // } - 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"); + // @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 expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); + // @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"); + // } + // } - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // @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"); + // } + // } - System.out.println("Attachment type and URL validated successfully."); + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (47): Rename link in entity"); + // List attachments = new ArrayList<>(); - String attachmentId = (String) copiedAttachment.get("ID"); - assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - 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, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save 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"); + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - sourceObjectIds.clear(); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - 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"); - } - } + // 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"); - 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"); - } - } + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save 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; - 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); - } + // @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"); + // } - @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, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - // 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); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - // 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"); + // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - // 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"); + // 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 createResponse = - api.createAttachment( - appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); + // @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<>(); - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - changelogAttachmentID = createResponse.get(1); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - 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"); + // 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"); + // } - // Fetch changelog for the newly created attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // 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"); + // } - // 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"); - } + // 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(61) - void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - System.out.println( - "Test (61): Modify note field and custom property, then verify changelog shows created + 3 updated entries"); + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (50): Edit existing link in 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); + // 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 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"); + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // 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"); + // } + // } + // } - // 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"); + // @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"); + // } + // } - // Fetch changelog after modifications - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // @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"); + // } + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); - // 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)"); + // Boolean testStatus = false; + // List attachments = new ArrayList<>(); - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); + // 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); + // } - // 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"); - } + // @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<>(); - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println( - "Test (62): Rename attachment and verify changelog increases with rename entry"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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"); + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - // 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 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"); + // } - // Save entity after rename - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // 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"); + // List sourceObjectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // Fetch changelog after rename - Map changelogAfterRename = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Id for link"); + // } - assertNotNull(changelogAfterRename, "Changelog response should not be null after rename"); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link: " + copyResponse); + // } - // 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"); + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } - @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); - } + // 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(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 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"); + // } + // } - // 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(55) + // void testCopyLinkUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (55): Copy invalid type of link from one entity to another new 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"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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 (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // List invalidObjectIds = Collections.singletonList("incorrectObjectId"); - 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"); + // 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, newEntityID); - 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 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 deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // if (!deleteSourceResponse.equals("Entity Deleted")) { + // fail("Could not delete source entity"); + // } + // } - // Fetch changelog after initial save - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // @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 has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + - // customProperty2) - assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); + // 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(3, changeLogs.size(), "Should have exactly 3 changelog entries"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } - // Save entity again without any modifications - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // List sourceObjectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // 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"); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch objectId from new source entity"); + // } - // Fetch changelog after second save - Map changelogAfterSecondSave = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // 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); + // } - assertNotNull( - changelogAfterSecondSave, "Changelog response should not be null after second save"); + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // 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"); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } - @SuppressWarnings("unchecked") - List> changeLogsAfterSecondSave = - (List>) changelogAfterSecondSave.get("changeLogs"); - assertEquals( - 3, - changeLogsAfterSecondSave.size(), - "Should still have exactly 3 changelog entries after second save"); + // attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // Clean up the entity - api.deleteEntity(appUrl, entityName, newEntityID); - } + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch. Expected '" + // + expectedType + // + "' but got '" + // + receivedType + // + "'."); - @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"); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - // 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); + // System.out.println("Attachment type and URL validated successfully."); - // 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"); + // String attachmentId = (String) copiedAttachment.get("ID"); + // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - // 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"); + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open the attachment"); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // if (!deleteResponse.equals("Entity Deleted")) { + // fail("Could not delete new source entity"); + // } + // } - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - String newAttachmentID = createResponse.get(1); + // @Test + // @Order(57) + // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println( + // "Test (57): Copy invalid type of link from new entity to existing target 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"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create new source entity"); + // } - // Save the entity immediately without any modifications - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // 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"); + // } - // 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"); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save new source entity"); + // } - // Save entity again without modifying the attachment - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } - // Edit entity to fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // List invalidObjectIds = Collections.singletonList("invalidObjectId123"); - // Fetch changelog for the attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); + // 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()); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // // 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"); + // } - // 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); - } + // 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(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println( - "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // @Test + // @Order(58) + // void testCopyLinkSuccessNewEntityDraft() throws IOException { + // System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); - // 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"); - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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 (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or 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"); + // 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 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 sourceObjectIds = + // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, + // copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Id for link"); + // } - // 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 copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link: " + copyResponse); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // 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); + // } - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetBeforeMoveResponse); - } + // @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"); + // } + // } - // 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"); - } - } + // 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"); + // } + // } - // 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 (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); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println("Test (60): View changelog for newly created attachment"); - @Test - @Order(66) - public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - System.out.println( - "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // // 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 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"); - } + // // 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 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 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"); - 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"); + // 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"); + // } - // 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"); + + // // 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"); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // @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); + // } - // 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(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"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from 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); - // 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 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"); - 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 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"); - File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - List targetCreateResponse = - api.createAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - srvpath, - targetPostData, - duplicateFile); + // 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); + // } - if (!targetCreateResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment on target entity"); - } + // @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"); - // 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"); + // // 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"); - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // 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"); - @Test - @Order(67) - public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - System.out.println( - "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + // 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 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(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println( + // "Test (65): Move attachments from Source Entity to Target Entity with 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"); - // 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); - } - } + // // 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); - } + // // 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()); + // } + // } - // 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); - } - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - 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"); + // } - // 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: " + saveTargetBeforeMoveResponse); + // } - // 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); - } + // // 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"); + // } + // } - // 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"); - } + // // 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 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); - } + // // 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(66) + // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + // System.out.println( + // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // 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"); + // // 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(68) - public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - System.out.println( - "Test (68): Move valid attachments from Source Entity to Target Entity 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())); + // // 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 - 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 - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // 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"); + // } - // 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()); - } - } + // 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"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target 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); + // } - // 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 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); + // } - // 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"); - } + // @Test + // @Order(67) + // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + // System.out.println( + // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); - // 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"); - } - } + // // 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: 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"); + // // 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 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); - } + // 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(69) - public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - System.out.println( - "Test (69): Move attachments into existing Target Entity when duplicate exists without sourceFacet"); + // // 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); + // } + // } - // 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"); - } + // // 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); + // } + // } - // 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 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 - 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 - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // 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(); - } - } else { - fail("Attachment metadata does not contain objectId"); - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // // 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); + // } - 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"); + // } - // 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"); - } + // // 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); + // } - // 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"); + // // 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); + // } + // } - 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"); - } + // // 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 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(); + // @Test + // @Order(68) + // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (68): Move valid attachments from Source Entity to Target Entity without + // sourceFacet"); - // 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); + // // 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())); - int nonDuplicateCount = moveObjectIds.size() - 1; - int expectedTargetCount = initialTargetCount + nonDuplicateCount; + // 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( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have initial attachments plus non-duplicate moved attachments"); + // // 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 at least one non-duplicate attachment was moved - assertTrue( - targetMetadataAfterMove.size() > initialTargetCount, - "Target should have more attachments after move (non-duplicates added)"); + // // 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 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); - } + // // 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()); + // } + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - @Test - @Order(70) - public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - throws Exception { - System.out.println( - "Test (70): Move attachments with notes and secondary properties without 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())); + // // 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"); + // } - 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 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"); + // } + // } - // 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"); - } - } + // // 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); + // } - // 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); - } - } + // // 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(69) + // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (69): Move attachments into existing Target Entity when duplicate exists without + // 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"); - // Get source attachment count before move - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create 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 before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // // 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 target attachment count before move - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // 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"); - } + // // 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"); + // } - // 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); - } + // // 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"); - // 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); - } - } + // 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"); + // } - // 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); - } + // // 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(71) - public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - System.out.println( - "Test (71): Move attachments with invalid or undefined secondary properties"); + // // 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())); - files.add(new File(classLoader.getResource("WDIRSCodeList.csv").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"); + // } - // 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 + // 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"); - } + // // 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); + // } + // } - // Get source attachment count before move - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // // 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 target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create 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 before move - String saveTargetBeforeMoveResponse68 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse68.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); - } + // // 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); + // } + // } - // 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"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - // Verify attachments moved to target - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // // Get source attachment count before move + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - 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"); - } - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // 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"); + // // 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); - } + // // 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"); + // } - @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"); + // // 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 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"); - } + // // 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); + // } + // } - // 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 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); + // } - 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(71) + // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + // System.out.println( + // "Test (71): Move attachments with invalid or undefined secondary properties"); - // 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"); + // } - // 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"); + // // 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())); - 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()); - // 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()); - } - } + // // 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"); + // } + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // 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); + // } - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // // 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); + // } - 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"); - } + // // 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 saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // // Get source attachment count before move + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // 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"); - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // 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"); + // // 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 all expected attachments are in target - Set targetFileNames = - targetMetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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"); + // } - for (File file : files) { - assertTrue( - targetFileNames.contains(file.getName()), - "Target should contain attachment: " + file.getName()); - } + // // 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"); + // } + // } - // 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 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); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - sourceCountBeforeMove, - sourceMetadataAfterMove.size(), - "Source entity in draft mode retains attachments after move (copy behavior)"); + // @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"); + // } - Set sourceFileNamesAfterMove = - sourceMetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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())); - for (File file : files) { - assertTrue( - sourceFileNamesAfterMove.contains(file.getName()), - "Source (draft) should still 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"); - // 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(73) - public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { - System.out.println( - "Test (73): Edit attachment file name in Source Entity and move it to Target 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); + // } - // 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"); - } + // // 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()); + // } + // } - // Add attachment with original name (sample.txt) - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = 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", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - 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"); - } + // 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"); + // } - String attachmentId = createResponse.get(1); - assertNotNull(attachmentId, "Attachment 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"); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Save target before move + // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - // 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"); - } + // // 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"); + // } - // 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"); + // // 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()); + // } - // 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); - } + // // 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 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> 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 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(73) + // public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { + // System.out.println( + // "Test (73): Edit attachment file name in Source Entity and move it to Target 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); + // // 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 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"); + // // Add attachment with original name (sample.txt) + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = 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", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - @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"); + // 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"); + // } - // 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 attachmentId = createResponse.get(1); + // assertNotNull(attachmentId, "Attachment 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())); + // // 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"); + // // 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"); + // } - // 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"); - } - } + // // 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); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // 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"); + // } - // 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()); - } - } + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // 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"); + // } - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // // 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 Target Entity 1 - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity 1"); - } + // @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"); - // 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 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 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"); - } + // // 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 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"); + // 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 all expected files are in Target Entity 1 - Set target1FileNames = - target1MetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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"); + // } + // } - for (File file : files) { - assertTrue( - target1FileNames.contains(file.getName()), - "Target 1 should 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); + // } - // 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"); - } + // // Get count of attachments in source + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have 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"); - } + // // 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()); + // } + // } - // 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); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - 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(moveSourceFolderId, "Source folder ID should not be null"); - assertNotNull(target1FolderId, "Target 1 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"); + // } - // 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"); - } + // // 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"); + // } - // 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"); + // // 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 all expected files are in Target Entity 2 - Set target2FileNames = - target2MetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // 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()); + // } - for (File file : files) { - assertTrue( - target2FileNames.contains(file.getName()), - "Target 2 should contain attachment: " + file.getName()); - } + // // 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 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); - } + // // 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"); + // } - @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"); - } + // // 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); + // } - // 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())); + // 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()); + // } + // } - 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"); + // 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"); + // } - // 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"); - } - } + // // 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()); + // } - // 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); - } + // // 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); + // } - // 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()); - } - } + // @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"); + // } - 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())); - assertNotNull(moveSourceFolderId, "Source folder ID 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"); - // 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"); - } + // // 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"); + // } + // } - // 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(); - } + // // 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); + // } - // 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); + // // Get count of attachments in source + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - // 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"); + // // 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 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"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Clean up - delete both entities using SDM role - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - @Test - @Order(76) - void testRenameAttachmentWithExtensionChange() throws IOException { - System.out.println( - "Test (76) : Rename attachment changing extension from .pdf to .txt - should return extension change warning"); + // // 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 1: Create a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (newEntityID.equals("Could not create entity")) { - fail("Could not create entity"); - } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!saveResponse.equals("Saved")) { - fail("Could not save new entity: " + saveResponse); - } + // // 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 2: Upload a PDF attachment - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); + // // 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); + // } - 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"); + // @Test + // @Order(76) + // void testRenameAttachmentWithExtensionChange() throws IOException { + // System.out.println( + // "Test (76) : Rename attachment changing extension from .pdf to .txt - should return + // extension change warning"); - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (editResponse != "Entity in draft mode") { - fail("Could not put entity in draft mode for PDF upload"); - } + // // Step 1: Create a new entity + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (newEntityID.equals("Could not create entity")) { + // fail("Could not create entity"); + // } + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save new entity: " + saveResponse); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - String check = createResponse.get(0); - if (!check.equals("Attachment created")) { - fail("Could not upload sample.pdf: " + check); - } - String newAttachmentID = createResponse.get(1); + // // Step 2: Upload a PDF attachment + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Step 3: Save the entity - String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (!savedAfterUpload.equals("Saved")) { - fail("Could not save entity after PDF upload: " + savedAfterUpload); - } + // 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"); - // Step 4: Edit the entity - String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - if (editDraftResponse != "Entity in draft mode") { - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not put entity in draft mode for rename"); - } + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (editResponse != "Entity in draft mode") { + // fail("Could not put entity in draft mode for PDF upload"); + // } - // Step 5: Rename the attachment changing the extension from .pdf to .txt - String renameResponse = - api.renameAttachment( - appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); - if (!renameResponse.equals("Renamed")) { - api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - api.deleteEntity(appUrl, entityName, newEntityID); - fail("Could not rename attachment: " + renameResponse); - } + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); + // String check = createResponse.get(0); + // if (!check.equals("Attachment created")) { + // fail("Could not upload sample.pdf: " + check); + // } + // String newAttachmentID = createResponse.get(1); - // Step 6: Save and validate the extension change error message - String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertNotNull(saveWithWarningResponse, "Response should not be null"); + // // Step 3: Save the entity + // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (!savedAfterUpload.equals("Saved")) { + // fail("Could not save entity after PDF upload: " + savedAfterUpload); + // } - String expectedMessage = - "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + // // Step 4: Edit the entity + // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // if (editDraftResponse != "Entity in draft mode") { + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not put entity in draft mode for rename"); + // } - com.fasterxml.jackson.databind.JsonNode messagesNode = - new ObjectMapper().readTree(saveWithWarningResponse); - assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); + // // Step 5: Rename the attachment changing the extension from .pdf to .txt + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); + // if (!renameResponse.equals("Renamed")) { + // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // api.deleteEntity(appUrl, entityName, newEntityID); + // fail("Could not rename attachment: " + renameResponse); + // } - boolean foundExtensionError = false; - for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - if (messageNode.has("message")) { - String message = messageNode.get("message").asText(); - if (message.contains("Changing the file extension is not allowed")) { - foundExtensionError = true; - assertEquals( - expectedMessage, - message, - "Extension change error message does not match expected value"); - break; - } - } - } + // // Step 6: Save and validate the extension change error message + // String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // newEntityID); + // assertNotNull(saveWithWarningResponse, "Response should not be null"); + + // String expectedMessage = + // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must + // retain its original extension \".pdf\"."; + + // com.fasterxml.jackson.databind.JsonNode messagesNode = + // new ObjectMapper().readTree(saveWithWarningResponse); + // assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); + + // boolean foundExtensionError = false; + // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + // if (messageNode.has("message")) { + // String message = messageNode.get("message").asText(); + // if (message.contains("Changing the file extension is not allowed")) { + // foundExtensionError = true; + // assertEquals( + // expectedMessage, + // message, + // "Extension change error message does not match expected value"); + // break; + // } + // } + // } - assertTrue( - foundExtensionError, - "Expected extension change warning not found in response. Full response: " - + saveWithWarningResponse); + // assertTrue( + // foundExtensionError, + // "Expected extension change warning not found in response. Full response: " + // + saveWithWarningResponse); - // Clean up - api.deleteEntity(appUrl, entityName, newEntityID); - } + // // Clean up + // api.deleteEntity(appUrl, entityName, newEntityID); + // } @Test @Order(77) From 2329ff2b3f5814e2e0faad88dbf4091cefbf02c1 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 16:37:46 +0530 Subject: [PATCH 10/13] Uncommenting singleFacet Changes --- .../cds/sdm/IntegrationTest_SingleFacet.java | 11434 ++++++++-------- 1 file changed, 5635 insertions(+), 5799 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 482340853..7f076c1a9 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 @@ -388,6237 +388,6073 @@ void testUploadAttachmentWithoutSDMRole() throws IOException { } } - // @Test - // @Order(7) - // void testUploadSingleAttachmentPDFDuplicate() throws IOException { - // System.out.println("Test (7) : Upload duplicate pdf"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Boolean testStatus = false; + @Test + @Order(7) + void testUploadSingleAttachmentPDFDuplicate() throws IOException { + System.out.println("Test (7) : Upload duplicate pdf"); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Boolean testStatus = false; + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID); + 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, entityID); + if (response == "Entity in draft mode") { + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + testStatus = false; + } else { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + String expectedJson = + "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" already exists. Rename the object and try again.\"}}"; + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode actualJsonNode = objectMapper.readTree(check); + JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); + if (expectedJsonNode.equals(actualJsonNode)) { + testStatus = true; + } + } + } + } + if (!testStatus) { + fail("Attachment created"); + } + } + + @Test + @Order(8) + void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { + System.out.println("Test (8) : Upload duplicate pdf in different entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { + entityID2 = response; + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if (response == "Saved") { + response = api.checkEntity(appUrl, entityName, entityID2); + if (response.equals("Entity exists")) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Could not create entity"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID2); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + if (response == "Entity in draft mode") { + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, file); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + attachmentID4 = createResponse.get(1); + response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, attachmentID4); + if (response.equals("OK")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if (response.equals("Saved")) { + response = api.readAttachment(appUrl, entityName, facetName, entityID2, attachmentID4); + + if (response.equals("OK")) { + testStatus = true; + } + } + } + } + } + if (!testStatus) { + fail("Could not upload sample.pdf " + response); + } + } + + @Test + @Order(9) + void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { + System.out.println("Test (9): Create attachment with restricted character in filename"); + + 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", entityID); + 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, entityID); + if (response.equals("Entity in draft mode")) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + attachmentID6 = createResponse.get(1); + + String restrictedFilename = "a/\\bc.pdf"; + response = + api.renameAttachment( + appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); + + if (response.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + api.renameAttachment( + appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Saved".equals(response)) testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + } + if (!testStatus) { + fail("Attachment created with restricted character in filename"); + } + } + + @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"); + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // 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 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 response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response == "Entity in draft mode") { - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID, srvpath, postData, file); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // testStatus = false; - // } else { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // String expectedJson = - // "{\"error\":{\"code\":\"500\",\"message\":\"An object named \\\"sample.pdf\\\" - // already exists. Rename the object and try again.\"}}"; - // ObjectMapper objectMapper = new ObjectMapper(); - // JsonNode actualJsonNode = objectMapper.readTree(check); - // JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); - // if (expectedJsonNode.equals(actualJsonNode)) { - // testStatus = true; - // } - // } - // } - // } - // if (!testStatus) { - // fail("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; + } - // @Test - // @Order(8) - // void testUploadSingleAttachmentPDFDuplicateDifferentEntity() throws IOException { - // System.out.println("Test (8) : Upload duplicate pdf in different entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - // entityID2 = response; - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if (response == "Saved") { - // response = api.checkEntity(appUrl, entityName, entityID2); - // if (response.equals("Entity exists")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Could not create entity"); - // } + 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; + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + 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; + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID2); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + 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"); + } + } - // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - // if (response == "Entity in draft mode") { - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, - // file); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // attachmentID4 = createResponse.get(1); - // response = api.readAttachmentDraft(appUrl, entityName, facetName, entityID2, - // attachmentID4); - // if (response.equals("OK")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if (response.equals("Saved")) { - // response = api.readAttachment(appUrl, entityName, facetName, entityID2, - // attachmentID4); - - // if (response.equals("OK")) { - // testStatus = true; - // } - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not upload sample.pdf " + response); - // } - // } + @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; + } - // @Test - // @Order(9) - // void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException { - // System.out.println("Test (9): Create attachment with restricted character in filename"); + 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; + } - // boolean testStatus = false; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + 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; + } - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + 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"); + } + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // 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(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 response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Entity in draft mode")) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, entityID, srvpath, postData, tempFile); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // attachmentID6 = createResponse.get(1); - - // String restrictedFilename = "a/\\bc.pdf"; - // response = - // api.renameAttachment( - // appUrl, entityName, facetName, entityID, attachmentID6, restrictedFilename); - - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // api.renameAttachment( - // appUrl, entityName, facetName, entityID, attachmentID6, "sample3.pdf"); - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if ("Saved".equals(response)) testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // } - // if (!testStatus) { - // fail("Attachment created with restricted character in filename"); - // } - // } + System.out.println("Entity created"); - // @Test - // @Order(10) - // void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { - // System.out.println("Test (10): Upload attachments, delete one and create entity"); + System.out.println("Creating attachment PDF"); + ClassLoader classLoader = getClass().getClassLoader(); - // 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 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"); + } - // 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); + 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); + + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } + + // Store objectId in array + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.isEmpty()) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(0, sourceObjectId); + } + + 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); + } + + // Create target entity + copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } + + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array + + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } + + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity"); + } + + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } + + // 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); + } + + // 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 (readResponse.equals("OK")) { + testStatus = true; + } + if (!testStatus) { + fail("Could not verify that notes field was copied from source to target attachment"); + } + } + + @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"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample1.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 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); + } + + // 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); + } + + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity. Response: " + saveSourceResponse); + } + + // Fetch attachment metadata to get objectId and verify secondary properties + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } + + // Store objectId in array for reuse + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.size() < 2) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(1, sourceObjectId); + } - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Failed to create entity after deleting one attachment"); - // } - // } + // 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); + } - // @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()); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment. Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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"); + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array - // 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); - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // @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"); - // } - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // @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"); - // } - // } + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save 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"); - // } - // } + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // @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"); - // } - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // @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); - // } - // } + // 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); - // if (!testStatus) { - // fail("Multiple renames should have failed due to one unsupported characters"); - // } - // } + if (copiedAttachmentMetadata == null) { + fail("Could not find the copied attachment with file in target entity"); + } - // @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."); - // } - // } + 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); + } - // @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; - // } - // } + // Verify customProperty2 + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied. Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // @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"); - // } - // } + // 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(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"); - // } - // } + 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(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(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; - // @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 editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source 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"); - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample2.pdf").getFile()); - // @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"); - // } - // } + 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(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"); - // } - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // @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 (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } - // @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"); - // } + String sourceAttachmentId = createResponse.get(1); - // 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"); - // } + // 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); - // 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 updateNotesResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateNotesBody); - // 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"); - // } - // } + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update attachment notes field"); + } - // @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; - // } + // 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); + } - // 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; - // } + // 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); + } - // 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; - // } + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity. Response: " + saveSourceResponse); + } - // 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"); - // } - // } + // Fetch attachment metadata to get objectId and verify notes and secondary properties + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - // @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"); - // } + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } - // 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"); - // } + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.size() < 3) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(2, sourceObjectId); + } - // 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 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); + } - // 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"); - // } - // } + 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); + } - // @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; - // } + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment. Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } - // 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; - // } + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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; - // } + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array - // 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"); - // } - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // @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"); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // 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"); - // } + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target 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"); - // } + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // 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"); - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // 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"); - // } - // } + // 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(32) - // void testUploadNAttachments() throws IOException { - // System.out.println("Test (32): Upload maximum 4 attachments in an exsisting entity"); + if (copiedAttachmentMetadata == null) { + fail("Could not find the copied attachment with fil in target 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"); - // } - // } - // } + // 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); + } - // @Test - // @Order(33) - // void testDiscardDraftWithoutAttachments() { - // System.out.println("Test (33) : Discard draft without adding attachments"); + // 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 response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // Verify customProperty2 + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied. Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // if (response.equals("Could not create entity")) { - // 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); - // response = api.deleteEntityDraft(appUrl, entityName, response); - // if (!response.equals("Entity Draft Deleted")) { - // fail("Draft was not discarded properly"); - // } - // } + 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(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(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(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"); - // } - // } + sourceObjectIds.clear(); + 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"); - // } - // } + 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(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(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"); + } + } - // @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(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"); + } + } - // // 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(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"); + } + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + @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"); + } + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + @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 sourceAttachmentId = createResponse.get(1); + @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"); + } + } - // // 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); + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (47): Rename link in entity"); + List attachments = new ArrayList<>(); - // String updateResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, - // updateBody); + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // if (!updateResponse.equals("Updated")) { - // fail("Could not update attachment notes field"); - // } + 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"); - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // // Fetch attachment metadata to get objectId - // 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 - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.isEmpty()) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(0, 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"); - // 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); - // } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + } - // // 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(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"); + } - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array + 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 copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target 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"); + } + } - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + @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<>(); - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // // 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); - // } + 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 attachment content can be read from target entity - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // if (readResponse.equals("OK")) { - // testStatus = true; - // } - // if (!testStatus) { - // fail("Could not verify that notes field was copied from source to target attachment"); - // } - // } + 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(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"); - // } + 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"); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample1.pdf").getFile()); + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (50): Edit existing link in 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"); + 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 createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + 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 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(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (51): Edit existing link with invalid url"); + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // // 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 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); - // // Save source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity. Response: " + saveSourceResponse); - // } + testStatus = true; + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!testStatus) { + fail("Could not edit link with an invalid URL"); + } + } - // // Fetch attachment metadata to get objectId and verify secondary properties - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + @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<>(); - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + 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"); + } + } - // // Store objectId in array for reuse - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.size() < 2) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(1, sourceObjectId); - // } + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); - // // 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); - // } + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment. Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } + 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 editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + @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<>(); - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + 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"); + } - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + List sourceObjectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Id for 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 copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link: " + copyResponse); + } - // if (copiedAttachmentMetadata == null) { - // fail("Could not find the copied attachment with file in target entity"); - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } - // 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); - // } + attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // // Verify customProperty2 - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied. Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + 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"); + } - // // 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 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"); + } + } - // 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(55) + void testCopyLinkUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (55): Copy invalid type of link from one entity to another new entity"); - // @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"); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target 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"); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + List invalidObjectIds = Collections.singletonList("incorrectObjectId"); - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + 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 (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after unsuccessful copy"); + } - // 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"); - // } + String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + if (!deleteSourceResponse.equals("Entity Deleted")) { + fail("Could not delete source entity"); + } + } - // // 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(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"); + } - // // 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 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 source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity. Response: " + saveSourceResponse); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save new source entity"); + } - // // Fetch attachment metadata to get objectId and verify notes and secondary properties - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + List sourceObjectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.size() < 3) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(2, sourceObjectId); - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch objectId from new source 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); - // } + 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); + } - // 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); - // } + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment. Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target 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"); - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch. Expected '" + + expectedType + + "' but got '" + + receivedType + + "'."); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + System.out.println("Attachment type and URL validated successfully."); - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + String attachmentId = (String) copiedAttachment.get("ID"); + assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + 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 deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + if (!deleteResponse.equals("Entity Deleted")) { + fail("Could not delete new source 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(57) + void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println( + "Test (57): Copy invalid type of link from new entity to existing target entity"); - // if (copiedAttachmentMetadata == null) { - // fail("Could not find the copied attachment with fil in target entity"); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create new source 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); - // } + 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 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 saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save new source entity"); + } - // // Verify customProperty2 - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied. Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } - // // 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 invalidObjectIds = Collections.singletonList("invalidObjectId123"); - // 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); - // } + 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()); + } - // @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()); - // } - // } + // 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"); + } - // 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 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"); + } + } - // 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(58) + void testCopyLinkSuccessNewEntityDraft() throws IOException { + System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); - // @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"); - // } - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // @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"); - // } - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target 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"); - // } - // } + 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"); + } - // @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"); - // } - // } + List sourceObjectIds = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .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"); - // } - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Id for link"); + } - // @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"); - // } - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link: " + copyResponse); + } - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (47): Rename link in entity"); - // List attachments = new ArrayList<>(); + 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"); - // 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 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); + } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save 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"); - // @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"); - // } + sourceObjectIds.clear(); - // 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"); - // } + 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"); + } + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + 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"); + } + } - // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit 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; + 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); + } - // 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(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println("Test (60): View changelog for newly created attachment"); - // @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<>(); + // 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); - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + // 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"); - // 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"); - // } + // 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); - // 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"); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + changelogAttachmentID = createResponse.get(1); - // 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"); - // } - // } + 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"); - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (50): Edit existing link in entity"); + // Fetch changelog for the newly created attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // 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"; + assertNotNull(changelogResponse, "Changelog response should not be null"); - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link"); - // } + // 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"); + } - // 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"); - // } - // } - // } + @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"); - // @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"); - // } - // } + // 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); - // @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 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"); - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // Boolean testStatus = false; - // List attachments = new ArrayList<>(); + // 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 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); - // } + // Fetch changelog after modifications + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // @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<>(); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // 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)"); - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); - // 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 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"); + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + @Test + @Order(62) + void testChangelogAfterRenamingAttachment() throws IOException { + System.out.println( + "Test (62): Rename attachment and verify changelog increases with rename entry"); - // List sourceObjectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + // 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"); - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Id for link"); - // } + // 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 copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link: " + copyResponse); - // } + // Save entity after rename + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.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"); - - // 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 after rename + Map changelogAfterRename = + 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(changelogAfterRename, "Changelog response should not be null after rename"); - // @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 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"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @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); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or 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"); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // List invalidObjectIds = Collections.singletonList("incorrectObjectId"); + // 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); - // 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()); - // } + // 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"); - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target entity after unsuccessful copy"); - // } + // 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"); - // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // if (!deleteSourceResponse.equals("Entity Deleted")) { - // fail("Could not delete source entity"); - // } - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // @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"); - // } + 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 sourceObjectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch objectId from new source entity"); - // } + // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + + // customProperty2) + assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); - // 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); - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // Save entity again without any modifications + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // 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, newEntityID); + 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 second save + Map changelogAfterSecondSave = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch. Expected '" - // + expectedType - // + "' but got '" - // + receivedType - // + "'."); + assertNotNull( + changelogAfterSecondSave, "Changelog response should not be null after second save"); - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // 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"); - // System.out.println("Attachment type and URL validated successfully."); + @SuppressWarnings("unchecked") + List> changeLogsAfterSecondSave = + (List>) changelogAfterSecondSave.get("changeLogs"); + assertEquals( + 3, + changeLogsAfterSecondSave.size(), + "Should still have exactly 3 changelog entries after second save"); - // String attachmentId = (String) copiedAttachment.get("ID"); - // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // Clean up the entity + api.deleteEntity(appUrl, entityName, newEntityID); + } - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open the 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"); - // String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // if (!deleteResponse.equals("Entity Deleted")) { - // fail("Could not delete new 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); - // @Test - // @Order(57) - // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println( - // "Test (57): Copy invalid type of link from new entity to existing target 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"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create new 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"); - // 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"); - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save new source entity"); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + String newAttachmentID = createResponse.get(1); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } + 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"); - // List invalidObjectIds = Collections.singletonList("invalidObjectId123"); + // 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"); - // 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()); - // } + // Save entity again without modifying the attachment + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // // 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"); - // } + // Edit entity to fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // 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"); - // } - // } + // Fetch changelog for the attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); - // @Test - // @Order(58) - // void testCopyLinkSuccessNewEntityDraft() throws IOException { - // System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // 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); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println( + "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // 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 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 sourceObjectIds = - // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, - // copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + // 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 (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Id for link"); - // } + 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"); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link: " + copyResponse); - // } + // 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> 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"); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // 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); - // } + // 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()); + } + } - // @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"); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // 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 target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target 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; - // 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); - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetBeforeMoveResponse); + } - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println("Test (60): View changelog for newly created attachment"); + // 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"); + } + } - // // 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); + // 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"); - // // 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"); + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // 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"); + @Test + @Order(66) + public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + System.out.println( + "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // 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"); - // } + // 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(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"); - // } + // 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(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); - // } + 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(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"); + // 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()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata for attachment: " + attachmentId); + } + } - // // 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 all objectIds from 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"); - - // // 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); - // } + // 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(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"); + 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 a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(newEntityID, "Failed to create new entity"); - // assertNotEquals("Could not create entity", newEntityID); + File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + List targetCreateResponse = + api.createAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + srvpath, + targetPostData, + duplicateFile); - // // 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"); + if (!targetCreateResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment on 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"); + // 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); + } - // 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); - // } + // 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"); - // @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(67) + public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + System.out.println( + "Test (67): Move attachments with notes and secondary properties with 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()); - // } - // } + // 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); + } + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // 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 target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create 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 before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity: " + 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); + } + } - // // 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"); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // // 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"); + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // 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); + } - // @Test - // @Order(66) - // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - // System.out.println( - // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // 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"); + } - // // 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 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); + } - // // 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 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); + } + } - // 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 (all moved with sourceFacet) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals(0, sourceMetadataAfterMove.size(), "Source entity has no attachments after 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"); - // } - // } + // 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(68) + public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + System.out.println( + "Test (68): Move valid attachments from Source Entity to Target Entity 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()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // 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 all objectIds from 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())); - // // 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"); - // } + 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 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"); + } + } - // 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"); - // } + // 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()); + // 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()); + } + } - // // 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); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // @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())); + // 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"); + } - // 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 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"); + } + } - // // 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"); - // } - // } + // 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"); - // // 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 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); + } - // // 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); - // } - // } + // 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()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // 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 all objectIds from 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())); - // // Create target entity - // 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"); - // // 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 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 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 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()); + // 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 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 object IDs for all attachments"); + } - // // 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"); + // 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"); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // 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"); - // @Test - // @Order(68) - // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (68): Move valid attachments from Source Entity to Target Entity without - // sourceFacet"); + 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"); + } - // // 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 entity before move + String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // // 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 initial target metadata count + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int initialTargetCount = targetMetadataBeforeMove.size(); - // 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"); + // 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 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"); - // } - // } + // Expected Behavior - Verify duplicate was skipped, other attachments moved + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + int nonDuplicateCount = moveObjectIds.size() - 1; + int expectedTargetCount = initialTargetCount + nonDuplicateCount; - // // 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()); - // } - // } + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have initial attachments plus non-duplicate moved attachments"); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Verify at least one non-duplicate attachment was moved + assertTrue( + targetMetadataAfterMove.size() > initialTargetCount, + "Target should have more attachments after move (non-duplicates added)"); - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // 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 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"); - // } + @Test + @Order(70) + public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + throws Exception { + System.out.println( + "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - // // 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"); - // } - // } + // 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: 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); - // } + // 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(69) - // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (69): Move attachments into existing Target Entity when duplicate exists 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"); - // } + // 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); + } + } - // // 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 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); + } + } - // 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()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata 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); - // } + 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()); - // // 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 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 object IDs for all attachments"); - // } + // 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 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"); - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // // 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"); + // Get target attachment count before move + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); - // 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"); - // } + // 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 entity before move - // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + // 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 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"); - // } + // 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); + } + } - // // 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); - // } + // 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); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + @Test + @Order(71) + public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + System.out.println( + "Test (71): Move attachments with invalid or undefined secondary properties"); - // @Test - // @Order(70) - // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - // throws Exception { - // System.out.println( - // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + // 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 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"); - // } + // 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())); + 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"); - // } - // } + // 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); + } - // // 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); - // } - // } + // 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 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); - // } - // } + // 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 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 saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + // Verify attachments moved to target + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // // 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"); - // } + 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 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 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 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 - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // 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); - // } + @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(71) - // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - // System.out.println( - // "Test (71): Move attachments with invalid or undefined secondary properties"); + // 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 add attachments - // 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"); - // // 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); - // } + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // 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); - // } + // 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()); + } + } - // // 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); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // // Get source attachment count before move - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + 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"); - // } + // 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 saveTargetBeforeMoveResponse68 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse68.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); - // } + // 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 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"); - // } + // 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"); + } - // // 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 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 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); - // } + // Verify all expected attachments are in target + Set targetFileNames = + targetMetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); - // @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"); + for (File file : files) { + assertTrue( + targetFileNames.contains(file.getName()), + "Target should contain attachment: " + file.getName()); + } - // // 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"); - // } + // 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); + } - // // 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())); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + sourceCountBeforeMove, + sourceMetadataAfterMove.size(), + "Source entity in draft mode retains attachments after move (copy behavior)"); - // 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"); + Set sourceFileNamesAfterMove = + sourceMetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); - // // 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"); - // } - // } + for (File file : files) { + assertTrue( + sourceFileNamesAfterMove.contains(file.getName()), + "Source (draft) should still contain attachment: " + file.getName()); + } - // // 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); - // } + // 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(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"); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // Add attachment with original name (sample.txt) + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // 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"); - // } + 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"); - // // 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 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"); - // // 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 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 - // 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()); - // } + // 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"); + } - // // 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); - // } + // 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"); - // 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 after rename + saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after rename: " + saveSourceResponse); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // 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"); + } - // @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 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 attachment - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // 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"); + } - // // Add attachment with original name (sample.txt) - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // 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); - // 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 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"); - // 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(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"); - // // 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"); + } - // // 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"); - // } + // 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())); - // // 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", "application/pdf"); + 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"); - // } + // 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"); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // 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"); - // } + // 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 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); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // @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"); + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // // 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 1 + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity 1"); + } - // // 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 target1 before move + String saveTarget1BeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTarget1BeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity 1 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 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"); + } - // // 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 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"); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // 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()); - // // Get count of attachments in source - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + for (File file : files) { + assertTrue( + target1FileNames.contains(file.getName()), + "Target 1 should contain attachment: " + file.getName()); + } - // // 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 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 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 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 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 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 all expected files are in Target Entity 2 + Set target2FileNames = + target2MetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); - // // 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"); - // } + for (File file : files) { + assertTrue( + target2FileNames.contains(file.getName()), + "Target 2 should contain attachment: " + file.getName()); + } - // // 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); - // } + // 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); + } - // 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()); - // } - // } + @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"); + } - // 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())); - // // 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 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"); + } + } - // @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"); - // } + // 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); + } - // // 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 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()); + } + } - // 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"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // // 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"); - // } - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // // 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); - // } + // 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"); + } - // // Get count of attachments in source - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + // 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(); + } - // // 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 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); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // 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"); - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // 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"); - // // 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"); - // } + // Clean up - delete both entities using SDM role + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // 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(); - // } + @Test + @Order(76) + void testRenameAttachmentWithExtensionChange() throws IOException { + System.out.println( + "Test (76) : Rename attachment changing extension from .pdf to .txt - should return extension change warning"); - // // 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); - // } + // Step 1: Create a new entity + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (newEntityID.equals("Could not create entity")) { + fail("Could not create entity"); + } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!saveResponse.equals("Saved")) { + fail("Could not save new entity: " + saveResponse); + } - // @Test - // @Order(76) - // void testRenameAttachmentWithExtensionChange() throws IOException { - // System.out.println( - // "Test (76) : Rename attachment changing extension from .pdf to .txt - should return - // extension change warning"); + // Step 2: Upload a PDF attachment + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // // Step 1: Create a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (newEntityID.equals("Could not create entity")) { - // fail("Could not create entity"); - // } - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save new entity: " + saveResponse); - // } + 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"); - // // Step 2: Upload a PDF attachment - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (editResponse != "Entity in draft mode") { + fail("Could not put entity in draft mode for PDF upload"); + } - // 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, newEntityID, srvpath, postData, file); + String check = createResponse.get(0); + if (!check.equals("Attachment created")) { + fail("Could not upload sample.pdf: " + check); + } + String newAttachmentID = createResponse.get(1); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (editResponse != "Entity in draft mode") { - // fail("Could not put entity in draft mode for PDF upload"); - // } + // Step 3: Save the entity + String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!savedAfterUpload.equals("Saved")) { + fail("Could not save entity after PDF upload: " + savedAfterUpload); + } - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); - // String check = createResponse.get(0); - // if (!check.equals("Attachment created")) { - // fail("Could not upload sample.pdf: " + check); - // } - // String newAttachmentID = createResponse.get(1); + // Step 4: Edit the entity + String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (editDraftResponse != "Entity in draft mode") { + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not put entity in draft mode for rename"); + } - // // Step 3: Save the entity - // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!savedAfterUpload.equals("Saved")) { - // fail("Could not save entity after PDF upload: " + savedAfterUpload); - // } + // Step 5: Rename the attachment changing the extension from .pdf to .txt + String renameResponse = + api.renameAttachment( + appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); + if (!renameResponse.equals("Renamed")) { + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not rename attachment: " + renameResponse); + } - // // Step 4: Edit the entity - // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (editDraftResponse != "Entity in draft mode") { - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not put entity in draft mode for rename"); - // } + // Step 6: Save and validate the extension change error message + String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertNotNull(saveWithWarningResponse, "Response should not be null"); - // // Step 5: Rename the attachment changing the extension from .pdf to .txt - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facetName, newEntityID, newAttachmentID, "renamed_document.txt"); - // if (!renameResponse.equals("Renamed")) { - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not rename attachment: " + renameResponse); - // } + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - // // Step 6: Save and validate the extension change error message - // String saveWithWarningResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // newEntityID); - // assertNotNull(saveWithWarningResponse, "Response should not be null"); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match expected value"); - // break; - // } - // } - // } + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue(messagesNode.isArray(), "sap-messages response should be a JSON array"); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match expected value"); + break; + } + } + } - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found in response. Full response: " - // + saveWithWarningResponse); + assertTrue( + foundExtensionError, + "Expected extension change warning not found in response. Full response: " + + saveWithWarningResponse); - // // Clean up - // api.deleteEntity(appUrl, entityName, newEntityID); - // } + // Clean up + api.deleteEntity(appUrl, entityName, newEntityID); + } @Test @Order(77) From a202300ae27037d365c187c376ffaca5b43eef0b Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 17:05:12 +0530 Subject: [PATCH 11/13] Fixing node issue --- ...ntegrationTest_Chapters_MultipleFacet.java | 12303 ++++++++------- .../sdm/IntegrationTest_MultipleFacet.java | 12426 ++++++++-------- .../cds/sdm/IntegrationTest_SingleFacet.java | 4 + 3 files changed, 12204 insertions(+), 12529 deletions(-) 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 b7b6c8441..9ae9dcf92 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 @@ -7,8 +7,13 @@ import java.io.File; import java.io.IOException; 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.*; @@ -467,6235 +472,6109 @@ void testCreateNewBookWithChapterAndAttachments() throws IOException { } } - // @Test - // @Order(7) - // void testRenameChapterAttachments() { - // System.out.println("Test (7) : Rename single attachment, reference, and footnote in - // chapter"); - // Boolean testStatus = true; - - // try { - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - // if ("Entity in draft mode".equals(response)) { - // String[] name = {"sample123", "reference123", "footnote123"}; - // for (int i = 0; i < facet.length; i++) { - // // Read the facet to ensure it exists - // response = - // api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], - // name[i]); - // if (!"Renamed".equals(response)) { - // testStatus = false; - // System.out.println(facet[i] + " was not renamed: " + response); - // } - // } - // // Save book draft if everything is renamed - // if (testStatus) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if (!"Saved".equals(response)) { - // testStatus = false; - // System.out.println("Book draft was not saved: " + response); - // } - // } else { - // // Attempt save despite potential rename failures - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // } - // } else { - // testStatus = false; - // System.out.println("Book was not put into draft mode: " + response); - // } - // } catch (Exception e) { - // testStatus = false; - // System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); - // } - - // if (!testStatus) { - // fail("There was an error during the rename test process for chapter."); - // } - // } - - // @Test - // @Order(8) - // void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { - // System.out.println("Test (8): Create chapter attachments with unsupported characters"); - // 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("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, bookEntityName, srvpath, bookID); - // if (!"Entity in draft mode".equals(response)) { - // fail("Book not in draft mode: " + response); - // return; - // } - - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", chapterID); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); - - // if (!"Attachment created".equals(createResponse.get(0))) { - // fail("Could not create attachment in chapter facet: " + facet[i]); - // return; - // } - - // String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE - // response = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); - - // System.out.println("Rename response for chapter " + facet[i] + ": " + response); - // } - - // // Save should fail - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - - // // ---------------- PARSE JSON ---------------- - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // String message = root.path("error").path("message").asText(); - - // // ---------------- NORMALIZE MESSAGE ---------------- - // // 1. Normalize smart quotes - // // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared - // message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); - - // // ---------------- EXPECTED MESSAGE (EXACT) ---------------- - // String expectedMessage = - // "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" - // + "Table: attachments\n" - // + "Page: IntegrationTestEntity"; - - // if (message.equals(expectedMessage)) { - - // for (int i = 0; i < facet.length; i++) { - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - - // if (!testStatus) { - // fail("Test for unsupported characters in chapter attachments failed"); - // } - // } - - // @Test - // @Order(9) - // void testRenameSingleDuplicateInChapter() throws IOException { - // System.out.println( - // "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); - // Boolean testStatus = false; - // int counter = 0; - - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Edit entity response: " + response); - - // if ("Entity in draft mode".equals(response)) { - // // To create a duplicate within the same facet, we need to rename ID2[i] to - // // the same name as an existing file in that facet. After test 7, the existing files are: - // // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) - // // We rename ID2[i] (sample123.txt from test 8) to these names which already exist - // String[] duplicateNames = {"sample123", "reference123", "footnote123"}; - // String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; - - // // Try to rename to duplicate file names (names that already exist in each facet) - // for (int i = 0; i < facet.length; i++) { - // response = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); - // System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); - // if ("Renamed".equals(response)) { - // counter++; - // } - // } - // System.out.println("Renamed count: " + counter); - - // if (counter == facet.length) { - // // Try to save - should fail with duplicate error - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Save response (expecting error): " + response); - - // // Parse JSON response to check for duplicate error - // ObjectMapper mapper = new ObjectMapper(); - // try { - // JsonNode root = mapper.readTree(response); - // String message = root.path("error").path("message").asText(); - - // if (message.contains("already exists")) { - // System.out.println("Duplicate error detected as expected: " + message); - // counter = 0; - // // Rename with valid different names - // for (int i = 0; i < facet.length; i++) { - // response = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); - // System.out.println("Rename " + facet[i] + " to valid name: " + response); - // if ("Renamed".equals(response)) { - // counter++; - // } - // } - - // if (counter == facet.length) { - // // Save should now succeed - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Final save response: " + response); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } else { - // System.out.println("Unexpected error message: " + message); - // } - // } catch (Exception e) { - // // Response might not be JSON if save succeeded (shouldn't happen with duplicates) - // System.out.println("Response was not JSON error: " + response); - // // If save succeeded unexpectedly, we still need to ensure book is saved - // if ("Saved".equals(response)) { - // System.out.println( - // "Save succeeded unexpectedly - duplicates might be in different facets"); - // } - // } - // } - // } else { - // System.out.println("Book was not put into draft mode: " + response); - // } - - // if (!testStatus) { - // fail("Duplicate rename test failed for chapter"); - // } - // } - - // @Test - // @Order(10) - // void testRenameToValidateNamesInChapter() throws IOException { - // System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); - // Boolean testStatus = false; - - // // Create a new book and chapter for this test - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // bookID3 = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); - // if (!"Could not create entity".equals(chapterResponse)) { - // chapterID3 = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID3); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); - // } - - // String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; - - // boolean allRenamed = true; - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); - // if (!"Renamed".equals(response1)) { - // allRenamed = false; - // System.out.println( - // "Failed to rename " - // + facet[i] - // + " to valid name " - // + validNames[i] - // + ": " - // + response1); - // } - // } - - // if (allRenamed) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } - // } - - // if (!testStatus) { - // fail("Could not rename chapter attachments to valid names"); - // } - // } - - // @Test - // @Order(11) - // void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { - // System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); - // boolean testStatus = true; - - // try { - // String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Edit entity response: " + response); - - // if (response.equals("Entity in draft mode")) { - // String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; - // for (int i = 0; i < facet.length; i++) { - // response = - // apiNoRoles.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); - // System.out.println("Rename response for " + facet[i] + ": " + response); - // if (!"Renamed".equals(response)) { - // testStatus = false; - // } - // } - - // if (testStatus) { - // // Save should fail with permission error - // response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // System.out.println("Save response (expecting permission error): " + response); - - // // The expected error should indicate no permissions to update - // String expected = - // "[{\"code\":\"\",\"message\":\"Could not update the following - // files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update - // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3}]"; - - // // Check if response contains permission error - // if (!response.equals(expected) - // && !response.contains("do not have the required permissions")) { - // System.out.println("Expected permission error but got: " + response); - // testStatus = false; - // } else { - // System.out.println("Got expected permission error"); - // } - // } else { - // // Some renames failed - save to release draft - // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // } - // } else { - // System.out.println("Could not edit entity: " + response); - // testStatus = false; - // } - // } catch (Exception e) { - // System.out.println("Exception: " + e.getMessage()); - // testStatus = false; - // } - - // if (!testStatus) { - // fail("Chapter attachment got renamed without SDM roles."); - // } - // } - - // @Test - // @Order(12) - // void testDeleteSingleChapterAttachment() throws IOException { - // System.out.println( - // "Test (12) : Delete single attachment, reference, and footnote from chapter"); - // Boolean testStatus = false; - // int deleteCounter = 0; - - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - // if (response.equals("Deleted")) deleteCounter++; - // } - // if (deleteCounter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); - // if (response.equals("Saved")) { - // int verifyCounter = 0; - // for (int i = 0; i < facet.length; i++) { - // response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); - // if (response.equals("Could not read Attachment")) verifyCounter++; - // } - // if (verifyCounter == facet.length) { - // testStatus = true; - // } else { - // fail( - // "Could not verify all deleted chapter facets. Verified: " - // + verifyCounter - // + "/" - // + facet.length); - // } - // } else { - // fail("Could not save book after deleting chapter attachments"); - // } - // } else { - // fail( - // "Could not delete all chapter attachments. Deleted: " - // + deleteCounter - // + "/" - // + facet.length); - // } - // } else { - // fail("Could not edit book to draft mode"); - // } - - // if (!testStatus) { - // fail("Test failed to delete chapter attachments"); - // } - // } - - // @Test - // @Order(13) - // void testUploadBlockedMimeTypeToChapter() throws IOException { - // System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); - // Boolean testStatus = false; - - // // Create new book and chapter - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // bookID4 = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); - // if (!"Could not create entity".equals(chapterResponse)) { - // chapterID4 = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = - // new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID4); - // postData.put("mimeType", "application/rtf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // boolean allBlocked = true; - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID4, 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)) { - // allBlocked = false; - // System.out.println( - // "Chapter facet " - // + facet[i] - // + " incorrectly accepted blocked mimeType: " - // + actualResponse); - // } - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); - // if ("Saved".equals(response) && allBlocked) { - // testStatus = true; - // } - // } - // } - - // if (!testStatus) { - // fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); - // } - // } - - // @Test - // @Order(14) - // void testDeleteBookAndChapter() { - // System.out.println("Test (14) : Delete book (and its chapters)"); - // Boolean testStatus = false; - // // Delete books (chapters are deleted automatically as they're composition) - // String response = api.deleteEntity(appUrl, bookEntityName, bookID); - // String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); - // String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); - // String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); - // if (response.equals("Entity Deleted") - // && response2.equals("Entity Deleted") - // && response3.equals("Entity Deleted") - // && response4.equals("Entity Deleted")) testStatus = true; - // if (!testStatus) fail("Could not delete books"); - // } - - // @Test - // @Order(15) - // void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { - // System.out.println( - // "Test (15) : Rename & Update secondary property in chapter before book is saved"); - // System.out.println("Creating book and chapter"); - - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (!response.equals("Could not create entity")) { - // bookID5 = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); - // if (!chapterResponse.equals("Could not create entity")) { - // chapterID5 = chapterResponse; - - // System.out.println("Creating attachment, reference, and footnote in chapter"); - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID5); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // boolean allCreated = true; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); - // if (tempID[i] == null || tempID[i].isEmpty()) { - // System.out.println("Failed to create attachment for facet: " + facet[i]); - // allCreated = false; - // } - // } - - // System.out.println("Attachments, References, and Footnotes created in chapter"); - // System.out.println( - // "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + - // tempID[2]); - - // if (!allCreated) { - // fail("Could not create all attachments for test 15"); - // } - - // // Reset counter for this test - // counter = 0; - - // // Use valid dropdown value for customProperty1 - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; - - // for (int i = 0; i < facet.length; i++) { - // System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - // System.out.println("Rename response for " + facet[i] + ": " + response1); - - // // Update customProperty1 (String - dropdown value) - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); - - // // Update customProperty2 (Integer) - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - - // // Update customProperty5 (DateTime) - using customProperty5 like Books test - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - - // // Update customProperty6 (Boolean) - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - // // Check all updates succeeded - // if ("Renamed".equals(response1) - // && "Updated".equals(updateSecondaryPropertyResponse1) - // && "Updated".equals(updateSecondaryPropertyResponse2) - // && "Updated".equals(updateSecondaryPropertyResponse3) - // && "Updated".equals(updateSecondaryPropertyResponse4)) { - // counter++; - // } else { - // System.out.println( - // "Update failed for " - // + facet[i] - // + ": rename=" - // + response1 - // + ", dropdown=" - // + updateSecondaryPropertyResponse1 - // + ", int=" - // + updateSecondaryPropertyResponse2 - // + ", datetime=" - // + updateSecondaryPropertyResponse3 - // + ", bool=" - // + updateSecondaryPropertyResponse4); - // } - // } - - // System.out.println("Counter after all facets: " + counter); - // if (counter == facet.length) { - // // Save the book (not the chapter) - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // System.out.println("Save response: " + response); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } else { - // System.out.println( - // "Counter is less than " + facet.length + ", not saving. Counter: " + counter); - // } - // } - // } - - // if (!testStatus) { - // fail( - // "Could not update secondary properties in chapter before book save. Counter: " + - // counter); - // } - // } - - // @Test - // @Order(16) - // void testUploadNAttachmentsToChapter() throws IOException { - // System.out.println("Test (16) : Upload N attachments to chapter"); - // Boolean testStatus = false; - // counter = 0; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); - - // for (int j = 0; j < 5; j++) { - // // Create temp file with unique name per iteration - // File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); - // Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID5); - // 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, bookEntityName, srvpath, bookID5); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - // String check = facetResponse.get(0); - // if (check.equals("Attachment created")) { - // counter++; - // } else { - // System.out.println( - // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - // } - // } - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if (!response.equals("Saved")) { - // System.out.println( - // "Failed to save book after creating attachments in chapter: " + response); - // } - // } else { - // System.out.println("Could not edit book draft: " + response); - // } - // tempFile.delete(); - // } - - // if (counter == 15) { // 5 iterations * 3 facets - // testStatus = true; - // } - - // if (!testStatus) { - // fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); - // } - // } - - // @Test - // @Order(17) - // void testDiscardDraftWithoutChapterAttachments() { - // System.out.println("Test (17) : Discard book draft without chapter attachments"); - // Boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // // Create chapter but don't add attachments - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft without chapter attachments was not discarded properly"); - // } - // } - - // @Test - // @Order(18) - // void testDiscardDraftWithChapterAttachments() throws IOException { - // System.out.println("Test (18) : Discard book draft with chapter attachments"); - // Boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // // Create chapter - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // 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 chapter - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - // String check = facetResponse.get(0); - // if (!check.equals("Attachment created")) { - // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - // } - // } - - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft with chapter attachments was not discarded properly"); - // } - // } - - // @Test - // @Order(19) - // void testUploadChapterAttachmentWithoutSDMRole() throws IOException { - // System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); - // Boolean testStatus = true; - - // String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, - // tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // try { - // List createResponse = - // apiNoRoles.createAttachment( - // appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); - // String check = createResponse.get(0); - - // if (check.equals("Attachment created")) { - // testStatus = false; - // } - // } catch (Exception e) { - // // Expected to fail - // testStatus = true; - // } - - // apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // } - // } - - // if (!testStatus) { - // fail("Chapter attachment was uploaded without SDM roles"); - // } - // } - - // @Test - // @Order(20) - // void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { - // System.out.println( - // "Test (20): Rename & Update secondary property in chapter after book is saved"); - // Boolean testStatus = false; - // counter = 0; // Reset counter for this test - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // System.out.println("Editing book, response: " + response); - - // if (response.equals("Entity in draft mode")) { - // // Use unique names that won't conflict with existing attachments - // String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; - // Integer secondaryPropertyInt = 42; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // System.out.println("Renaming and updating secondary properties for chapter attachment"); - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // // Get the first attachment ID from the chapter - // try { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); - // if (!metadata.isEmpty()) { - // tempID[i] = (String) metadata.get(0).get("ID"); - // } - // } catch (IOException e) { - // fail("Could not fetch metadata for chapter: " + e.getMessage()); - // } - - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if (response.equals("Saved")) { - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for chapter attachment"); - // } - // } - // } - // if (!testStatus) fail("Could not update secondary properties in chapter after book is - // saved"); - // } - - // @Test - // @Order(21) - // void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException - // { - // System.out.println( - // "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); - // System.out.println("Creating book and chapter"); - // Boolean testStatus = false; - // int localCounter = 0; - // int createCounter = 0; - - // // Create new book and chapter for this test - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // if (tempID[i] != null) { - // createCounter++; - // } - // } - - // // Only proceed if all facets were created successfully - // if (createCounter == facet.length) { - // // Prepare test data - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + - // "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], - // invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // localCounter++; - // } - // } - - // if (localCounter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - // // Fetch metadata and verify values weren't updated due to invalid property - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, - // tempID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - - // // Parse JSON response and check for expected error messages - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved with expected invalid property errors"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for chapter attachment is unsuccessful"); - // } - // } else { - // System.out.println( - // "Not all facets updated successfully. localCounter: " + localCounter); - // } - // } else { - // System.out.println( - // "Not all facets created successfully. createCounter: " + createCounter); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // } - // } - // if (!testStatus) - // fail("Could not update invalid secondary property in chapter before book is saved"); - // } - - // @Test - // @Order(22) - // void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { - // System.out.println( - // "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); - // System.out.println("Creating book and chapter"); - // Boolean testStatus = false; - // int localCounter = 0; - // int createCounter = 0; - - // // Create new book and chapter - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] tempID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // tempID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // if (tempID[i] != null) { - // createCounter++; - // } - // } - - // // Only proceed if all facets were created successfully - // if (createCounter != facet.length) { - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // fail("Not all facets created successfully. createCounter: " + createCounter); - // } - - // // Save the book first - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (!response.equals("Saved")) { - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // fail("Could not save book initially"); - // } - - // // Now edit to update with invalid property - // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (response.equals("Entity in draft mode")) { - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt = 12; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testidinvalid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + - // "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], - // invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // localCounter++; - // } - // } - - // if (localCounter == facet.length) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, - // tempID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - - // // Parse JSON response and check for expected error messages - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved with expected invalid property errors"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for chapter attachment is unsuccessful"); - // } - // } else { - // System.out.println( - // "Not all facets updated successfully. localCounter: " + localCounter); - // } - // } - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // } - // } - // if (!testStatus) - // fail("Could not update invalid secondary property in chapter after book is saved"); - // } - - // @Test - // @Order(23) - // void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { - // System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); - - // boolean testStatus = false; - - // // Reuse bookID5 and chapterID5 - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - - // if (response.equals("Entity in draft mode")) { - // ClassLoader classLoader = getClass().getClassLoader(); - - // // Use temp files with unique names to avoid duplicate name errors - // File originalPdf = - // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // File originalTxt = - // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); - - // File file1 = File.createTempFile("test23_pdf_", ".pdf"); - // File file2 = File.createTempFile("test23_txt_", ".txt"); - // Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); - // Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); - - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", chapterID5); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // Map postData2 = new HashMap<>(postData1); - // postData2.put("up__ID", chapterID5); - // postData2.put("mimeType", "text/plain"); - - // boolean allCreated = true; - // String[] tempID1 = new String[facet.length]; - // String[] tempID2 = new String[facet.length]; - - // for (int i = 0; i < facet.length; i++) { - // List response1 = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); - // List response2 = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); - - // if (response1.get(0).equals("Attachment created") - // && response2.get(0).equals("Attachment created")) { - // tempID1[i] = response1.get(1); // to keep one - // tempID2[i] = response2.get(1); // will delete this one - // } else { - // System.out.println("Failed to create attachments for facet " + facet[i]); - // System.out.println("Response 1: " + response1.get(0)); - // System.out.println("Response 2: " + response2.get(0)); - // allCreated = false; - // break; - // } - - // String deleteResponse = - // api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); - // if (!"Deleted".equals(deleteResponse)) { - // allCreated = false; - // break; - // } - // } - - // file1.delete(); - // file2.delete(); - - // if (allCreated) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } else { - // System.out.println("Could not edit book: " + response); - // } - - // if (!testStatus) { - // fail("Failed to upload multiple chapter facet entries, delete one per facet and save - // book"); - // } - // } - - // @Test - // @Order(24) - // void testUpdateChapterEntityDraft() throws IOException { - // System.out.println("Test (24): Update chapter in book draft with new facet content"); - // boolean testStatus = false; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - // // Use unique temp file name to avoid duplicates - // File tempFile = File.createTempFile("test24_sample_", ".pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", chapterID5); - // 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, bookEntityName, srvpath, bookID5); - // if (response.equals("Entity in draft mode")) { - // boolean allCreated = true; - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); - // String check = facetResponse.get(0); - // if (!check.equals("Attachment created")) { - // allCreated = false; - // System.out.println( - // "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); - // } - // } - - // if (allCreated) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } else { - // System.out.println("Could not edit book: " + response); - // } - - // tempFile.delete(); - - // if (!testStatus) { - // fail("Failed to update chapter entity draft with new attachments"); - // } - // } - - // @Test - // @Order(25) - // void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - // throws IOException { - // System.out.println( - // "Test (25): Rename & Update secondary properties for multiple chapter attachments after - // book is saved"); - // System.out.println("Creating book and chapter with multiple attachments"); - - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!"Could not create entity".equals(chapterResponse)) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create PDF attachments - // postData.put("mimeType", "application/pdf"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // String[] pdfID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // pdfID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create TXT attachments - // postData.put("mimeType", "application/txt"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // String[] txtID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // txtID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create EXE attachments - // postData.put("mimeType", "application/exe"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // String[] exeID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // exeID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Save book first - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (!"Saved".equals(response)) { - // fail("Could not save book initially"); - // } - - // // Edit book to update chapter attachments - // response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (response.equals("Entity in draft mode")) { - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // // Update PDF properties - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String renameResp = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - // String upd3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - // String upd4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - - // if ("Renamed".equals(renameResp) - // && "Updated".equals(upd1) - // && "Updated".equals(upd2) - // && "Updated".equals(upd3) - // && "Updated".equals(upd4)) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // PDF"); - // } - // } - - // // Update TXT properties (only boolean) - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - // String upd = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - // if ("Updated".equals(upd)) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // TXT"); - // } - // } - - // // Update EXE properties (dropdown and int) - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValueExe = integrationTestUtils.getDropDownValue(); - // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyDropdownExe = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - // RequestBody bodyIntExe = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], - // bodyDropdownExe); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // EXE"); - // } - // } - - // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (response.equals("Saved")) { - // System.out.println("Book saved"); - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for chapter - // attachments"); - // } - // } - // } - // api.deleteEntity(appUrl, bookEntityName, tempBookID); - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property in chapter after book is saved"); - // } - // } - - // @Test - // @Order(26) - // void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() - // throws IOException { - // System.out.println( - // "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter - // facets before book is saved"); - // System.out.println("Creating book and chapter"); - - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (!"Could not create entity".equals(response)) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!"Could not create entity".equals(chapterResponse)) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create PDF attachments - // postData.put("mimeType", "application/pdf"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // String[] pdfID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // pdfID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create TXT attachments - // postData.put("mimeType", "application/txt"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // String[] txtID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // txtID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // // Create EXE attachments - // postData.put("mimeType", "application/exe"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // String[] exeID = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // exeID[i] = - // CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, - // file); - // } - - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample1234.pdf"; - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - - // // Update PDF properties - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String renameResp = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); - // String upd3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); - // String upd4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); - // String updInvalid = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], - // invalidPropertyPDF); - - // if ("Renamed".equals(renameResp) - // && "Updated".equals(upd1) - // && "Updated".equals(upd2) - // && "Updated".equals(upd3) - // && "Updated".equals(upd4) - // && "Updated".equals(updInvalid)) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // PDF"); - // } - // } - - // // Update TXT properties - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - // String upd = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); - // if ("Updated".equals(upd)) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // TXT"); - // } - // } - - // // Update EXE properties - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValueExe = integrationTestUtils.getDropDownValue(); - // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyDropdownExe = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - // RequestBody bodyIntExe = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); - - // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // EXE"); - // } - // } - - // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - - // // Verify PDF metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); - // assertEquals(expectedNames[0], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertNull(metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } - - // // Verify TXT metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); - // assertEquals(expectedNames[1], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertTrue((Boolean) metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } - - // // Verify EXE metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); - // assertEquals(expectedNames[2], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); - // assertEquals(1234, metadata.get("customProperty2")); - // } - - // // Parse JSON response and check for expected error messages - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved with expected invalid property errors"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessful for invalid properties and successful for valid - // attachments"); - // } - // } - // } - // } - - // if (!testStatus) { - // fail("Could not update secondary property before book is saved"); - // } - // } - - // @Test - // @Order(27) - // void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() - // throws IOException { - // System.out.println( - // "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter - // attachments after book is saved"); - - // // Reuse bookID5 and chapterID5 - // System.out.println("Editing book with bookID5: " + bookID5); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // System.out.println("Edit entity response: " + response); - - // if (response.equals("Entity in draft mode")) { - // // Fetch existing attachments from the chapter - // List> attachmentsMeta = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); - // List> referencesMeta = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); - // List> footnotesMeta = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); - - // System.out.println("Attachments count: " + attachmentsMeta.size()); - // System.out.println("References count: " + referencesMeta.size()); - // System.out.println("Footnotes count: " + footnotesMeta.size()); - - // if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) - // { - // String[] pdfID = new String[facet.length]; - // String[] txtID = new String[facet.length]; - // String[] exeID = new String[facet.length]; - - // pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); - // pdfID[1] = (String) referencesMeta.get(0).get("ID"); - // pdfID[2] = (String) footnotesMeta.get(0).get("ID"); - - // txtID[0] = (String) attachmentsMeta.get(1).get("ID"); - // txtID[1] = (String) referencesMeta.get(1).get("ID"); - // txtID[2] = (String) footnotesMeta.get(1).get("ID"); - - // exeID[0] = (String) attachmentsMeta.get(2).get("ID"); - // exeID[1] = (String) referencesMeta.get(2).get("ID"); - // exeID[2] = (String) footnotesMeta.get(2).get("ID"); - - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // // PDF - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); - - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); - - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); - - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); - - // String updateSecondaryPropertyResponse5 = - // api.updateInvalidSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated") - // && updateSecondaryPropertyResponse5.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // PDF"); - // } - // } - - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); - // String updateSecondaryPropertyResponseTXT1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // TXT"); - // } - // } - - // Integer secondaryPropertyInt3 = 12; - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // for (int i = 0; i < facet.length; i++) { - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); - - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); - - // if (updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " - // EXE"); - // } - // } - - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); - // // Note: Don't verify specific filenames since previous tests may have changed them - // System.out.println("Save response: " + response); - - // // Parse JSON response to check for invalid secondary property errors in all three - // tables - // ObjectMapper mapper = new ObjectMapper(); - // JsonNode root = mapper.readTree(response); - // boolean hasAttachmentsError = false; - // boolean hasReferencesError = false; - // boolean hasFootnotesError = false; - // if (root.isArray()) { - // for (JsonNode node : root) { - // String message = node.path("message").asText(); - // if (message.contains("id1") && message.contains("Table: attachments")) { - // hasAttachmentsError = true; - // } - // if (message.contains("id1") && message.contains("Table: references")) { - // hasReferencesError = true; - // } - // if (message.contains("id1") && message.contains("Table: footnotes")) { - // hasFootnotesError = true; - // } - // } - // } - // if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { - // System.out.println("Book saved"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessful for invalid Secondary properties and successful for - // valid property attachments"); - // } else { - // System.out.println("Save response did not match expected: " + response); - // } - // } else { - // System.out.println("Not enough attachments in facets - need at least 3 per facet"); - // } - // } - // } else { - // System.out.println( - // "Could not edit book - it may be stuck in draft mode from a previous test"); - // } - // if (!testStatus) { - // fail("Could not update secondary property before book is saved"); - // } - // } + @Test + @Order(7) + void testRenameChapterAttachments() { + System.out.println("Test (7) : Rename single attachment, reference, and footnote in chapter"); + Boolean testStatus = true; + + try { + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + if ("Entity in draft mode".equals(response)) { + String[] name = {"sample123", "reference123", "footnote123"}; + for (int i = 0; i < facet.length; i++) { + // Read the facet to ensure it exists + response = + api.renameAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); + if (!"Renamed".equals(response)) { + testStatus = false; + System.out.println(facet[i] + " was not renamed: " + response); + } + } + // Save book draft if everything is renamed + if (testStatus) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if (!"Saved".equals(response)) { + testStatus = false; + System.out.println("Book draft was not saved: " + response); + } + } else { + // Attempt save despite potential rename failures + api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + } + } else { + testStatus = false; + System.out.println("Book was not put into draft mode: " + response); + } + } catch (Exception e) { + testStatus = false; + System.out.println("Exception during renaming chapter attachments: " + e.getMessage()); + } + + if (!testStatus) { + fail("There was an error during the rename test process for chapter."); + } + } + + @Test + @Order(8) + void testCreateChapterAttachmentsWithUnsupportedCharacter() throws IOException { + System.out.println("Test (8): Create chapter attachments with unsupported characters"); + 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("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, bookEntityName, srvpath, bookID); + if (!"Entity in draft mode".equals(response)) { + fail("Book not in draft mode: " + response); + return; + } + + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", chapterID); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID, srvpath, postData, tempFile); + + if (!"Attachment created".equals(createResponse.get(0))) { + fail("Could not create attachment in chapter facet: " + facet[i]); + return; + } + + String restrictedName = "a/\\bc.txt"; // \b becomes BACKSPACE + response = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], restrictedName); + + System.out.println("Rename response for chapter " + facet[i] + ": " + response); + } + + // Save should fail + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + + // ---------------- PARSE JSON ---------------- + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + String message = root.path("error").path("message").asText(); + + // ---------------- NORMALIZE MESSAGE ---------------- + // 1. Normalize smart quotes + // 2. Convert BACKSPACE (\b) to literal "\b" so it can be compared + message = message.replace('‘', '\'').replace('’', '\'').replace("\b", "\\b"); + + // ---------------- EXPECTED MESSAGE (EXACT) ---------------- + String expectedMessage = + "\"a/\\bc.txt\" contains unsupported characters ('/' or '\\'). Rename and try again.\n\n" + + "Table: attachments\n" + + "Page: IntegrationTestEntity"; + + if (message.equals(expectedMessage)) { + + for (int i = 0; i < facet.length; i++) { + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], "sample123.txt"); + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if ("Saved".equals(response)) { + testStatus = true; + } + } + + if (!testStatus) { + fail("Test for unsupported characters in chapter attachments failed"); + } + } + + @Test + @Order(9) + void testRenameSingleDuplicateInChapter() throws IOException { + System.out.println( + "Test (9) : Rename chapter attachment, reference, and footnote to duplicate names"); + Boolean testStatus = false; + int counter = 0; + + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Edit entity response: " + response); + + if ("Entity in draft mode".equals(response)) { + // To create a duplicate within the same facet, we need to rename ID2[i] to + // the same name as an existing file in that facet. After test 7, the existing files are: + // sample123 (ID[0]), reference123 (ID[1]), footnote123 (ID[2]) + // We rename ID2[i] (sample123.txt from test 8) to these names which already exist + String[] duplicateNames = {"sample123", "reference123", "footnote123"}; + String[] validNames = {"unique_sample1.txt", "unique_sample2.txt", "unique_sample3.txt"}; + + // Try to rename to duplicate file names (names that already exist in each facet) + for (int i = 0; i < facet.length; i++) { + response = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], duplicateNames[i]); + System.out.println("Rename " + facet[i] + " to " + duplicateNames[i] + ": " + response); + if ("Renamed".equals(response)) { + counter++; + } + } + System.out.println("Renamed count: " + counter); + + if (counter == facet.length) { + // Try to save - should fail with duplicate error + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Save response (expecting error): " + response); + + // Parse JSON response to check for duplicate error + ObjectMapper mapper = new ObjectMapper(); + try { + JsonNode root = mapper.readTree(response); + String message = root.path("error").path("message").asText(); + + if (message.contains("already exists")) { + System.out.println("Duplicate error detected as expected: " + message); + counter = 0; + // Rename with valid different names + for (int i = 0; i < facet.length; i++) { + response = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID2[i], validNames[i]); + System.out.println("Rename " + facet[i] + " to valid name: " + response); + if ("Renamed".equals(response)) { + counter++; + } + } + + if (counter == facet.length) { + // Save should now succeed + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Final save response: " + response); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } else { + System.out.println("Unexpected error message: " + message); + } + } catch (Exception e) { + // Response might not be JSON if save succeeded (shouldn't happen with duplicates) + System.out.println("Response was not JSON error: " + response); + // If save succeeded unexpectedly, we still need to ensure book is saved + if ("Saved".equals(response)) { + System.out.println( + "Save succeeded unexpectedly - duplicates might be in different facets"); + } + } + } + } else { + System.out.println("Book was not put into draft mode: " + response); + } + + if (!testStatus) { + fail("Duplicate rename test failed for chapter"); + } + } + + @Test + @Order(10) + void testRenameToValidateNamesInChapter() throws IOException { + System.out.println("Test (10) : Rename chapter attachments to validate valid file names"); + Boolean testStatus = false; + + // Create a new book and chapter for this test + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + bookID3 = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID3); + if (!"Could not create entity".equals(chapterResponse)) { + chapterID3 = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID3); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, chapterID3, facet[i], postData, file); + } + + String[] validNames = {"valid_file_name.pdf", "another-valid-name.pdf", "simple123.pdf"}; + + boolean allRenamed = true; + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID3, tempID[i], validNames[i]); + if (!"Renamed".equals(response1)) { + allRenamed = false; + System.out.println( + "Failed to rename " + + facet[i] + + " to valid name " + + validNames[i] + + ": " + + response1); + } + } + + if (allRenamed) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID3); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } + } + + if (!testStatus) { + fail("Could not rename chapter attachments to valid names"); + } + } + + @Test + @Order(11) + void testRenameChapterAttachmentsWithoutSDMRole() throws IOException { + System.out.println("Test (11) : Try to rename chapter attachments without SDM role"); + boolean testStatus = true; + + try { + String response = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Edit entity response: " + response); + + if (response.equals("Entity in draft mode")) { + String[] name = {"noRole1.pdf", "noRole2.pdf", "noRole3.pdf"}; + for (int i = 0; i < facet.length; i++) { + response = + apiNoRoles.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID, ID[i], name[i]); + System.out.println("Rename response for " + facet[i] + ": " + response); + if (!"Renamed".equals(response)) { + testStatus = false; + } + } + + if (testStatus) { + // Save should fail with permission error + response = apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + System.out.println("Save response (expecting permission error): " + response); + + // The expected error should indicate no permissions to update + String expected = + "[{\"code\":\"\",\"message\":\"Could not update the following files.\\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 unique_sample1\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + + // Check if response contains permission error + if (!response.equals(expected) + && !response.contains("do not have the required permissions")) { + System.out.println("Expected permission error but got: " + response); + testStatus = false; + } else { + System.out.println("Got expected permission error"); + } + } else { + // Some renames failed - save to release draft + apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + } + } else { + System.out.println("Could not edit entity: " + response); + testStatus = false; + } + } catch (Exception e) { + System.out.println("Exception: " + e.getMessage()); + testStatus = false; + } + + if (!testStatus) { + fail("Chapter attachment got renamed without SDM roles."); + } + } + + @Test + @Order(12) + void testDeleteSingleChapterAttachment() throws IOException { + System.out.println( + "Test (12) : Delete single attachment, reference, and footnote from chapter"); + Boolean testStatus = false; + int deleteCounter = 0; + + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + if (response.equals("Deleted")) deleteCounter++; + } + if (deleteCounter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID); + if (response.equals("Saved")) { + int verifyCounter = 0; + for (int i = 0; i < facet.length; i++) { + response = api.readAttachment(appUrl, chapterEntityName, facet[i], chapterID, ID[i]); + if (response.equals("Could not read Attachment")) verifyCounter++; + } + if (verifyCounter == facet.length) { + testStatus = true; + } else { + fail( + "Could not verify all deleted chapter facets. Verified: " + + verifyCounter + + "/" + + facet.length); + } + } else { + fail("Could not save book after deleting chapter attachments"); + } + } else { + fail( + "Could not delete all chapter attachments. Deleted: " + + deleteCounter + + "/" + + facet.length); + } + } else { + fail("Could not edit book to draft mode"); + } + + if (!testStatus) { + fail("Test failed to delete chapter attachments"); + } + } + + @Test + @Order(13) + void testUploadBlockedMimeTypeToChapter() throws IOException { + System.out.println("Test (13) : Upload blocked mimeType .rtf to chapter"); + Boolean testStatus = false; + + // Create new book and chapter + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + bookID4 = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID4); + if (!"Could not create entity".equals(chapterResponse)) { + chapterID4 = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = + new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID4); + postData.put("mimeType", "application/rtf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + boolean allBlocked = true; + for (int i = 0; i < facet.length; i++) { + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID4, 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)) { + allBlocked = false; + System.out.println( + "Chapter facet " + + facet[i] + + " incorrectly accepted blocked mimeType: " + + actualResponse); + } + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID4); + if ("Saved".equals(response) && allBlocked) { + testStatus = true; + } + } + } + + if (!testStatus) { + fail("Attachment got uploaded to chapter with blocked .rtf MIME type"); + } + } + + @Test + @Order(14) + void testDeleteBookAndChapter() { + System.out.println("Test (14) : Delete book (and its chapters)"); + Boolean testStatus = false; + // Delete books (chapters are deleted automatically as they're composition) + String response = api.deleteEntity(appUrl, bookEntityName, bookID); + String response2 = api.deleteEntity(appUrl, bookEntityName, bookID2); + String response3 = api.deleteEntity(appUrl, bookEntityName, bookID3); + String response4 = api.deleteEntity(appUrl, bookEntityName, bookID4); + if (response.equals("Entity Deleted") + && response2.equals("Entity Deleted") + && response3.equals("Entity Deleted") + && response4.equals("Entity Deleted")) testStatus = true; + if (!testStatus) fail("Could not delete books"); + } + + @Test + @Order(15) + void testUpdateValidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { + System.out.println( + "Test (15) : Rename & Update secondary property in chapter before book is saved"); + System.out.println("Creating book and chapter"); + + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (!response.equals("Could not create entity")) { + bookID5 = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, bookID5); + if (!chapterResponse.equals("Could not create entity")) { + chapterID5 = chapterResponse; + + System.out.println("Creating attachment, reference, and footnote in chapter"); + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID5); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + boolean allCreated = true; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, chapterID5, facet[i], postData, file); + if (tempID[i] == null || tempID[i].isEmpty()) { + System.out.println("Failed to create attachment for facet: " + facet[i]); + allCreated = false; + } + } + + System.out.println("Attachments, References, and Footnotes created in chapter"); + System.out.println( + "tempID[0]: " + tempID[0] + ", tempID[1]: " + tempID[1] + ", tempID[2]: " + tempID[2]); + + if (!allCreated) { + fail("Could not create all attachments for test 15"); + } + + // Reset counter for this test + counter = 0; + + // Use valid dropdown value for customProperty1 + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + for (int i = 0; i < facet.length; i++) { + System.out.println("Processing facet " + facet[i] + " with tempID: " + tempID[i]); + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + System.out.println("Rename response for " + facet[i] + ": " + response1); + + // Update customProperty1 (String - dropdown value) + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDropdown); + + // Update customProperty2 (Integer) + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + + // Update customProperty5 (DateTime) - using customProperty5 like Books test + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + + // Update customProperty6 (Boolean) + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + // Check all updates succeeded + if ("Renamed".equals(response1) + && "Updated".equals(updateSecondaryPropertyResponse1) + && "Updated".equals(updateSecondaryPropertyResponse2) + && "Updated".equals(updateSecondaryPropertyResponse3) + && "Updated".equals(updateSecondaryPropertyResponse4)) { + counter++; + } else { + System.out.println( + "Update failed for " + + facet[i] + + ": rename=" + + response1 + + ", dropdown=" + + updateSecondaryPropertyResponse1 + + ", int=" + + updateSecondaryPropertyResponse2 + + ", datetime=" + + updateSecondaryPropertyResponse3 + + ", bool=" + + updateSecondaryPropertyResponse4); + } + } + + System.out.println("Counter after all facets: " + counter); + if (counter == facet.length) { + // Save the book (not the chapter) + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + System.out.println("Save response: " + response); + if ("Saved".equals(response)) { + testStatus = true; + } + } else { + System.out.println( + "Counter is less than " + facet.length + ", not saving. Counter: " + counter); + } + } + } + + if (!testStatus) { + fail( + "Could not update secondary properties in chapter before book save. Counter: " + counter); + } + } + + @Test + @Order(16) + void testUploadNAttachmentsToChapter() throws IOException { + System.out.println("Test (16) : Upload N attachments to chapter"); + Boolean testStatus = false; + counter = 0; + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.pdf").getFile()); + + for (int j = 0; j < 5; j++) { + // Create temp file with unique name per iteration + File tempFile = File.createTempFile("sample_iter" + j + "_", ".pdf"); + Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID5); + 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, bookEntityName, srvpath, bookID5); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + String check = facetResponse.get(0); + if (check.equals("Attachment created")) { + counter++; + } else { + System.out.println( + "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + } + } + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if (!response.equals("Saved")) { + System.out.println( + "Failed to save book after creating attachments in chapter: " + response); + } + } else { + System.out.println("Could not edit book draft: " + response); + } + tempFile.delete(); + } + + if (counter == 15) { // 5 iterations * 3 facets + testStatus = true; + } + + if (!testStatus) { + fail("Could not upload N attachments to chapter. Created: " + counter + " out of 15"); + } + } + + @Test + @Order(17) + void testDiscardDraftWithoutChapterAttachments() { + System.out.println("Test (17) : Discard book draft without chapter attachments"); + Boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + // Create chapter but don't add attachments + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft without chapter attachments was not discarded properly"); + } + } + + @Test + @Order(18) + void testDiscardDraftWithChapterAttachments() throws IOException { + System.out.println("Test (18) : Discard book draft with chapter attachments"); + Boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + // Create chapter + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + 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 chapter + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + String check = facetResponse.get(0); + if (!check.equals("Attachment created")) { + System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + } + } + + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft with chapter attachments was not discarded properly"); + } + } + + @Test + @Order(19) + void testUploadChapterAttachmentWithoutSDMRole() throws IOException { + System.out.println("Test (19) : Try to upload chapter attachment without SDM role"); + Boolean testStatus = true; + + String response = apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + try { + List createResponse = + apiNoRoles.createAttachment( + appUrl, chapterEntityName, facet[0], tempChapterID, srvpath, postData, file); + String check = createResponse.get(0); + + if (check.equals("Attachment created")) { + testStatus = false; + } + } catch (Exception e) { + // Expected to fail + testStatus = true; + } + + apiNoRoles.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + } + } + + if (!testStatus) { + fail("Chapter attachment was uploaded without SDM roles"); + } + } + + @Test + @Order(20) + void testUpdateValidSecondaryPropertyInChapter_afterBookIsSaved_single() { + System.out.println( + "Test (20): Rename & Update secondary property in chapter after book is saved"); + Boolean testStatus = false; + counter = 0; // Reset counter for this test + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + System.out.println("Editing book, response: " + response); + + if (response.equals("Entity in draft mode")) { + // Use unique names that won't conflict with existing attachments + String name[] = {"test20_attachment.pdf", "test20_reference.pdf", "test20_footnote.pdf"}; + Integer secondaryPropertyInt = 42; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + System.out.println("Renaming and updating secondary properties for chapter attachment"); + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + // Get the first attachment ID from the chapter + try { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], chapterID5); + if (!metadata.isEmpty()) { + tempID[i] = (String) metadata.get(0).get("ID"); + } + } catch (IOException e) { + fail("Could not fetch metadata for chapter: " + e.getMessage()); + } + + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], name[i]); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], 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, chapterEntityName, facet[i], chapterID5, tempID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, tempID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if (response.equals("Saved")) { + testStatus = true; + System.out.println("Renamed & updated Secondary properties for chapter attachment"); + } + } + } + if (!testStatus) fail("Could not update secondary properties in chapter after book is saved"); + } + + @Test + @Order(21) + void testUpdateInvalidSecondaryPropertyInChapter_beforeBookIsSaved_single() throws IOException { + System.out.println( + "Test (21): Rename & Update invalid secondary property in chapter before book is saved"); + System.out.println("Creating book and chapter"); + Boolean testStatus = false; + int localCounter = 0; + int createCounter = 0; + + // Create new book and chapter for this test + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + if (tempID[i] != null) { + createCounter++; + } + } + + // Only proceed if all facets were created successfully + if (createCounter == facet.length) { + // Prepare test data + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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 updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + localCounter++; + } + } + + if (localCounter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + // Fetch metadata and verify values weren't updated due to invalid property + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + + // Parse JSON response and check for expected error messages + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved with expected invalid property errors"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for chapter attachment is unsuccessful"); + } + } else { + System.out.println( + "Not all facets updated successfully. localCounter: " + localCounter); + } + } else { + System.out.println( + "Not all facets created successfully. createCounter: " + createCounter); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, tempBookID); + } + } + if (!testStatus) + fail("Could not update invalid secondary property in chapter before book is saved"); + } + + @Test + @Order(22) + void testUpdateInvalidSecondaryPropertyInChapter_afterBookIsSaved_single() throws IOException { + System.out.println( + "Test (22): Rename & Update invalid secondary property in chapter after book is saved"); + System.out.println("Creating book and chapter"); + Boolean testStatus = false; + int localCounter = 0; + int createCounter = 0; + + // Create new book and chapter + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] tempID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + tempID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + if (tempID[i] != null) { + createCounter++; + } + } + + // Only proceed if all facets were created successfully + if (createCounter != facet.length) { + api.deleteEntity(appUrl, bookEntityName, tempBookID); + fail("Not all facets created successfully. createCounter: " + createCounter); + } + + // Save the book first + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (!response.equals("Saved")) { + api.deleteEntity(appUrl, bookEntityName, tempBookID); + fail("Could not save book initially"); + } + + // Now edit to update with invalid property + response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (response.equals("Entity in draft mode")) { + String name1 = "sample.pdf"; + Integer secondaryPropertyInt = 12; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testidinvalid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], name1); + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], 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, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + localCounter++; + } + } + + if (localCounter == facet.length) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, tempID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + + // Parse JSON response and check for expected error messages + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved with expected invalid property errors"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for chapter attachment is unsuccessful"); + } + } else { + System.out.println( + "Not all facets updated successfully. localCounter: " + localCounter); + } + } + api.deleteEntity(appUrl, bookEntityName, tempBookID); + } + } + if (!testStatus) + fail("Could not update invalid secondary property in chapter after book is saved"); + } + + @Test + @Order(23) + void testDraftUpdateUploadTwoDeleteOneAndCreateInChapter() throws IOException { + System.out.println("Test (23): Upload to all chapter facets, delete one, and save book"); + + boolean testStatus = false; + + // Reuse bookID5 and chapterID5 + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + + if (response.equals("Entity in draft mode")) { + ClassLoader classLoader = getClass().getClassLoader(); + + // Use temp files with unique names to avoid duplicate name errors + File originalPdf = + new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + File originalTxt = + new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).getFile()); + + File file1 = File.createTempFile("test23_pdf_", ".pdf"); + File file2 = File.createTempFile("test23_txt_", ".txt"); + Files.copy(originalPdf.toPath(), file1.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.copy(originalTxt.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData1 = new HashMap<>(); + postData1.put("up__ID", chapterID5); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); + + Map postData2 = new HashMap<>(postData1); + postData2.put("up__ID", chapterID5); + postData2.put("mimeType", "text/plain"); + + boolean allCreated = true; + String[] tempID1 = new String[facet.length]; + String[] tempID2 = new String[facet.length]; + + for (int i = 0; i < facet.length; i++) { + List response1 = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData1, file1); + List response2 = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData2, file2); + + if (response1.get(0).equals("Attachment created") + && response2.get(0).equals("Attachment created")) { + tempID1[i] = response1.get(1); // to keep one + tempID2[i] = response2.get(1); // will delete this one + } else { + System.out.println("Failed to create attachments for facet " + facet[i]); + System.out.println("Response 1: " + response1.get(0)); + System.out.println("Response 2: " + response2.get(0)); + allCreated = false; + break; + } + + String deleteResponse = + api.deleteAttachment(appUrl, chapterEntityName, facet[i], chapterID5, tempID2[i]); + if (!"Deleted".equals(deleteResponse)) { + allCreated = false; + break; + } + } + + file1.delete(); + file2.delete(); + + if (allCreated) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } else { + System.out.println("Could not edit book: " + response); + } + + if (!testStatus) { + fail("Failed to upload multiple chapter facet entries, delete one per facet and save book"); + } + } + + @Test + @Order(24) + void testUpdateChapterEntityDraft() throws IOException { + System.out.println("Test (24): Update chapter in book draft with new facet content"); + boolean testStatus = false; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + // Use unique temp file name to avoid duplicates + File tempFile = File.createTempFile("test24_sample_", ".pdf"); + Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", chapterID5); + 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, bookEntityName, srvpath, bookID5); + if (response.equals("Entity in draft mode")) { + boolean allCreated = true; + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, srvpath, postData, tempFile); + String check = facetResponse.get(0); + if (!check.equals("Attachment created")) { + allCreated = false; + System.out.println( + "Attachment creation failed in chapter facet: " + facet[i] + " - " + check); + } + } + + if (allCreated) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } else { + System.out.println("Could not edit book: " + response); + } + + tempFile.delete(); + + if (!testStatus) { + fail("Failed to update chapter entity draft with new attachments"); + } + } + + @Test + @Order(25) + void testUpdateSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + throws IOException { + System.out.println( + "Test (25): Rename & Update secondary properties for multiple chapter attachments after book is saved"); + System.out.println("Creating book and chapter with multiple attachments"); + + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!"Could not create entity".equals(chapterResponse)) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create PDF attachments + postData.put("mimeType", "application/pdf"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + String[] pdfID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + pdfID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create TXT attachments + postData.put("mimeType", "application/txt"); + file = new File(classLoader.getResource("sample.txt").getFile()); + String[] txtID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + txtID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create EXE attachments + postData.put("mimeType", "application/exe"); + file = new File(classLoader.getResource("sample.exe").getFile()); + String[] exeID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + exeID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Save book first + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (!"Saved".equals(response)) { + fail("Could not save book initially"); + } + + // Edit book to update chapter attachments + response = api.editEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (response.equals("Entity in draft mode")) { + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; + + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // Update PDF properties + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String renameResp = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty2\" : " + secondaryPropertyInt + " }"); + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty5\" : \"" + secondaryPropertyDateTime + "\" }"); + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + String upd3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + String upd4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + + if ("Renamed".equals(renameResp) + && "Updated".equals(upd1) + && "Updated".equals(upd2) + && "Updated".equals(upd3) + && "Updated".equals(upd4)) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // Update TXT properties (only boolean) + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + String upd = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + if ("Updated".equals(upd)) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + // Update EXE properties (dropdown and int) + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValueExe = integrationTestUtils.getDropDownValue(); + String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + for (int i = 0; i < facet.length; i++) { + RequestBody bodyDropdownExe = + RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + RequestBody bodyIntExe = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + + if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (response.equals("Saved")) { + System.out.println("Book saved"); + testStatus = true; + System.out.println("Renamed & updated Secondary properties for chapter attachments"); + } + } + } + api.deleteEntity(appUrl, bookEntityName, tempBookID); + } + } + if (!testStatus) { + fail("Could not update secondary property in chapter after book is saved"); + } + } + + @Test + @Order(26) + void testUpdateInvalidSecondaryProperty_beforeBookIsSaved_multipleChapterAttachments() + throws IOException { + System.out.println( + "Test (26): Rename & Update invalid and valid secondary properties for multiple chapter facets before book is saved"); + System.out.println("Creating book and chapter"); + + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (!"Could not create entity".equals(response)) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!"Could not create entity".equals(chapterResponse)) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create PDF attachments + postData.put("mimeType", "application/pdf"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + String[] pdfID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + pdfID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create TXT attachments + postData.put("mimeType", "application/txt"); + file = new File(classLoader.getResource("sample.txt").getFile()); + String[] txtID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + txtID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + // Create EXE attachments + postData.put("mimeType", "application/exe"); + file = new File(classLoader.getResource("sample.exe").getFile()); + String[] exeID = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + exeID[i] = + CreateandReturnFacetID(appUrl, serviceName, tempChapterID, facet[i], postData, file); + } + + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; + + String name1 = "sample1234.pdf"; + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + + // Update PDF properties + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String renameResp = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDropdown); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyInt); + String upd3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyDate); + String upd4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], bodyBool); + String updInvalid = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i], invalidPropertyPDF); + + if ("Renamed".equals(renameResp) + && "Updated".equals(upd1) + && "Updated".equals(upd2) + && "Updated".equals(upd3) + && "Updated".equals(upd4) + && "Updated".equals(updInvalid)) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // Update TXT properties + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + String upd = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i], bodyBool); + if ("Updated".equals(upd)) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + // Update EXE properties + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValueExe = integrationTestUtils.getDropDownValue(); + String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; + + for (int i = 0; i < facet.length; i++) { + RequestBody bodyDropdownExe = + RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + RequestBody bodyIntExe = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyDropdownExe); + String upd2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i], bodyIntExe); + + if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + + if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; + + // Verify PDF metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, pdfID[i]); + assertEquals(expectedNames[0], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertNull(metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } + + // Verify TXT metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, txtID[i]); + assertEquals(expectedNames[1], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertTrue((Boolean) metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } + + // Verify EXE metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], tempChapterID, exeID[i]); + assertEquals(expectedNames[2], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertEquals(dropdownValueExe, metadata.get("customProperty1_code")); + assertEquals(1234, metadata.get("customProperty2")); + } + + // Parse JSON response and check for expected error messages + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved with expected invalid property errors"); + testStatus = true; + System.out.println( + "Rename & update unsuccessful for invalid properties and successful for valid attachments"); + } + } + } + } + + if (!testStatus) { + fail("Could not update secondary property before book is saved"); + } + } + + @Test + @Order(27) + void testUpdateInvalidSecondaryProperty_afterBookIsSaved_multipleChapterAttachments() + throws IOException { + System.out.println( + "Test (27): Rename & Update invalid and valid secondary properties for multiple chapter attachments after book is saved"); + + // Reuse bookID5 and chapterID5 + System.out.println("Editing book with bookID5: " + bookID5); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + System.out.println("Edit entity response: " + response); + + if (response.equals("Entity in draft mode")) { + // Fetch existing attachments from the chapter + List> attachmentsMeta = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], chapterID5); + List> referencesMeta = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], chapterID5); + List> footnotesMeta = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[2], chapterID5); + + System.out.println("Attachments count: " + attachmentsMeta.size()); + System.out.println("References count: " + referencesMeta.size()); + System.out.println("Footnotes count: " + footnotesMeta.size()); + + if (attachmentsMeta.size() >= 3 && referencesMeta.size() >= 3 && footnotesMeta.size() >= 3) { + String[] pdfID = new String[facet.length]; + String[] txtID = new String[facet.length]; + String[] exeID = new String[facet.length]; + + pdfID[0] = (String) attachmentsMeta.get(0).get("ID"); + pdfID[1] = (String) referencesMeta.get(0).get("ID"); + pdfID[2] = (String) footnotesMeta.get(0).get("ID"); + + txtID[0] = (String) attachmentsMeta.get(1).get("ID"); + txtID[1] = (String) referencesMeta.get(1).get("ID"); + txtID[2] = (String) footnotesMeta.get(1).get("ID"); + + exeID[0] = (String) attachmentsMeta.get(2).get("ID"); + exeID[1] = (String) referencesMeta.get(2).get("ID"); + exeID[2] = (String) footnotesMeta.get(2).get("ID"); + + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; + + String name1 = "sample.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // PDF + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDropdown); + + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyInt); + + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyDate); + + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], bodyBool); + + String updateSecondaryPropertyResponse5 = + api.updateInvalidSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, pdfID[i], invalidPropertyPDF); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated") + && updateSecondaryPropertyResponse5.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); + String updateSecondaryPropertyResponseTXT1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, txtID[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + Integer secondaryPropertyInt3 = 12; + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + for (int i = 0; i < facet.length; i++) { + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyDropdown1); + + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[i], chapterID5, exeID[i], bodyInt); + + if (updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, bookID5); + // Note: Don't verify specific filenames since previous tests may have changed them + System.out.println("Save response: " + response); + + // Parse JSON response to check for invalid secondary property errors in all three tables + ObjectMapper mapper = new ObjectMapper(); + JsonNode root = mapper.readTree(response); + boolean hasAttachmentsError = false; + boolean hasReferencesError = false; + boolean hasFootnotesError = false; + if (root.isArray()) { + for (JsonNode node : root) { + String message = node.path("message").asText(); + if (message.contains("id1") && message.contains("Table: attachments")) { + hasAttachmentsError = true; + } + if (message.contains("id1") && message.contains("Table: references")) { + hasReferencesError = true; + } + if (message.contains("id1") && message.contains("Table: footnotes")) { + hasFootnotesError = true; + } + } + } + if (hasAttachmentsError && hasReferencesError && hasFootnotesError) { + System.out.println("Book saved"); + testStatus = true; + System.out.println( + "Rename & update unsuccessful for invalid Secondary properties and successful for valid property attachments"); + } else { + System.out.println("Save response did not match expected: " + response); + } + } else { + System.out.println("Not enough attachments in facets - need at least 3 per facet"); + } + } + } else { + System.out.println( + "Could not edit book - it may be stuck in draft mode from a previous test"); + } + if (!testStatus) { + fail("Could not update secondary property before book is saved"); + } + } + + // // Tests 28 and 29 removed - chapters have no attachment limit + + // // Tests 28-29 skipped - chapters have no attachment limit + + @Test + @Order(30) + void testDiscardBookDraftWithoutChapterAttachments() { + System.out.println("Test (30) : Discard book draft without adding chapter attachments"); + Boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!chapterResponse.equals("Could not create entity")) { + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if (response.equals("Entity Draft Deleted")) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft with chapter was not discarded properly"); + } + } + + @Test + @Order(31) + void testDiscardBookDraftWithChapterAttachments() throws IOException { + System.out.println("Test (31): Discard book draft with chapter attachments"); + boolean testStatus = false; + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (!"Could not create entity".equals(chapterResponse)) { + String tempChapterID = chapterResponse; + + ClassLoader classLoader = getClass().getClassLoader(); + File file = + new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", tempChapterID); + 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 < facet.length; i++) { + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); + if ("Attachment created".equals(createResponse.get(0))) { + System.out.println("Attachment created in chapter facet: " + facet[i]); + } else { + System.out.println("Attachment creation failed in chapter facet: " + facet[i]); + } + } + + response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Book draft with chapter attachments was not discarded properly"); + } + } + + // // Tests 32-34 covered in tests 19, 23, 24 + // // Tests 37-41 skipped - copy with notes/secondary properties not applicable + + @Test + @Order(42) + void testCreateLinkSuccessInChapter() throws IOException { + System.out.println("Test (42): Create link in chapter"); + List attachments = new ArrayList<>(); + + // Create book and chapter for link testing + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create book"); + } + String createLinkBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + if (chapterResponse.equals("Could not create entity")) { + fail("Could not create chapter"); + } + String createLinkChapterID = chapterResponse; + + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + for (String facetName : facet) { + String createLinkResponse1 = + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + String createLinkResponse2 = + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", linkUrl); + if (!createLinkResponse1.equals("Link created successfully") + || !createLinkResponse2.equals("Link created successfully")) { + fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); + } + } + + String saveEntityResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save book"); + } + + for (String facetName : facet) { + attachments = + api + .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) + .stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String openAttachmentResponse; + for (String attachment : attachments) { + openAttachmentResponse = + api.openAttachment( + appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open created link in chapter facet : " + facetName); + } + } + } + api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + } + + @Test + @Order(43) + void testCreateLinkDifferentChapter() throws IOException { + System.out.println("Test (43): Create link with same name in different chapter"); + + // Create new book and chapter + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (response.equals("Could not edit entity")) { + fail("Could not create book"); + } + String tempBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); + if (chapterResponse.equals("Could not create entity")) { + fail("Could not create chapter"); + } + String tempChapterID = chapterResponse; + + String linkName = "sample"; + String linkUrl = "https://example.com"; + for (String facetName : facet) { + String createResponse = + api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); + if (!createResponse.equals("Link created successfully")) { + fail("Could not create link in different chapter with same name"); + } + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); + if (!response.equals("Saved")) { + fail("Could not save book"); + } + + response = api.deleteEntity(appUrl, bookEntityName, tempBookID); + if (!response.equals("Entity Deleted")) { + fail("Could not delete book"); + } + } + + @Test + @Order(44) + void testCreateLinkFailureInChapter() throws IOException { + System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if ("Could not create entity".equals(response)) { + fail("Could not create book"); + } + String createLinkBookID = response; + + response = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); + if ("Could not create entity".equals(response)) { + fail("Could not create chapter"); + } + String createLinkChapterID = response; + + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + + ObjectMapper mapper = new ObjectMapper(); + + for (String facetName : facet) { + + // Create initial link for this facet first (so duplicate test works) + response = + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + if (!"Link created successfully".equals(response)) { + fail("Could not create initial link for facet: " + facetName); + } + + /* ---------- INVALID URL ---------- */ + try { + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); + fail("Expected invalid URL error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + assertEquals("400018", error.path("code").asText()); + assertTrue( + error.path("message").asText().contains("expected pattern"), + "Unexpected message: " + error.path("message").asText()); + } + + /* ---------- INVALID NAME ---------- */ + try { + api.createLink( + appUrl, + chapterEntityName, + facetName, + createLinkChapterID, + "sample//", + "https://example.com"); + fail("Expected invalid name error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); + + assertEquals("500", error.path("code").asText()); + assertTrue( + message.contains("contains unsupported characters") + && message.contains("Rename and try again"), + "Unexpected message: " + message); + } + + /* ---------- EMPTY NAME & URL ---------- */ + try { + api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); + fail("Expected missing value error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + assertEquals("409008", error.path("code").asText()); + assertEquals("Provide the missing value.", error.path("message").asText()); + } + + /* ---------- DUPLICATE NAME ---------- */ + try { + api.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); + fail("Expected duplicate name error"); + } catch (IOException e) { + JsonNode error = + mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); + + assertEquals("500", error.path("code").asText()); + assertEquals( + "An object named \"sample\" already exists. Rename the object and try again.", + error.path("message").asText()); + } + } + + response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); + if (!"Saved".equals(response)) { + fail("Could not save book"); + } + + response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); + if (!"Entity Deleted".equals(response)) { + fail("Could not delete book"); + } + } + + @Test + @Order(45) + void testCreateLinkNoSDMRolesInChapter() throws IOException { + System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); + + String createLinkBookNoRoles = + apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (createLinkBookNoRoles.equals("Could not edit entity")) { + fail("Could not create book"); + } + + String createLinkChapterNoRoles = + apiNoRoles.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); + if (createLinkChapterNoRoles.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + for (String facetName : facet) { + String linkName = "sample27"; + String linkUrl = "https://example.com"; + try { + apiNoRoles.createLink( + appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); + if (!response.equals("Saved")) { + fail("Could not save book"); + } + + response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); + if (!response.equals("Entity Deleted")) { + fail("Could not delete book"); + } + } + + @Test + @Order(46) + void testDeleteLinkInChapter() throws IOException { + System.out.println("Test (46): Delete link in chapter"); + List> attachments = new ArrayList<>(); + + String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create book"); + } + String deleteLinkBookID = response; + + String chapterResponse = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); + if (chapterResponse.equals("Could not create entity")) { + fail("Could not create chapter"); + } + String deleteLinkChapterID = chapterResponse; + + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink( + appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for chapter facet : " + facetName); + } + } + + String saveEntityResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save book"); + } + + for (String facetName : facet) { + attachments.add( + api + .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + .stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } + + String editEntityResponse = + api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + int index = 0; + for (String facetName : facet) { + String deleteLinkResponse = + api.deleteAttachment( + appUrl, + chapterEntityName, + facetName, + deleteLinkChapterID, + attachments.get(index).get(0)); + System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + if (!deleteLinkResponse.equals("Deleted")) { + fail("Could not delete created link"); + } + index += 1; + } + + saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save book"); + } + + index = 0; + attachments.clear(); + for (String facetName : facet) { + attachments.add( + api + .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) + .stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + System.out.println( + "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + if (attachments.get(index).size() != 0) { + fail("Link wasn't deleted"); + } + index += 1; + } + + response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); + if (!response.equals("Entity Deleted")) { + fail("Could not delete book"); + } + } + + @Test + @Order(35) + void testCopyAttachmentsToNewChapterInSameBook() throws IOException { + System.out.println( + "Test (35): Copy attachments from one chapter to another new chapter in the same book"); + + // Create source book and chapter with attachments + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Load original files for copying content + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(new ArrayList<>()); + } + + // Create attachments in all facets - each upload needs a unique filename + int fileCounter = 0; + for (int i = 0; i < facet.length; i++) { + boolean useTxt = (i == 1); // Use txt for references facet + postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + File originalFile = useTxt ? originalTxt : originalPdf; + String extension = useTxt ? ".txt" : ".pdf"; + + for (int j = 0; j < 2; j++) { // Create 2 attachments per facet + // Create unique temp file for EACH upload to avoid duplicate filename errors + fileCounter++; + File tempFile = + File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + System.out.println("Created attachment ID: " + createResponse.get(1)); + } else { + System.out.println("Failed to create attachment: " + createResponse.get(0)); + fail("Could not create attachment in facet: " + facet[i]); + } + } + } + + // Fetch object IDs from source attachments + List objectIds = new ArrayList<>(); + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + if (metadata.containsKey("objectId")) { + objectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + } + + // Save the source book + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Create target chapter in the SAME book + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source book for adding target chapter"); + } + + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (targetChapterID.equals("Could not create entity")) { + fail("Could not create target chapter in same book"); + } + + // Copy attachments to target chapter + int objectIdIndex = 0; + for (String facetName : facet) { + List facetObjectIds = + objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, facetObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + } + + // Fetch and wait for copied attachments + List> copiedMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + for (Map meta : copiedMetadata) { + String copiedId = (String) meta.get("ID"); + } + objectIdIndex += 2; + } + + // Save the book with new chapter + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book after copying attachments"); + } + + // Verify attachments were copied - read them + for (String facetName : facet) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.size() != 2) { + fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); + } + for (Map meta : targetMetadata) { + String attachmentId = (String) meta.get("ID"); + String readResponse = + api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment in facet: " + facetName); + } + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + } + + @Test + @Order(36) + void testCopyAttachmentsToChapterInDifferentBook() throws IOException { + System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); + + // Create Book1 with source chapter and attachments + copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create Book2 with target chapter + copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Load original files for copying content + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(new ArrayList<>()); + } + + // Create attachments in all facets of source chapter - each upload needs unique filename + int fileCounter = 0; + for (int i = 0; i < facet.length; i++) { + boolean useTxt = (i == 1); // Use txt for references facet + postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); + File originalFile = useTxt ? originalTxt : originalPdf; + String extension = useTxt ? ".txt" : ".pdf"; + + for (int j = 0; j < 2; j++) { + // Create unique temp file for EACH upload to avoid duplicate filename errors + fileCounter++; + File tempFile = + File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + System.out.println("Created attachment ID: " + createResponse.get(1)); + } else { + System.out.println("Failed to create attachment: " + createResponse.get(0)); + fail("Could not create attachment in facet: " + facet[i]); + } + } + } + + // Fetch object IDs + sourceObjectIds.clear(); + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + } + + // Save Book1 + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Copy attachments from Book1's chapter to Book2's chapter + if (sourceObjectIds.size() == 6) { + int objectIdIndex = 0; + for (String facetName : facet) { + List facetObjectIds = + sourceObjectIds.subList( + objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, facetObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); + } + + objectIdIndex += 2; + } + + // Save Book2 + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book after copying attachments"); + } + + // Verify attachments were copied + for (String facetName : facet) { + List> targetMetadata = + api.fetchEntityMetadata( + appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); + if (targetMetadata.size() != 2) { + fail("Expected 2 attachments in facet " + facetName + ", found " + targetMetadata.size()); + } + for (Map meta : targetMetadata) { + String attachmentId = (String) meta.get("ID"); + String readResponse = + api.readAttachment( + appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, attachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment in facet: " + facetName); + } + } + } + + // Cleanup - delete both books after verification + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + } else { + fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); + } + } + + @Test + @Order(37) + void testCopyAttachmentsWithNotePreserved() throws IOException { + System.out.println("Test (37): Copy attachments with note field preserved"); + + // Create source book and chapter + copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create attachments with notes in source chapter + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] sourceAttachmentIds = new String[facet.length]; + String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); + + for (int i = 0; i < facet.length; i++) { + // Create unique temp file for each facet + File tempFile = + File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + sourceAttachmentIds[i] = createResponse.get(1); + + // Update note field using RequestBody + String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + String noteResponse = + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + noteBody); + if (!noteResponse.equals("Updated")) { + fail("Could not update note for attachment in facet: " + facet[i]); + } + System.out.println("Note updated for facet: " + facet[i]); + } + + // Save source book + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Verify notes were saved in source + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + if (!testNote.equals(metadata.get("note"))) { + fail("Note not saved correctly in source for facet: " + facet[i]); + } + } + + // Create target book and chapter + copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Get object IDs and copy attachments + for (int i = 0; i < facet.length; i++) { + Map sourceMetadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + String objectId = sourceMetadata.get("objectId").toString(); + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + System.out.println("Attachment copied to facet: " + facet[i]); + } + + // Save target book + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify notes were preserved in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); + if (targetMetadata.isEmpty()) { + fail("No attachments found in target facet: " + facet[i]); + } + Map copiedAttachment = targetMetadata.get(0); + String copiedNote = (String) copiedAttachment.get("note"); + if (!testNote.equals(copiedNote)) { + fail( + "Note not preserved after copy in facet: " + + facet[i] + + ". Expected: " + + testNote + + ", Got: " + + copiedNote); + } + System.out.println("Note preserved in target facet: " + facet[i]); + } + + System.out.println("Test 37 passed - notes preserved during copy"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + copyAttachmentSourceBook = null; + copyAttachmentTargetBook = null; + } + + @Test + @Order(38) + void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { + System.out.println("Test (38): Copy attachments with secondary properties preserved"); + + // Use entities from test 37 or create new ones if needed + boolean sourceBookJustCreated = false; + boolean targetBookJustCreated = false; + + if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + copyAttachmentSourceBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + sourceBookJustCreated = true; + } + + if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + copyAttachmentTargetBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + targetBookJustCreated = true; + } + + // If source book was just created, save it first before we can edit it + if (sourceBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created source book"); + } + } + + // If target book was just created, save it first + if (targetBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created target book"); + } + } + + // Edit source book + String editResponse = + api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source book"); + } + + // Create new attachments with secondary properties + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] sourceAttachmentIds = new String[facet.length]; + Boolean testBooleanProp = true; + Integer testIntegerProp = 12345; + + for (int i = 0; i < facet.length; i++) { + // Create unique temp file + File tempFile = + File.createTempFile( + "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + sourceAttachmentIds[i] = createResponse.get(1); + + // Update secondary properties using RequestBody (customProperty6 - Boolean, customProperty2 - + // Integer) + String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + String boolResponse = + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + boolBody); + if (!boolResponse.equals("Updated")) { + System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); + } + + String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + String intResponse = + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + intBody); + if (!intResponse.equals("Updated")) { + System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); + } + + System.out.println("Secondary properties updated for facet: " + facet[i]); + } + + // Save source book + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Edit target book + editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + // Copy attachments to target + for (int i = 0; i < facet.length; i++) { + Map sourceMetadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + String objectId = sourceMetadata.get("objectId").toString(); + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); + } + + // Save target book + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify secondary properties were preserved in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); + + // Find the attachment we just copied (most recent one) + boolean found = false; + for (Map attachment : targetMetadata) { + Object boolProp = attachment.get("customProperty6"); + Object intProp = attachment.get("customProperty2"); + + if (boolProp != null && intProp != null) { + if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { + found = true; + System.out.println("Secondary properties preserved in target facet: " + facet[i]); + break; + } + } + } + if (!found) { + System.out.println( + "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); + } + } + + System.out.println("Test 38 passed - secondary properties checked during copy"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + copyAttachmentSourceBook = null; + copyAttachmentTargetBook = null; + } + + @Test + @Order(39) + void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { + System.out.println( + "Test (39): Copy attachments with both note and secondary properties preserved"); + + // Use entities from previous tests or create new ones + boolean sourceBookJustCreated = false; + boolean targetBookJustCreated = false; + + if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { + copyAttachmentSourceBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentSourceBook.equals("Could not create entity")) { + fail("Could not create source book"); + } + copyAttachmentSourceChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); + if (copyAttachmentSourceChapter.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + sourceBookJustCreated = true; + } + + if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { + copyAttachmentTargetBook = + api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (copyAttachmentTargetBook.equals("Could not create entity")) { + fail("Could not create target book"); + } + copyAttachmentTargetChapter = + api.createEntityDraft( + appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); + if (copyAttachmentTargetChapter.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + targetBookJustCreated = true; + } + + // If source book was just created, save it first before we can edit it + if (sourceBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created source book"); + } + } + + // If target book was just created, save it first + if (targetBookJustCreated) { + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save newly created target book"); + } + } + + // Edit source book + String editResponse = + api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source book"); + } + + // Create new attachments with both note and secondary properties + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", copyAttachmentSourceChapter); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String[] sourceAttachmentIds = new String[facet.length]; + String testNote = "Combined test note - " + System.currentTimeMillis(); + Boolean testBooleanProp = true; + Integer testIntegerProp = 99999; + + for (int i = 0; i < facet.length; i++) { + // Create unique temp file + File tempFile = + File.createTempFile( + "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + List createResponse = + api.createAttachment( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + srvpath, + postData, + tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + sourceAttachmentIds[i] = createResponse.get(1); + + // Update note using RequestBody + String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; + RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + noteBody); + + // Update secondary properties using RequestBody + String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; + RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + boolBody); + + String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; + RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); + api.updateSecondaryProperty( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i], + intBody); + + System.out.println("Note and secondary properties updated for facet: " + facet[i]); + } + + // Save source book + String saveResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Edit target book + editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + // Copy attachments to target + for (int i = 0; i < facet.length; i++) { + Map sourceMetadata = + api.fetchMetadata( + appUrl, + chapterEntityName, + facet[i], + copyAttachmentSourceChapter, + sourceAttachmentIds[i]); + String objectId = sourceMetadata.get("objectId").toString(); + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + + String copyResponse = + api.copyAttachment( + appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + System.out.println("Attachment with note and properties copied to facet: " + facet[i]); + } + + // Save target book + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify note and secondary properties were preserved in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter); + + boolean noteFound = false; + boolean propsFound = false; + + for (Map attachment : targetMetadata) { + String copiedNote = (String) attachment.get("note"); + Object boolProp = attachment.get("customProperty6"); + Object intProp = attachment.get("customProperty2"); + + if (testNote.equals(copiedNote)) { + noteFound = true; + System.out.println("Note preserved in target facet: " + facet[i]); + } + + if (boolProp != null && intProp != null) { + if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { + propsFound = true; + System.out.println("Secondary properties preserved in target facet: " + facet[i]); + } + } + } + + if (!noteFound) { + System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); + } + if (!propsFound) { + System.out.println( + "Warning: Secondary properties may not be preserved in facet: " + facet[i]); + } + } + + // Cleanup - delete both books + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + + // Reset static variables + copyAttachmentSourceBook = null; + copyAttachmentTargetBook = null; + copyAttachmentSourceChapter = null; + copyAttachmentTargetChapter = null; + + System.out.println("Test 39 passed - both note and secondary properties checked during copy"); + } + + @Test + @Order(40) + void testCopyAttachmentsWithInvalidObjectId() throws IOException { + System.out.println("Test (40): Copy attachments with invalid object ID should fail"); + + // Create independent test entities (don't rely on previous tests) + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create test book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create test chapter"); + } + + // Save the book first so it's not in draft mode + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save test book"); + } + + // Now edit it to test copy with invalid object IDs + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit test book"); + } + + // Try to copy with invalid object ID + for (String facetName : facet) { + try { + List invalidObjectIds = new ArrayList<>(); + invalidObjectIds.add("invalidObjectId123"); + invalidObjectIds.add("anotherInvalidId456"); + api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, invalidObjectIds); + fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); + } catch (IOException e) { + // Expected - copy should fail with invalid object ID + System.out.println( + "Expected error received for invalid object ID in facet " + + facetName + + ": " + + e.getMessage()); + } + } + + // Save and cleanup + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + + // Also cleanup test 36 entities if they exist + if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { + try { + api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); + } catch (Exception e) { + // Ignore - may already be deleted + } + } + if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { + try { + api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); + } catch (Exception e) { + // Ignore - may already be deleted + } + } + } + + @Test + @Order(41) + void testCopyAttachmentsToExistingChapter() throws IOException { + System.out.println( + "Test (41): Copy attachments to an existing chapter that already has attachments"); + + // Create Book1 with source chapter + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create Book2 with target chapter that has existing attachments + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (targetBookID.equals("Could not create entity")) { + fail("Could not create target book"); + } + + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + if (targetChapterID.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Create temp files with unique names to avoid duplicate filename errors + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test41_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create attachment in source chapter + List sourceObjIds = new ArrayList<>(); + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create source attachment"); + } + String attachmentId = createResponse.get(1); + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + sourceObjIds.add(metadata.get("objectId").toString()); + } + + // Create existing attachment in target chapter + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", targetChapterID); + postData.put("mimeType", "text/plain"); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create existing target attachment"); + } + String attachmentId = createResponse.get(1); + } + + // Save both books + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Edit target book and copy attachments + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + // Copy from source to target (target already has 1 attachment per facet) + for (int i = 0; i < facet.length; i++) { + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjIds.get(i)); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, objectIdsToCopy); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to facet: " + facet[i]); + } + } + + // Save target book + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) + for (String facetName : facet) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.size() != 2) { + fail( + "Expected 2 attachments in facet " + + facetName + + " (1 existing + 1 copied), found " + + targetMetadata.size()); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + // // ============= LINK RENAME TESTS (47-49) ============= + + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (47): Rename link in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links in all facets + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit and rename links + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + String renameResponse = + api.renameAttachment( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); + if (!renameResponse.equals("Renamed")) { + fail("Could not rename link in facet: " + facetName); + } + } + + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book after renaming links"); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(48) + void testRenameLinkDuplicate() throws IOException { + System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create two links in all facets + for (String facetName : facet) { + String createLinkResponse1 = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "link1", + "https://www.example1.com"); + String createLinkResponse2 = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "link2", + "https://www.example2.com"); + if (!createLinkResponse1.equals("Link created successfully") + || !createLinkResponse2.equals("Link created successfully")) { + fail("Could not create links in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit and try to rename link2 to link1 (duplicate) + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.size() < 2) { + fail("Expected 2 links in facet: " + facetName); + } + + // Find link2 and rename to link1 + for (Map attachment : attachments) { + if ("link2".equals(attachment.get("fileName"))) { + String linkId = (String) attachment.get("ID"); + api.renameAttachment( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); + break; + } + } + } + + // Save should fail with duplicate error + String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + ObjectMapper mapper = new ObjectMapper(); + try { + JsonNode errorJson = mapper.readTree(saveError); + String errorMessage = errorJson.path("error").path("message").asText(); + if (!errorMessage.contains("already exists")) { + fail("Expected duplicate error but got: " + saveError); + } + } catch (Exception e) { + if (!saveError.contains("already exists")) { + fail("Expected duplicate error but got: " + saveError); + } + } + + // Cleanup + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(49) + void testRenameLinkUnsupportedCharacters() throws IOException { + System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links in all facets + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit and rename with unsupported characters + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + api.renameAttachment( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); + } + + // Save should fail with unsupported characters error + String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveError.contains("unsupported characters")) { + fail("Expected unsupported characters error but got: " + saveError); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + // // ============= LINK EDIT TESTS (50-53) ============= + + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (50): Edit existing link URL in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links in all facets + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit links + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + String editLinkResponse = + api.editLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + linkId, + "https://www.editedexample.com"); + if (!editLinkResponse.equals("Link edited successfully")) { + fail("Could not edit link in facet: " + facetName); + } + } + + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book after editing links"); + } + + // Verify links open successfully + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + for (Map attachment : attachments) { + String linkId = (String) attachment.get("ID"); + String openResponse = + api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); + if (!openResponse.equals("Attachment opened successfully")) { + fail("Could not open edited link in facet: " + facetName); + } + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (51): Edit link with invalid URL fails in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create links + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + try { + api.editLink( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://editedexample"); + fail("Edit link should have failed with invalid URL in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected error received for invalid URL in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(52) + void testEditLinkFailureEmptyURL() throws IOException { + System.out.println("Test (52): Edit link with empty URL fails in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + try { + api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); + fail("Edit link should have failed with empty URL in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected error received for empty URL in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + for (String facetName : facet) { + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "sample", + "https://www.example.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + for (String facetName : facet) { + List> attachments = + apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + if (attachments.isEmpty()) { + fail("No links found in facet: " + facetName); + } + + String linkId = (String) attachments.get(0).get("ID"); + try { + apiNoRoles.editLink( + appUrl, chapterEntityName, facetName, testChapterID, linkId, "https://www.edited.com"); + fail("Edit link should have failed without SDM roles in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected permission error received in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + // // ============= COPY LINK TESTS (54-58) ============= + + @Test + @Order(54) + void testCopyLinkSuccessNewChapter() throws IOException { + System.out.println("Test (54): Copy link from one chapter to another new chapter"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + if (sourceChapterID.equals("Could not create entity") + || targetChapterID.equals("Could not create entity")) { + fail("Could not create chapters"); + } + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + // Create links in source chapter + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch object IDs + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + // Copy links to target chapter + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link for facet " + facetName + ": " + copyResponse); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify link type and URL + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.isEmpty()) { + fail("No links found in target chapter for facet: " + facetName); + } + + Map copiedLink = targetMetadata.get(0); + String receivedUrl = (String) copiedLink.get("linkUrl"); + assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); + + objectIdIndex++; + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(55) + void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { + System.out.println("Test (55): Copy invalid link object ID to chapter fails"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + for (String facetName : facet) { + try { + List invalidObjectIds = new ArrayList<>(); + invalidObjectIds.add("incorrectObjectId"); + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, invalidObjectIds); + fail("Copy should have thrown error for invalid object ID in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected error received for invalid object ID in facet " + facetName); + } + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(56) + void testCopyLinkToExistingChapter() throws IOException { + System.out.println("Test (56): Copy link to existing chapter that has attachments"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + // Create links in source chapter + for (int i = 0; i < facet.length; i++) { + String linkName = "sourceLink" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in source chapter for facet: " + facet[i]); + } + } + + // Create existing links in target chapter + for (int i = 0; i < facet.length; i++) { + String linkName = "existingLink" + i; + String createLinkResponse = + api.createLink( + appUrl, + chapterEntityName, + facet[i], + targetChapterID, + linkName, + "https://www.existing.com"); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create existing link in target chapter for facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch source object IDs + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + // Copy links to target chapter + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link for facet " + facetName); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify target has 2 links (existing + copied) + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.size() != 2) { + fail( + "Expected 2 links in target chapter facet " + + facetName + + ", found " + + targetMetadata.size()); + } + + objectIdIndex++; + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(57) + void testCopyLinkNoSDMRoles() throws IOException { + System.out.println("Test (57): Copy link fails due to no SDM roles"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch object IDs + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + // Try to copy with no SDM roles + int objectIdIndex = 0; + for (String facetName : facet) { + try { + // Use normal api to put book in draft mode + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + // Use apiNoRoles to attempt copy (should fail) + apiNoRoles.copyAttachment( + appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + fail("Copy should have failed without SDM roles in facet: " + facetName); + } catch (IOException e) { + System.out.println("Expected permission error in facet " + facetName); + // Discard draft to clean up for next iteration + api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); + } + objectIdIndex++; + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(58) + void testCopyLinkFromDraftChapter() throws IOException { + System.out.println("Test (58): Copy link from draft chapter to another chapter"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + String linkUrl = "https://www.example.com"; + List linkObjectIds = new ArrayList<>(); + + // Create links in source chapter (NOT saved yet - draft mode) + for (int i = 0; i < facet.length; i++) { + String linkName = "draftLink" + i; + String createLinkResponse = + api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + + // Save target book only + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch object IDs from draft + for (int i = 0; i < facet.length; i++) { + List> metadata = + api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); + for (Map meta : metadata) { + if (meta.containsKey("objectId")) { + linkObjectIds.add(meta.get("objectId").toString()); + } + } + } + + if (linkObjectIds.size() != facet.length) { + fail("Could not fetch all object IDs from draft"); + } + + // Copy links from draft to target + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link from draft for facet " + facetName); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify link was copied + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.isEmpty()) { + fail("No links found in target chapter for facet: " + facetName); + } + + objectIdIndex++; + } + + // Cleanup + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + // // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= + + @Test + @Order(59) + void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { + System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + if (sourceChapterID.equals("Could not create entity") + || targetChapterID.equals("Could not create entity")) { + fail("Could not create chapters"); + } + + // Create temp files with unique names + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test59_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List sourceObjectIds = new ArrayList<>(); + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(new ArrayList<>()); + } + + // Create attachments in source chapter (still in draft) + for (int i = 0; i < facet.length; i++) { + postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); + File file = i == 1 ? tempTxt : tempPdf; + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment in facet: " + facet[i]); + } + } + + // Fetch object IDs from draft + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + Map metadata = + api.fetchMetadataDraft( + appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + } + + // Save target book only + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Copy attachments from draft to target + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment from draft for facet " + facetName); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book"); + } + + // Verify attachment was copied + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); + if (targetMetadata.isEmpty()) { + fail("No attachments found in target chapter for facet: " + facetName); + } + + // Read attachment to verify + String attachmentId = (String) targetMetadata.get(0).get("ID"); + String readResponse = + api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, attachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment in facet: " + facetName); + } + + objectIdIndex++; + } + + // Cleanup + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + // // ============= CHANGELOG TESTS (60-64) ============= + + @Test + @Order(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println("Test (60): View changelog for newly created attachment in chapter"); + + for (int i = 0; i < facet.length; i++) { + String facetName = facet[i]; + + // Create book and chapter + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = + File.createTempFile( + "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } + + String attachmentId = createResponse.get(1); + + // Fetch changelog + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); + + // Cleanup + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + } + } + + @Test + @Order(61) + void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { + System.out.println("Test (61): Changelog after modifying note and custom property in chapter"); + + for (int i = 0; i < facet.length; i++) { + String facetName = facet[i]; + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = + File.createTempFile( + "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Update note + String notesValue = "Test note for changelog verification"; + RequestBody updateNotesBody = + RequestBody.create( + MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); + + // Update custom property + RequestBody bodyInt = + RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 12345}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); + + // Save + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit to fetch changelog + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + // Fetch changelog + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + int numItems = (int) changelogResponse.get("numItems"); + assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + } + + @Test + @Order(62) + void testChangelogAfterRenamingAttachment() throws IOException { + System.out.println("Test (62): Changelog after renaming attachment in chapter"); + + for (int i = 0; i < facet.length; i++) { + String facetName = facet[i]; + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = + File.createTempFile( + "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Rename attachment + String renameResponse = + api.renameAttachment( + appUrl, + chapterEntityName, + facetName, + testChapterID, + attachmentId, + "renamed_file.txt"); + if (!renameResponse.equals("Renamed")) { + fail("Could not rename attachment"); + } + + // Save + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book"); + } + + // Edit to fetch changelog + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit book"); + } + + // Fetch changelog + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + int numItems = (int) changelogResponse.get("numItems"); + assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + } + + @Test + @Order(63) + void testChangelogForCopiedAttachment() throws IOException { + System.out.println("Test (63): Changelog for copied attachment in chapter"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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 in source + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Save both books + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Get object ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + + // Copy to target + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target book"); + } + + List objectIds = new ArrayList<>(); + objectIds.add(objectId); + String copyResponse = + api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); + + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment"); + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Fetch changelog for copied attachment + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); + + editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + Map changelogResponse = + api.fetchChangelog( + appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + int numItems = (int) changelogResponse.get("numItems"); + assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(64) + void testChangelogForNewChapter() throws IOException { + System.out.println("Test (64): Changelog for attachment in newly created chapter"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + fail("Could not create chapter"); + } + + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), ".txt"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalFile.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + + String attachmentId = createResponse.get(1); + + // Fetch changelog before saving + Map changelogResponse = + api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + + assertNotNull(changelogResponse, "Changelog response should not be null"); + assertEquals( + 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); + + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); + + // Cleanup + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + } + + // // ============= MOVE ATTACHMENT TESTS (65-75) ============= + + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println("Test (65): Move attachments from source chapter to target chapter"); + + for (int i = 0; i < facet.length; i++) { + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create temp files + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempPdf.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), + tempTxt.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List sourceAttachmentIds = new ArrayList<>(); + File[] files = {tempPdf, tempTxt}; + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source chapter"); + } + } + + // Save source book + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save source book"); + } + + // Get object IDs and folder ID + List moveObjectIds = new ArrayList<>(); + String sourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (sourceFolderId == null && metadata.containsKey("folderId")) { + sourceFolderId = metadata.get("folderId").toString(); + } + } + } + + // Create target book and chapter + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (targetBookID.equals("Could not create entity")) { + fail("Could not create target book"); + } + + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + if (targetChapterID.equals("Could not create entity")) { + fail("Could not create target chapter"); + } + + // Save target book before moving attachments (moveAttachments requires Active entity) + saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save target book before move"); + } + + // Move attachments to Active entity + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[i], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } + + // Verify + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + assertEquals( + sourceAttachmentIds.size(), + targetMetadata.size(), + "Target should have all attachments after move"); + + List> sourceMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, targetBookID); + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + } + } + + @Test + @Order(66) + void testMoveAttachmentsToChapterWithDuplicate() throws IOException { + System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Create attachment in source with specific name + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, originalPdf); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create source attachment"); + } + String sourceAttachmentId = createResponse.get(1); + + // Create attachment in target with SAME name (duplicate) + postData.put("up__ID", targetChapterID); + createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, originalPdf); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create target attachment"); + } + + // Save both + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Get source object ID and folder ID + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, sourceAttachmentId); + String objectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move to saved target + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Move should handle duplicate - attachment stays in source + + // Verify source still has attachment (duplicate not moved) + List> sourceMetadataAfter = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertTrue( + sourceMetadataAfter.size() >= 1, + "Source should still have attachment when duplicate exists in target"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(67) + void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { + System.out.println("Test (67): Move attachments with notes and secondary properties"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + if (sourceChapterID.equals("Could not create entity")) { + fail("Could not create source chapter"); + } + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // // Tests 28 and 29 removed - chapters have no attachment limit + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Tests 28-29 skipped - chapters have no attachment limit + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + // Add note and secondary property + String testNote = "Test note for move"; + RequestBody noteBody = + RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + "\"}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); + + RequestBody propBody = + RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); + api.updateSecondaryProperty( + appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); + + // Save source + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get object ID and folder ID + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // Create target + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null"); + } - // @Test - // @Order(30) - // void testDiscardBookDraftWithoutChapterAttachments() { - // System.out.println("Test (30) : Discard book draft without adding chapter attachments"); - // Boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!chapterResponse.equals("Could not create entity")) { - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if (response.equals("Entity Draft Deleted")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft with chapter was not discarded properly"); - // } - // } - - // @Test - // @Order(31) - // void testDiscardBookDraftWithChapterAttachments() throws IOException { - // System.out.println("Test (31): Discard book draft with chapter attachments"); - // boolean testStatus = false; - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (!"Could not create entity".equals(chapterResponse)) { - // String tempChapterID = chapterResponse; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = - // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", tempChapterID); - // 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 < facet.length; i++) { - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], tempChapterID, srvpath, postData, file); - // if ("Attachment created".equals(createResponse.get(0))) { - // System.out.println("Attachment created in chapter facet: " + facet[i]); - // } else { - // System.out.println("Attachment creation failed in chapter facet: " + facet[i]); - // } - // } - - // response = api.deleteEntityDraft(appUrl, bookEntityName, tempBookID); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Book draft with chapter attachments was not discarded properly"); - // } - // } + // Verify note was preserved + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + if (!targetMetadata.isEmpty()) { + String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); + Map movedMetadata = + api.fetchMetadata( + appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); + + // Note should be preserved + if (movedMetadata.containsKey("note")) { + assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); + } + } - // // Tests 32-34 covered in tests 19, 23, 24 - // // Tests 37-41 skipped - copy with notes/secondary properties not applicable + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } - // @Test - // @Order(42) - // void testCreateLinkSuccessInChapter() throws IOException { - // System.out.println("Test (42): Create link in chapter"); - // List attachments = new ArrayList<>(); - - // // Create book and chapter for link testing - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (response.equals("Could not create entity")) { - // fail("Could not create book"); - // } - // String createLinkBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - // if (chapterResponse.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - // String createLinkChapterID = chapterResponse; - - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // for (String facetName : facet) { - // String createLinkResponse1 = - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - // String createLinkResponse2 = - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName + "1", - // linkUrl); - // if (!createLinkResponse1.equals("Link created successfully") - // || !createLinkResponse2.equals("Link created successfully")) { - // fail("Could not create links for chapter facet : " + facetName + createLinkResponse1); - // } - // } - - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // for (String facetName : facet) { - // attachments = - // api - // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, createLinkChapterID) - // .stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String openAttachmentResponse; - // for (String attachment : attachments) { - // openAttachmentResponse = - // api.openAttachment( - // appUrl, chapterEntityName, facetName, createLinkChapterID, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open created link in chapter facet : " + facetName); - // } - // } - // } - // api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - // } - - // @Test - // @Order(43) - // void testCreateLinkDifferentChapter() throws IOException { - // System.out.println("Test (43): Create link with same name in different chapter"); - - // // Create new book and chapter - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (response.equals("Could not edit entity")) { - // fail("Could not create book"); - // } - // String tempBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, tempBookID); - // if (chapterResponse.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - // String tempChapterID = chapterResponse; - - // String linkName = "sample"; - // String linkUrl = "https://example.com"; - // for (String facetName : facet) { - // String createResponse = - // api.createLink(appUrl, chapterEntityName, facetName, tempChapterID, linkName, linkUrl); - // if (!createResponse.equals("Link created successfully")) { - // fail("Could not create link in different chapter with same name"); - // } - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, tempBookID); - // if (!response.equals("Saved")) { - // fail("Could not save book"); - // } - - // response = api.deleteEntity(appUrl, bookEntityName, tempBookID); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(44) - // void testCreateLinkFailureInChapter() throws IOException { - // System.out.println("Test (44): Create link fails due to invalid URL and name in chapter"); - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if ("Could not create entity".equals(response)) { - // fail("Could not create book"); - // } - // String createLinkBookID = response; - - // response = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, createLinkBookID); - // if ("Could not create entity".equals(response)) { - // fail("Could not create chapter"); - // } - // String createLinkChapterID = response; - - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - - // ObjectMapper mapper = new ObjectMapper(); - - // for (String facetName : facet) { - - // // Create initial link for this facet first (so duplicate test works) - // response = - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - // if (!"Link created successfully".equals(response)) { - // fail("Could not create initial link for facet: " + facetName); - // } - - // /* ---------- INVALID URL ---------- */ - // try { - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, "example.com"); - // fail("Expected invalid URL error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // assertEquals("400018", error.path("code").asText()); - // assertTrue( - // error.path("message").asText().contains("expected pattern"), - // "Unexpected message: " + error.path("message").asText()); - // } - - // /* ---------- INVALID NAME ---------- */ - // try { - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // createLinkChapterID, - // "sample//", - // "https://example.com"); - // fail("Expected invalid name error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // String message = error.path("message").asText().replace('‘', '\'').replace('’', '\''); - - // assertEquals("500", error.path("code").asText()); - // assertTrue( - // message.contains("contains unsupported characters") - // && message.contains("Rename and try again"), - // "Unexpected message: " + message); - // } - - // /* ---------- EMPTY NAME & URL ---------- */ - // try { - // api.createLink(appUrl, chapterEntityName, facetName, createLinkChapterID, "", ""); - // fail("Expected missing value error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // assertEquals("409008", error.path("code").asText()); - // assertEquals("Provide the missing value.", error.path("message").asText()); - // } - - // /* ---------- DUPLICATE NAME ---------- */ - // try { - // api.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterID, linkName, linkUrl); - // fail("Expected duplicate name error"); - // } catch (IOException e) { - // JsonNode error = - // mapper.readTree(e.getMessage().substring(e.getMessage().indexOf('{'))).path("error"); - - // assertEquals("500", error.path("code").asText()); - // assertEquals( - // "An object named \"sample\" already exists. Rename the object and try again.", - // error.path("message").asText()); - // } - // } - - // response = api.saveEntityDraft(appUrl, bookEntityName, srvpath, createLinkBookID); - // if (!"Saved".equals(response)) { - // fail("Could not save book"); - // } - - // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookID); - // if (!"Entity Deleted".equals(response)) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(45) - // void testCreateLinkNoSDMRolesInChapter() throws IOException { - // System.out.println("Test (45): Create link fails due to no SDM roles assigned in chapter"); - - // String createLinkBookNoRoles = - // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (createLinkBookNoRoles.equals("Could not edit entity")) { - // fail("Could not create book"); - // } - - // String createLinkChapterNoRoles = - // apiNoRoles.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, createLinkBookNoRoles); - // if (createLinkChapterNoRoles.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // for (String facetName : facet) { - // String linkName = "sample27"; - // String linkUrl = "https://example.com"; - // try { - // apiNoRoles.createLink( - // appUrl, chapterEntityName, facetName, createLinkChapterNoRoles, 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, bookEntityName, srvpath, createLinkBookNoRoles); - // if (!response.equals("Saved")) { - // fail("Could not save book"); - // } - - // response = api.deleteEntity(appUrl, bookEntityName, createLinkBookNoRoles); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(46) - // void testDeleteLinkInChapter() throws IOException { - // System.out.println("Test (46): Delete link in chapter"); - // List> attachments = new ArrayList<>(); - - // String response = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (response.equals("Could not create entity")) { - // fail("Could not create book"); - // } - // String deleteLinkBookID = response; - - // String chapterResponse = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, deleteLinkBookID); - // if (chapterResponse.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - // String deleteLinkChapterID = chapterResponse; - - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink( - // appUrl, chapterEntityName, facetName, deleteLinkChapterID, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for chapter facet : " + facetName); - // } - // } - - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // for (String facetName : facet) { - // attachments.add( - // api - // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - // .stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // } - - // String editEntityResponse = - // api.editEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // int index = 0; - // for (String facetName : facet) { - // String deleteLinkResponse = - // api.deleteAttachment( - // appUrl, - // chapterEntityName, - // facetName, - // deleteLinkChapterID, - // attachments.get(index).get(0)); - // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - // if (!deleteLinkResponse.equals("Deleted")) { - // fail("Could not delete created link"); - // } - // index += 1; - // } - - // saveEntityResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, deleteLinkBookID); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // index = 0; - // attachments.clear(); - // for (String facetName : facet) { - // attachments.add( - // api - // .fetchEntityMetadata(appUrl, chapterEntityName, facetName, deleteLinkChapterID) - // .stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // System.out.println( - // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - // if (attachments.get(index).size() != 0) { - // fail("Link wasn't deleted"); - // } - // index += 1; - // } - - // response = api.deleteEntity(appUrl, bookEntityName, deleteLinkBookID); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete book"); - // } - // } - - // @Test - // @Order(35) - // void testCopyAttachmentsToNewChapterInSameBook() throws IOException { - // System.out.println( - // "Test (35): Copy attachments from one chapter to another new chapter in the same book"); - - // // Create source book and chapter with attachments - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Load original files for copying content - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(new ArrayList<>()); - // } - - // // Create attachments in all facets - each upload needs a unique filename - // int fileCounter = 0; - // for (int i = 0; i < facet.length; i++) { - // boolean useTxt = (i == 1); // Use txt for references facet - // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - // File originalFile = useTxt ? originalTxt : originalPdf; - // String extension = useTxt ? ".txt" : ".pdf"; - - // for (int j = 0; j < 2; j++) { // Create 2 attachments per facet - // // Create unique temp file for EACH upload to avoid duplicate filename errors - // fileCounter++; - // File tempFile = - // File.createTempFile("test35_" + facet[i] + "_" + fileCounter + "_", extension); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, - // tempFile); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // System.out.println("Created attachment ID: " + createResponse.get(1)); - // } else { - // System.out.println("Failed to create attachment: " + createResponse.get(0)); - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - // } - - // // Fetch object IDs from source attachments - // List objectIds = new ArrayList<>(); - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - // if (metadata.containsKey("objectId")) { - // objectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } - // } - - // // Save the source book - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Create target chapter in the SAME book - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source book for adding target chapter"); - // } - - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (targetChapterID.equals("Could not create entity")) { - // fail("Could not create target chapter in same book"); - // } - - // // Copy attachments to target chapter - // int objectIdIndex = 0; - // for (String facetName : facet) { - // List facetObjectIds = - // objectIds.subList(objectIdIndex, Math.min(objectIdIndex + 2, objectIds.size())); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // facetObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - // } - - // // Fetch and wait for copied attachments - // List> copiedMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // for (Map meta : copiedMetadata) { - // String copiedId = (String) meta.get("ID"); - // } - // objectIdIndex += 2; - // } - - // // Save the book with new chapter - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book after copying attachments"); - // } - - // // Verify attachments were copied - read them - // for (String facetName : facet) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.size() != 2) { - // fail("Expected 2 attachments in facet " + facetName + ", found " + - // targetMetadata.size()); - // } - // for (Map meta : targetMetadata) { - // String attachmentId = (String) meta.get("ID"); - // String readResponse = - // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // attachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment in facet: " + facetName); - // } - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // } - - // @Test - // @Order(36) - // void testCopyAttachmentsToChapterInDifferentBook() throws IOException { - // System.out.println("Test (36): Copy attachments from chapter in Book1 to chapter in Book2"); - - // // Create Book1 with source chapter and attachments - // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create Book2 with target chapter - // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Load original files for copying content - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(new ArrayList<>()); - // } - - // // Create attachments in all facets of source chapter - each upload needs unique filename - // int fileCounter = 0; - // for (int i = 0; i < facet.length; i++) { - // boolean useTxt = (i == 1); // Use txt for references facet - // postData.put("mimeType", useTxt ? "text/plain" : "application/pdf"); - // File originalFile = useTxt ? originalTxt : originalPdf; - // String extension = useTxt ? ".txt" : ".pdf"; - - // for (int j = 0; j < 2; j++) { - // // Create unique temp file for EACH upload to avoid duplicate filename errors - // fileCounter++; - // File tempFile = - // File.createTempFile("test36_" + facet[i] + "_" + fileCounter + "_", extension); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // System.out.println("Uploading file: " + tempFile.getName() + " to facet: " + facet[i]); - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // System.out.println("Created attachment ID: " + createResponse.get(1)); - // } else { - // System.out.println("Failed to create attachment: " + createResponse.get(0)); - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - // } - - // // Fetch object IDs - // sourceObjectIds.clear(); - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], copyAttachmentSourceChapter, attachment); - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } - // } - - // // Save Book1 - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Copy attachments from Book1's chapter to Book2's chapter - // if (sourceObjectIds.size() == 6) { - // int objectIdIndex = 0; - // for (String facetName : facet) { - // List facetObjectIds = - // sourceObjectIds.subList( - // objectIdIndex, Math.min(objectIdIndex + 2, sourceObjectIds.size())); - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, - // facetObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments to facet: " + facetName + " - " + copyResponse); - // } - - // objectIdIndex += 2; - // } - - // // Save Book2 - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book after copying attachments"); - // } - - // // Verify attachments were copied - // for (String facetName : facet) { - // List> targetMetadata = - // api.fetchEntityMetadata( - // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter); - // if (targetMetadata.size() != 2) { - // fail("Expected 2 attachments in facet " + facetName + ", found " + - // targetMetadata.size()); - // } - // for (Map meta : targetMetadata) { - // String attachmentId = (String) meta.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, chapterEntityName, facetName, copyAttachmentTargetChapter, - // attachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment in facet: " + facetName); - // } - // } - // } - - // // Cleanup - delete both books after verification - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // } else { - // fail("Could not fetch object IDs for all attachments. Found: " + sourceObjectIds.size()); - // } - // } - - // @Test - // @Order(37) - // void testCopyAttachmentsWithNotePreserved() throws IOException { - // System.out.println("Test (37): Copy attachments with note field preserved"); - - // // Create source book and chapter - // copyAttachmentSourceBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create attachments with notes in source chapter - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] sourceAttachmentIds = new String[facet.length]; - // String testNote = "Test note for copy attachment - " + System.currentTimeMillis(); - - // for (int i = 0; i < facet.length; i++) { - // // Create unique temp file for each facet - // File tempFile = - // File.createTempFile("test37_note_" + facet[i] + "_" + System.currentTimeMillis(), - // ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // sourceAttachmentIds[i] = createResponse.get(1); - - // // Update note field using RequestBody - // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - // String noteResponse = - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // noteBody); - // if (!noteResponse.equals("Updated")) { - // fail("Could not update note for attachment in facet: " + facet[i]); - // } - // System.out.println("Note updated for facet: " + facet[i]); - // } - - // // Save source book - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Verify notes were saved in source - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // if (!testNote.equals(metadata.get("note"))) { - // fail("Note not saved correctly in source for facet: " + facet[i]); - // } - // } - - // // Create target book and chapter - // copyAttachmentTargetBook = api.createEntityDraft(appUrl, bookEntityName, entityName2, - // srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Get object IDs and copy attachments - // for (int i = 0; i < facet.length; i++) { - // Map sourceMetadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // String objectId = sourceMetadata.get("objectId").toString(); - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // System.out.println("Attachment copied to facet: " + facet[i]); - // } - - // // Save target book - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify notes were preserved in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], - // copyAttachmentTargetChapter); - // if (targetMetadata.isEmpty()) { - // fail("No attachments found in target facet: " + facet[i]); - // } - // Map copiedAttachment = targetMetadata.get(0); - // String copiedNote = (String) copiedAttachment.get("note"); - // if (!testNote.equals(copiedNote)) { - // fail( - // "Note not preserved after copy in facet: " - // + facet[i] - // + ". Expected: " - // + testNote - // + ", Got: " - // + copiedNote); - // } - // System.out.println("Note preserved in target facet: " + facet[i]); - // } - - // System.out.println("Test 37 passed - notes preserved during copy"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // copyAttachmentSourceBook = null; - // copyAttachmentTargetBook = null; - // } - - // @Test - // @Order(38) - // void testCopyAttachmentsWithSecondaryPropertiesPreserved() throws IOException { - // System.out.println("Test (38): Copy attachments with secondary properties preserved"); - - // // Use entities from test 37 or create new ones if needed - // boolean sourceBookJustCreated = false; - // boolean targetBookJustCreated = false; - - // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - // copyAttachmentSourceBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - // sourceBookJustCreated = true; - // } - - // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - // copyAttachmentTargetBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - // targetBookJustCreated = true; - // } - - // // If source book was just created, save it first before we can edit it - // if (sourceBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created source book"); - // } - // } - - // // If target book was just created, save it first - // if (targetBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created target book"); - // } - // } - - // // Edit source book - // String editResponse = - // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source book"); - // } - - // // Create new attachments with secondary properties - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] sourceAttachmentIds = new String[facet.length]; - // Boolean testBooleanProp = true; - // Integer testIntegerProp = 12345; - - // for (int i = 0; i < facet.length; i++) { - // // Create unique temp file - // File tempFile = - // File.createTempFile( - // "test38_props_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // sourceAttachmentIds[i] = createResponse.get(1); - - // // Update secondary properties using RequestBody (customProperty6 - Boolean, - // customProperty2 - - // // Integer) - // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - // String boolResponse = - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // boolBody); - // if (!boolResponse.equals("Updated")) { - // System.out.println("Warning: Could not update customProperty6 for facet: " + facet[i]); - // } - - // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - // String intResponse = - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // intBody); - // if (!intResponse.equals("Updated")) { - // System.out.println("Warning: Could not update customProperty2 for facet: " + facet[i]); - // } - - // System.out.println("Secondary properties updated for facet: " + facet[i]); - // } - - // // Save source book - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Edit target book - // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // // Copy attachments to target - // for (int i = 0; i < facet.length; i++) { - // Map sourceMetadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // String objectId = sourceMetadata.get("objectId").toString(); - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // System.out.println("Attachment with secondary properties copied to facet: " + facet[i]); - // } - - // // Save target book - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify secondary properties were preserved in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], - // copyAttachmentTargetChapter); - - // // Find the attachment we just copied (most recent one) - // boolean found = false; - // for (Map attachment : targetMetadata) { - // Object boolProp = attachment.get("customProperty6"); - // Object intProp = attachment.get("customProperty2"); - - // if (boolProp != null && intProp != null) { - // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(12345).equals(intProp)) { - // found = true; - // System.out.println("Secondary properties preserved in target facet: " + facet[i]); - // break; - // } - // } - // } - // if (!found) { - // System.out.println( - // "Warning: Secondary properties may not be fully preserved in facet: " + facet[i]); - // } - // } - - // System.out.println("Test 38 passed - secondary properties checked during copy"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // copyAttachmentSourceBook = null; - // copyAttachmentTargetBook = null; - // } - - // @Test - // @Order(39) - // void testCopyAttachmentsWithNoteAndSecondaryPropertiesPreserved() throws IOException { - // System.out.println( - // "Test (39): Copy attachments with both note and secondary properties preserved"); - - // // Use entities from previous tests or create new ones - // boolean sourceBookJustCreated = false; - // boolean targetBookJustCreated = false; - - // if (copyAttachmentSourceBook == null || copyAttachmentSourceBook.isEmpty()) { - // copyAttachmentSourceBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentSourceBook.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - // copyAttachmentSourceChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentSourceBook); - // if (copyAttachmentSourceChapter.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - // sourceBookJustCreated = true; - // } - - // if (copyAttachmentTargetBook == null || copyAttachmentTargetBook.isEmpty()) { - // copyAttachmentTargetBook = - // api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (copyAttachmentTargetBook.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - // copyAttachmentTargetChapter = - // api.createEntityDraft( - // appUrl, chapterEntityName, entityName2, srvpath, copyAttachmentTargetBook); - // if (copyAttachmentTargetChapter.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - // targetBookJustCreated = true; - // } - - // // If source book was just created, save it first before we can edit it - // if (sourceBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created source book"); - // } - // } - - // // If target book was just created, save it first - // if (targetBookJustCreated) { - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save newly created target book"); - // } - // } - - // // Edit source book - // String editResponse = - // api.editEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source book"); - // } - - // // Create new attachments with both note and secondary properties - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyAttachmentSourceChapter); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String[] sourceAttachmentIds = new String[facet.length]; - // String testNote = "Combined test note - " + System.currentTimeMillis(); - // Boolean testBooleanProp = true; - // Integer testIntegerProp = 99999; - - // for (int i = 0; i < facet.length; i++) { - // // Create unique temp file - // File tempFile = - // File.createTempFile( - // "test39_combined_" + facet[i] + "_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // List createResponse = - // api.createAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // srvpath, - // postData, - // tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // sourceAttachmentIds[i] = createResponse.get(1); - - // // Update note using RequestBody - // String jsonNote = "{ \"note\" : \"" + testNote + "\" }"; - // RequestBody noteBody = RequestBody.create(MediaType.parse("application/json"), jsonNote); - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // noteBody); - - // // Update secondary properties using RequestBody - // String jsonBool = "{ \"customProperty6\" : " + testBooleanProp + " }"; - // RequestBody boolBody = RequestBody.create(MediaType.parse("application/json"), jsonBool); - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // boolBody); - - // String jsonInt = "{ \"customProperty2\" : " + testIntegerProp + " }"; - // RequestBody intBody = RequestBody.create(MediaType.parse("application/json"), jsonInt); - // api.updateSecondaryProperty( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i], - // intBody); - - // System.out.println("Note and secondary properties updated for facet: " + facet[i]); - // } - - // // Save source book - // String saveResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, copyAttachmentSourceBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Edit target book - // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // // Copy attachments to target - // for (int i = 0; i < facet.length; i++) { - // Map sourceMetadata = - // api.fetchMetadata( - // appUrl, - // chapterEntityName, - // facet[i], - // copyAttachmentSourceChapter, - // sourceAttachmentIds[i]); - // String objectId = sourceMetadata.get("objectId").toString(); - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - - // String copyResponse = - // api.copyAttachment( - // appUrl, chapterEntityName, facet[i], copyAttachmentTargetChapter, objectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // System.out.println("Attachment with note and properties copied to facet: " + facet[i]); - // } - - // // Save target book - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, - // copyAttachmentTargetBook); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify note and secondary properties were preserved in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], - // copyAttachmentTargetChapter); - - // boolean noteFound = false; - // boolean propsFound = false; - - // for (Map attachment : targetMetadata) { - // String copiedNote = (String) attachment.get("note"); - // Object boolProp = attachment.get("customProperty6"); - // Object intProp = attachment.get("customProperty2"); - - // if (testNote.equals(copiedNote)) { - // noteFound = true; - // System.out.println("Note preserved in target facet: " + facet[i]); - // } - - // if (boolProp != null && intProp != null) { - // if (Boolean.TRUE.equals(boolProp) && Integer.valueOf(99999).equals(intProp)) { - // propsFound = true; - // System.out.println("Secondary properties preserved in target facet: " + facet[i]); - // } - // } - // } - - // if (!noteFound) { - // System.out.println("Warning: Note may not be preserved in facet: " + facet[i]); - // } - // if (!propsFound) { - // System.out.println( - // "Warning: Secondary properties may not be preserved in facet: " + facet[i]); - // } - // } - - // // Cleanup - delete both books - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - - // // Reset static variables - // copyAttachmentSourceBook = null; - // copyAttachmentTargetBook = null; - // copyAttachmentSourceChapter = null; - // copyAttachmentTargetChapter = null; - - // System.out.println("Test 39 passed - both note and secondary properties checked during - // copy"); - // } - - // @Test - // @Order(40) - // void testCopyAttachmentsWithInvalidObjectId() throws IOException { - // System.out.println("Test (40): Copy attachments with invalid object ID should fail"); - - // // Create independent test entities (don't rely on previous tests) - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create test book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create test chapter"); - // } - - // // Save the book first so it's not in draft mode - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save test book"); - // } - - // // Now edit it to test copy with invalid object IDs - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit test book"); - // } - - // // Try to copy with invalid object ID - // for (String facetName : facet) { - // try { - // List invalidObjectIds = new ArrayList<>(); - // invalidObjectIds.add("invalidObjectId123"); - // invalidObjectIds.add("anotherInvalidId456"); - // api.copyAttachment(appUrl, chapterEntityName, facetName, testChapterID, - // invalidObjectIds); - // fail("Copy with invalid object ID should have thrown an error for facet: " + facetName); - // } catch (IOException e) { - // // Expected - copy should fail with invalid object ID - // System.out.println( - // "Expected error received for invalid object ID in facet " - // + facetName - // + ": " - // + e.getMessage()); - // } - // } - - // // Save and cleanup - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - - // // Also cleanup test 36 entities if they exist - // if (copyAttachmentSourceBook != null && !copyAttachmentSourceBook.isEmpty()) { - // try { - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentSourceBook); - // } catch (Exception e) { - // // Ignore - may already be deleted - // } - // } - // if (copyAttachmentTargetBook != null && !copyAttachmentTargetBook.isEmpty()) { - // try { - // api.deleteEntity(appUrl, bookEntityName, copyAttachmentTargetBook); - // } catch (Exception e) { - // // Ignore - may already be deleted - // } - // } - // } - - // @Test - // @Order(41) - // void testCopyAttachmentsToExistingChapter() throws IOException { - // System.out.println( - // "Test (41): Copy attachments to an existing chapter that already has attachments"); - - // // Create Book1 with source chapter - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create Book2 with target chapter that has existing attachments - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (targetBookID.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - // if (targetChapterID.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Create temp files with unique names to avoid duplicate filename errors - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test41_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("copy_sample" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("copy_sample" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create attachment in source chapter - // List sourceObjIds = new ArrayList<>(); - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempPdf); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create source attachment"); - // } - // String attachmentId = createResponse.get(1); - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - // sourceObjIds.add(metadata.get("objectId").toString()); - // } - - // // Create existing attachment in target chapter - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", targetChapterID); - // postData.put("mimeType", "text/plain"); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], targetChapterID, srvpath, postData, tempTxt); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create existing target attachment"); - // } - // String attachmentId = createResponse.get(1); - // } - - // // Save both books - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Edit target book and copy attachments - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // // Copy from source to target (target already has 1 attachment per facet) - // for (int i = 0; i < facet.length; i++) { - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjIds.get(i)); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facet[i], targetChapterID, - // objectIdsToCopy); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to facet: " + facet[i]); - // } - // } - - // // Save target book - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify target chapter now has 2 attachments per facet (1 existing + 1 copied) - // for (String facetName : facet) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.size() != 2) { - // fail( - // "Expected 2 attachments in facet " - // + facetName - // + " (1 existing + 1 copied), found " - // + targetMetadata.size()); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } + @Test + @Order(68) + void testMoveAttachmentsPartialFailure() throws IOException { + System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); - // // ============= LINK RENAME TESTS (47-49) ============= + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (47): Rename link in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links in all facets - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facetName, testChapterID, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit and rename links - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // String renameResponse = - // api.renameAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, "sampleRenamed"); - // if (!renameResponse.equals("Renamed")) { - // fail("Could not rename link in facet: " + facetName); - // } - // } - - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book after renaming links"); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(48) - // void testRenameLinkDuplicate() throws IOException { - // System.out.println("Test (48): Rename link in chapter fails due to duplicate error"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create two links in all facets - // for (String facetName : facet) { - // String createLinkResponse1 = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "link1", - // "https://www.example1.com"); - // String createLinkResponse2 = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "link2", - // "https://www.example2.com"); - // if (!createLinkResponse1.equals("Link created successfully") - // || !createLinkResponse2.equals("Link created successfully")) { - // fail("Could not create links in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit and try to rename link2 to link1 (duplicate) - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.size() < 2) { - // fail("Expected 2 links in facet: " + facetName); - // } - - // // Find link2 and rename to link1 - // for (Map attachment : attachments) { - // if ("link2".equals(attachment.get("fileName"))) { - // String linkId = (String) attachment.get("ID"); - // api.renameAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, "link1"); - // break; - // } - // } - // } - - // // Save should fail with duplicate error - // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // ObjectMapper mapper = new ObjectMapper(); - // try { - // JsonNode errorJson = mapper.readTree(saveError); - // String errorMessage = errorJson.path("error").path("message").asText(); - // if (!errorMessage.contains("already exists")) { - // fail("Expected duplicate error but got: " + saveError); - // } - // } catch (Exception e) { - // if (!saveError.contains("already exists")) { - // fail("Expected duplicate error but got: " + saveError); - // } - // } - - // // Cleanup - // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(49) - // void testRenameLinkUnsupportedCharacters() throws IOException { - // System.out.println("Test (49): Rename link in chapter fails due to unsupported characters"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links in all facets - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit and rename with unsupported characters - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // api.renameAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, "invalid//name"); - // } - - // // Save should fail with unsupported characters error - // String saveError = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveError.contains("unsupported characters")) { - // fail("Expected unsupported characters error but got: " + saveError); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // // ============= LINK EDIT TESTS (50-53) ============= + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (50): Edit existing link URL in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links in all facets - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit links - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // String editLinkResponse = - // api.editLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // linkId, - // "https://www.editedexample.com"); - // if (!editLinkResponse.equals("Link edited successfully")) { - // fail("Could not edit link in facet: " + facetName); - // } - // } - - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book after editing links"); - // } - - // // Verify links open successfully - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // for (Map attachment : attachments) { - // String linkId = (String) attachment.get("ID"); - // String openResponse = - // api.openAttachment(appUrl, chapterEntityName, facetName, testChapterID, linkId); - // if (!openResponse.equals("Attachment opened successfully")) { - // fail("Could not open edited link in facet: " + facetName); - // } - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(51) - // void testEditLinkFailureInvalidURL() throws IOException { - // System.out.println("Test (51): Edit link with invalid URL fails in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create links - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // try { - // api.editLink( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, - // "https://editedexample"); - // fail("Edit link should have failed with invalid URL in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected error received for invalid URL in facet " + facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(52) - // void testEditLinkFailureEmptyURL() throws IOException { - // System.out.println("Test (52): Edit link with empty URL fails in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // try { - // api.editLink(appUrl, chapterEntityName, facetName, testChapterID, linkId, ""); - // fail("Edit link should have failed with empty URL in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected error received for empty URL in facet " + facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (53): Edit link fails due to no SDM roles assigned in chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // for (String facetName : facet) { - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // "sample", - // "https://www.example.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // String editResponse = apiNoRoles.editEntityDraft(appUrl, bookEntityName, srvpath, - // testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // for (String facetName : facet) { - // List> attachments = - // apiNoRoles.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); - // if (attachments.isEmpty()) { - // fail("No links found in facet: " + facetName); - // } - - // String linkId = (String) attachments.get(0).get("ID"); - // try { - // apiNoRoles.editLink( - // appUrl, chapterEntityName, facetName, testChapterID, linkId, - // "https://www.edited.com"); - // fail("Edit link should have failed without SDM roles in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected permission error received in facet " + facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // ============= COPY LINK TESTS (54-58) ============= + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get real object ID and folder ID + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String realObjectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + // Create target + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Try to move with mix of valid and invalid object IDs + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(realObjectId); + moveObjectIds.add("invalidObjectId123"); + + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Should handle partial failure + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } - // @Test - // @Order(54) - // void testCopyLinkSuccessNewChapter() throws IOException { - // System.out.println("Test (54): Copy link from one chapter to another new chapter"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // if (sourceChapterID.equals("Could not create entity") - // || targetChapterID.equals("Could not create entity")) { - // fail("Could not create chapters"); - // } - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // // Create links in source chapter - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch object IDs - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // // Copy links to target chapter - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link for facet " + facetName + ": " + copyResponse); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify link type and URL - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.isEmpty()) { - // fail("No links found in target chapter for facet: " + facetName); - // } - - // Map copiedLink = targetMetadata.get(0); - // String receivedUrl = (String) copiedLink.get("linkUrl"); - // assertEquals(linkUrl, receivedUrl, "Link URL mismatch in facet " + facetName); - - // objectIdIndex++; - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(55) - // void testCopyLinkUnsuccessfulInvalidObjectId() throws IOException { - // System.out.println("Test (55): Copy invalid link object ID to chapter fails"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // for (String facetName : facet) { - // try { - // List invalidObjectIds = new ArrayList<>(); - // invalidObjectIds.add("incorrectObjectId"); - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // invalidObjectIds); - // fail("Copy should have thrown error for invalid object ID in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected error received for invalid object ID in facet " + - // facetName); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(56) - // void testCopyLinkToExistingChapter() throws IOException { - // System.out.println("Test (56): Copy link to existing chapter that has attachments"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // // Create links in source chapter - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sourceLink" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in source chapter for facet: " + facet[i]); - // } - // } - - // // Create existing links in target chapter - // for (int i = 0; i < facet.length; i++) { - // String linkName = "existingLink" + i; - // String createLinkResponse = - // api.createLink( - // appUrl, - // chapterEntityName, - // facet[i], - // targetChapterID, - // linkName, - // "https://www.existing.com"); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create existing link in target chapter for facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch source object IDs - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // // Copy links to target chapter - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link for facet " + facetName); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify target has 2 links (existing + copied) - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.size() != 2) { - // fail( - // "Expected 2 links in target chapter facet " - // + facetName - // + ", found " - // + targetMetadata.size()); - // } - - // objectIdIndex++; - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(57) - // void testCopyLinkNoSDMRoles() throws IOException { - // System.out.println("Test (57): Copy link fails due to no SDM roles"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch object IDs - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // // Try to copy with no SDM roles - // int objectIdIndex = 0; - // for (String facetName : facet) { - // try { - // // Use normal api to put book in draft mode - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // // Use apiNoRoles to attempt copy (should fail) - // apiNoRoles.copyAttachment( - // appUrl, chapterEntityName, facetName, targetChapterID, subListToCopy); - // fail("Copy should have failed without SDM roles in facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Expected permission error in facet " + facetName); - // // Discard draft to clean up for next iteration - // api.deleteEntityDraft(appUrl, bookEntityName, targetBookID); - // } - // objectIdIndex++; - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(58) - // void testCopyLinkFromDraftChapter() throws IOException { - // System.out.println("Test (58): Copy link from draft chapter to another chapter"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // String linkUrl = "https://www.example.com"; - // List linkObjectIds = new ArrayList<>(); - - // // Create links in source chapter (NOT saved yet - draft mode) - // for (int i = 0; i < facet.length; i++) { - // String linkName = "draftLink" + i; - // String createLinkResponse = - // api.createLink(appUrl, chapterEntityName, facet[i], sourceChapterID, linkName, - // linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - - // // Save target book only - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch object IDs from draft - // for (int i = 0; i < facet.length; i++) { - // List> metadata = - // api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facet[i], sourceChapterID); - // for (Map meta : metadata) { - // if (meta.containsKey("objectId")) { - // linkObjectIds.add(meta.get("objectId").toString()); - // } - // } - // } - - // if (linkObjectIds.size() != facet.length) { - // fail("Could not fetch all object IDs from draft"); - // } - - // // Copy links from draft to target - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = linkObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link from draft for facet " + facetName); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify link was copied - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.isEmpty()) { - // fail("No links found in target chapter for facet: " + facetName); - // } - - // objectIdIndex++; - // } - - // // Cleanup - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } + @Test + @Order(69) + void testMoveAttachmentsEmptyList() throws IOException { + System.out.println("Test (69): Move attachments with empty object ID list"); - // // ============= COPY ATTACHMENTS DRAFT MODE (59) ============= + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // @Test - // @Order(59) - // void testCopyAttachmentsSuccessNewChapterDraft() throws IOException { - // System.out.println("Test (59): Copy attachments from one chapter to another in draft mode"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // if (sourceChapterID.equals("Could not create entity") - // || targetChapterID.equals("Could not create entity")) { - // fail("Could not create chapters"); - // } - - // // Create temp files with unique names - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test59_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("draft_copy" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("draft_copy" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List sourceObjectIds = new ArrayList<>(); - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(new ArrayList<>()); - // } - - // // Create attachments in source chapter (still in draft) - // for (int i = 0; i < facet.length; i++) { - // postData.put("mimeType", i == 1 ? "text/plain" : "application/pdf"); - // File file = i == 1 ? tempTxt : tempPdf; - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - - // // Fetch object IDs from draft - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // Map metadata = - // api.fetchMetadataDraft( - // appUrl, chapterEntityName, facet[i], sourceChapterID, attachment); - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } - // } - - // // Save target book only - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Copy attachments from draft to target - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // subListToCopy); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment from draft for facet " + facetName); - // } - - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book"); - // } - - // // Verify attachment was copied - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, targetChapterID); - // if (targetMetadata.isEmpty()) { - // fail("No attachments found in target chapter for facet: " + facetName); - // } - - // // Read attachment to verify - // String attachmentId = (String) targetMetadata.get(0).get("ID"); - // String readResponse = - // api.readAttachment(appUrl, chapterEntityName, facetName, targetChapterID, - // attachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment in facet: " + facetName); - // } - - // objectIdIndex++; - // } - - // // Cleanup - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } - // // ============= CHANGELOG TESTS (60-64) ============= + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Try to move with empty list + List emptyObjectIds = new ArrayList<>(); + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + + try { + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + "someFolderId", + emptyObjectIds, + targetFacet, + sourceFacet); + // Should either fail or do nothing + } catch (Exception e) { + System.out.println("Expected: Move with empty list handled: " + e.getMessage()); + } - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println("Test (60): View changelog for newly created attachment in chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String facetName = facet[i]; - - // // Create book and chapter - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = - // File.createTempFile( - // "changelog_test60_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } - - // String attachmentId = createResponse.get(1); - - // // Fetch changelog - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // assertEquals(1, changelogResponse.get("numItems"), "Should have 1 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'"); - - // // Cleanup - // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - // } - // } - - // @Test - // @Order(61) - // void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - // System.out.println("Test (61): Changelog after modifying note and custom property in - // chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String facetName = facet[i]; - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = - // File.createTempFile( - // "changelog_test61_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Update note - // String notesValue = "Test note for changelog verification"; - // RequestBody updateNotesBody = - // RequestBody.create( - // MediaType.parse("application/json"), "{\"note\": \"" + notesValue + "\"}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, updateNotesBody); - - // // Update custom property - // RequestBody bodyInt = - // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": - // 12345}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facetName, testChapterID, attachmentId, bodyInt); - - // // Save - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // // Fetch changelog - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // int numItems = (int) changelogResponse.get("numItems"); - // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + updates)"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - // } - - // @Test - // @Order(62) - // void testChangelogAfterRenamingAttachment() throws IOException { - // System.out.println("Test (62): Changelog after renaming attachment in chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String facetName = facet[i]; - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = - // File.createTempFile( - // "changelog_test62_" + facetName + "_" + System.currentTimeMillis(), ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Rename attachment - // String renameResponse = - // api.renameAttachment( - // appUrl, - // chapterEntityName, - // facetName, - // testChapterID, - // attachmentId, - // "renamed_file.txt"); - // if (!renameResponse.equals("Renamed")) { - // fail("Could not rename attachment"); - // } - - // // Save - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book"); - // } - - // // Edit to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit book"); - // } - - // // Fetch changelog - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facetName, testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // int numItems = (int) changelogResponse.get("numItems"); - // assertTrue(numItems >= 2, "Should have at least 2 changelog entries (created + renamed)"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - // } - - // @Test - // @Order(63) - // void testChangelogForCopiedAttachment() throws IOException { - // System.out.println("Test (63): Changelog for copied attachment in chapter"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = File.createTempFile("changelog_test63_" + System.currentTimeMillis(), - // ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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 in source - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Save both books - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Get object ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - - // // Copy to target - // String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target book"); - // } - - // List objectIds = new ArrayList<>(); - // objectIds.add(objectId); - // String copyResponse = - // api.copyAttachment(appUrl, chapterEntityName, facet[0], targetChapterID, objectIds); - - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment"); - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Fetch changelog for copied attachment - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - // String copiedAttachmentId = (String) targetMetadata.get(0).get("ID"); - - // editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // Map changelogResponse = - // api.fetchChangelog( - // appUrl, chapterEntityName, facet[0], targetChapterID, copiedAttachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // int numItems = (int) changelogResponse.get("numItems"); - // assertTrue(numItems >= 1, "Copied attachment should have changelog entries"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(64) - // void testChangelogForNewChapter() throws IOException { - // System.out.println("Test (64): Changelog for attachment in newly created chapter"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // if (testChapterID.equals("Could not create entity")) { - // fail("Could not create chapter"); - // } - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // File tempFile = File.createTempFile("changelog_test64_" + System.currentTimeMillis(), - // ".txt"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalFile.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", testChapterID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - - // String attachmentId = createResponse.get(1); - - // // Fetch changelog before saving - // Map changelogResponse = - // api.fetchChangelog(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - // assertEquals( - // 1, changelogResponse.get("numItems"), "New attachment should have 1 changelog entry"); - - // @SuppressWarnings("unchecked") - // List> changeLogs = - // (List>) changelogResponse.get("changeLogs"); - // assertEquals("created", changeLogs.get(0).get("operation"), "Operation should be 'created'"); - - // // Cleanup - // api.deleteEntityDraft(appUrl, bookEntityName, testBookID); - // } + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } - // // ============= MOVE ATTACHMENT TESTS (65-75) ============= + @Test + @Order(70) + void testMoveAttachmentsToSameChapter() throws IOException { + System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + 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"); + + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Get object ID and folder ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String folderId = metadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move to same chapter + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + testChapterID, + folderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Should handle gracefully - attachment stays in place + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Verify attachment still exists + List> metadataAfter = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); + assertEquals(1, metadataAfter.size(), "Attachment should still exist"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(71) + void testMoveAttachmentsBetweenFacets() throws IOException { + System.out.println("Test (71): Move attachments between different facets in chapters"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Create in attachments facet + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + + // Get object ID and folder ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String sourceFolderId = metadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move from attachments to references facet + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[1], // references facet + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify moved to different facet + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); + assertTrue( + targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(72) + void testMoveMultipleAttachments() throws IOException { + System.out.println("Test (72): Move multiple attachments at once between chapters"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // Create multiple temp files + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); + + String uniqueSuffix = "_test72_" + System.currentTimeMillis(); + File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); + File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); + tempPdf.deleteOnExit(); + tempTxt.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempPdf.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + java.nio.file.Files.copy( + originalTxt.toPath(), tempTxt.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List sourceAttachmentIds = new ArrayList<>(); + File[] files = {tempPdf, tempTxt}; + String[] mimeTypes = {"application/pdf", "text/plain"}; + + for (int i = 0; i < files.length; i++) { + postData.put("mimeType", mimeTypes[i]); + List createResponse = + api.createAttachment( + appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get object IDs + List moveObjectIds = new ArrayList<>(); + String sourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + moveObjectIds.add(metadata.get("objectId").toString()); + if (sourceFolderId == null) { + sourceFolderId = metadata.get("folderId").toString(); + } + } + + // Create target + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Move all at once + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + Map moveResult = + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify all moved + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); + assertEquals( + sourceAttachmentIds.size(), + targetMetadata.size(), + "All attachments should be moved to target"); + + List> sourceMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(73) + void testMoveAttachmentsAllFacets() throws IOException { + System.out.println("Test (73): Move attachments from all facets between chapters"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || targetBookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String targetChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + + // Create attachment in each facet + for (int i = 0; i < facet.length; i++) { + String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); + File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), + tempFile.toPath(), + java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facet[i]); + } + } + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + // Move from each facet + for (int i = 0; i < facet.length; i++) { + List> sourceMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); + if (sourceMetadata.isEmpty()) { + continue; + } + + String attachmentId = (String) sourceMetadata.get(0).get("ID"); + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String sourceFolderId = metadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; + api.moveAttachment( + appUrl, + chapterEntityName, + facet[i], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + } + + // Verify all facets have attachments in target + for (int i = 0; i < facet.length; i++) { + List> targetMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); + assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + facet[i]); + } + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(74) + void testChainMoveAttachments() throws IOException { + System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + + if (sourceBookID.equals("Could not create entity") + || target1BookID.equals("Could not create entity") + || target2BookID.equals("Could not create entity")) { + fail("Could not create books"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + String target1ChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); + String target2ChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); + api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); + + // First move: Source -> Target1 + Map sourceMetadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = sourceMetadata.get("objectId").toString(); + String sourceFolderId = sourceMetadata.get("folderId").toString(); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + // Move to target1 + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + target1ChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify in target1 + List> target1Metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); + + // Second move: Target1 -> Target2 + String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); + Map target1AttMetadata = + api.fetchMetadata( + appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); + String target1ObjectId = target1AttMetadata.get("objectId").toString(); + String target1FolderId = target1AttMetadata.get("folderId").toString(); + + moveObjectIds.clear(); + moveObjectIds.add(target1ObjectId); + + // Move to target2 + api.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + target2ChapterID, + target1FolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify final state + List> target2Metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); + assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); + + target1Metadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); + assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); + + List> sourceFinalMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, target1BookID); + api.deleteEntity(appUrl, bookEntityName, target2BookID); + } + + @Test + @Order(75) + void testMoveAttachmentsWithoutSDMRole() throws IOException { + System.out.println("Test (75): Move attachments fails without SDM role"); + + String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (sourceBookID.equals("Could not create entity")) { + fail("Could not create source book"); + } + + String sourceChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); + + // Create temp file + ClassLoader classLoader = getClass().getClassLoader(); + File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempFile = + File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); + tempFile.deleteOnExit(); + java.nio.file.Files.copy( + originalPdf.toPath(), tempFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", sourceChapterID); + 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } + String attachmentId = createResponse.get(1); + + api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); + + // Get object ID and folder ID + Map metadata = + api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); + String objectId = metadata.get("objectId").toString(); + String sourceFolderId = metadata.get("folderId").toString(); + + // Create target with no role user + String targetBookID = + apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (targetBookID.equals("Could not create entity")) { + fail("Could not create target book"); + } + + String targetChapterID = + apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); + + // Save target before move + apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); + + List moveObjectIds = new ArrayList<>(); + moveObjectIds.add(objectId); + + String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; + boolean moveFailed = false; + String errorMessage = null; + + try { + Map moveResult = + apiNoRoles.moveAttachment( + appUrl, + chapterEntityName, + facet[0], + targetChapterID, + sourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null || moveResult.containsKey("error")) { + moveFailed = true; + errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; + } + } catch (Exception e) { + moveFailed = true; + errorMessage = e.getMessage(); + } + + assertTrue(moveFailed, "Move should fail without SDM role"); + System.out.println("Move correctly failed without SDM role: " + errorMessage); + + // Verify source still has attachment + List> sourceMetadataAfter = + api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); + assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); + + // Cleanup + api.deleteEntity(appUrl, bookEntityName, sourceBookID); + api.deleteEntity(appUrl, bookEntityName, targetBookID); + } + + @Test + @Order(76) + void testRenameChapterAttachmentWithExtensionChange() throws IOException { + System.out.println( + "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); + + // Step 1: Create a new book and chapter + String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (newBookID.equals("Could not create entity")) { + fail("Could not create book"); + } + String newChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + if (newChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not create chapter"); + } + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + if (!saveResponse.equals("Saved")) { + fail("Could not save book: " + saveResponse); + } + + // Step 2: Upload a PDF attachment to each facet in the chapter + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", newChapterID); + 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, bookEntityName, srvpath, newBookID); + if (!"Entity in draft mode".equals(editResponse)) { + fail("Could not put book in draft mode for PDF upload"); + } + + String[] facetAttachmentIDs = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + facetAttachmentIDs[i] = + CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + if (facetAttachmentIDs[i] == null) { + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + } + } + + // Step 3: Save the book + String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + if (!savedAfterUpload.equals("Saved")) { + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not save book after PDF upload: " + savedAfterUpload); + } + + // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt + for (int i = 0; i < facet.length; i++) { + String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + if (!"Entity in draft mode".equals(editDraftResponse)) { + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not put book in draft mode for rename on facet: " + facet[i]); + } + + String renameResponse = + api.renameAttachment( + appUrl, + chapterEntityName, + facet[i], + newChapterID, + facetAttachmentIDs[i], + "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + api.deleteEntity(appUrl, bookEntityName, newBookID); + fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + } + + // Step 6: Save and validate the extension change warning message + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } + + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + } - // @Test - // @Order(65) - // void testMoveAttachmentsWithSourceFacet() throws IOException { - // System.out.println("Test (65): Move attachments from source chapter to target chapter"); - - // for (int i = 0; i < facet.length; i++) { - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create temp files - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test65_" + facet[i] + "_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("move" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("move" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), - // tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List sourceAttachmentIds = new ArrayList<>(); - // File[] files = {tempPdf, tempTxt}; - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source chapter"); - // } - // } - - // // Save source book - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save source book"); - // } - - // // Get object IDs and folder ID - // List moveObjectIds = new ArrayList<>(); - // String sourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, - // attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (sourceFolderId == null && metadata.containsKey("folderId")) { - // sourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } - - // // Create target book and chapter - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (targetBookID.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - // if (targetChapterID.equals("Could not create entity")) { - // fail("Could not create target chapter"); - // } - - // // Save target book before moving attachments (moveAttachments requires Active entity) - // saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target book before move"); - // } - - // // Move attachments to Active entity - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } - - // // Verify - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadata.size(), - // "Target should have all attachments after move"); - - // List> sourceMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // assertEquals(0, sourceMetadata.size(), "Source should have no attachments after move"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // } - // } - - // @Test - // @Order(66) - // void testMoveAttachmentsToChapterWithDuplicate() throws IOException { - // System.out.println("Test (66): Move attachments to chapter with duplicate attachment"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Create attachment in source with specific name - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, - // originalPdf); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create source attachment"); - // } - // String sourceAttachmentId = createResponse.get(1); - - // // Create attachment in target with SAME name (duplicate) - // postData.put("up__ID", targetChapterID); - // createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], targetChapterID, srvpath, postData, - // originalPdf); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create target attachment"); - // } - - // // Save both - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Get source object ID and folder ID - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, - // sourceAttachmentId); - // String objectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move to saved target - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Move should handle duplicate - attachment stays in source - - // // Verify source still has attachment (duplicate not moved) - // List> sourceMetadataAfter = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertTrue( - // sourceMetadataAfter.size() >= 1, - // "Source should still have attachment when duplicate exists in target"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(67) - // void testMoveAttachmentsWithNotesAndSecondaryProperties() throws IOException { - // System.out.println("Test (67): Move attachments with notes and secondary properties"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // if (sourceChapterID.equals("Could not create entity")) { - // fail("Could not create source chapter"); - // } - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test67_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // // Add note and secondary property - // String testNote = "Test note for move"; - // RequestBody noteBody = - // RequestBody.create(MediaType.parse("application/json"), "{\"note\": \"" + testNote + - // "\"}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, noteBody); - - // RequestBody propBody = - // RequestBody.create(MediaType.parse("application/json"), "{\"customProperty2\": 9999}"); - // api.updateSecondaryProperty( - // appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId, propBody); - - // // Save source - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get object ID and folder ID - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // // Create target - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Save target before move - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null"); - // } - - // // Verify note was preserved - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - // if (!targetMetadata.isEmpty()) { - // String movedAttachmentId = (String) targetMetadata.get(0).get("ID"); - // Map movedMetadata = - // api.fetchMetadata( - // appUrl, chapterEntityName, facet[0], targetChapterID, movedAttachmentId); - - // // Note should be preserved - // if (movedMetadata.containsKey("note")) { - // assertEquals(testNote, movedMetadata.get("note"), "Note should be preserved after move"); - // } - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(68) - // void testMoveAttachmentsPartialFailure() throws IOException { - // System.out.println("Test (68): Move attachments with partial failure (invalid object ID)"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test68_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get real object ID and folder ID - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String realObjectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // // Create target - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Save target before move - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Try to move with mix of valid and invalid object IDs - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(realObjectId); - // moveObjectIds.add("invalidObjectId123"); - - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Should handle partial failure - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(69) - // void testMoveAttachmentsEmptyList() throws IOException { - // System.out.println("Test (69): Move attachments with empty object ID list"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Try to move with empty list - // List emptyObjectIds = new ArrayList<>(); - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - - // try { - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // "someFolderId", - // emptyObjectIds, - // targetFacet, - // sourceFacet); - // // Should either fail or do nothing - // } catch (Exception e) { - // System.out.println("Expected: Move with empty list handled: " + e.getMessage()); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(70) - // void testMoveAttachmentsToSameChapter() throws IOException { - // System.out.println("Test (70): Move attachments to same chapter (should handle gracefully)"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String testChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test70_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // 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"); - - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], testChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Get object ID and folder ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], testChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String folderId = metadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move to same chapter - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // testChapterID, - // folderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Should handle gracefully - attachment stays in place - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Verify attachment still exists - // List> metadataAfter = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], testChapterID); - // assertEquals(1, metadataAfter.size(), "Attachment should still exist"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(71) - // void testMoveAttachmentsBetweenFacets() throws IOException { - // System.out.println("Test (71): Move attachments between different facets in chapters"); - - // String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (testBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("move_test71_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // // Create in attachments facet - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); - - // // Get object ID and folder ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String sourceFolderId = metadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move from attachments to references facet - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[1]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[1], // references facet - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify moved to different facet - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[1], targetChapterID); - // assertTrue( - // targetMetadata.size() >= 1, "Target references facet should have the moved attachment"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, testBookID); - // } - - // @Test - // @Order(72) - // void testMoveMultipleAttachments() throws IOException { - // System.out.println("Test (72): Move multiple attachments at once between chapters"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // // Create multiple temp files - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File originalTxt = new File(classLoader.getResource("sample.txt").getFile()); - - // String uniqueSuffix = "_test72_" + System.currentTimeMillis(); - // File tempPdf = File.createTempFile("multi_move" + uniqueSuffix, ".pdf"); - // File tempTxt = File.createTempFile("multi_move" + uniqueSuffix, ".txt"); - // tempPdf.deleteOnExit(); - // tempTxt.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempPdf.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - // java.nio.file.Files.copy( - // originalTxt.toPath(), tempTxt.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List sourceAttachmentIds = new ArrayList<>(); - // File[] files = {tempPdf, tempTxt}; - // String[] mimeTypes = {"application/pdf", "text/plain"}; - - // for (int i = 0; i < files.length; i++) { - // postData.put("mimeType", mimeTypes[i]); - // List createResponse = - // api.createAttachment( - // appUrl, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, files[i]); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get object IDs - // List moveObjectIds = new ArrayList<>(); - // String sourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (sourceFolderId == null) { - // sourceFolderId = metadata.get("folderId").toString(); - // } - // } - - // // Create target - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // // Save target before move - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Move all at once - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify all moved - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], targetChapterID); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadata.size(), - // "All attachments should be moved to target"); - - // List> sourceMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertEquals(0, sourceMetadata.size(), "Source should have no attachments"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(73) - // void testMoveAttachmentsAllFacets() throws IOException { - // System.out.println("Test (73): Move attachments from all facets between chapters"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String targetBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || targetBookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String targetChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, targetBookID); - - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - - // // Create attachment in each facet - // for (int i = 0; i < facet.length; i++) { - // String uniqueSuffix = "_test73_" + facet[i] + "_" + System.currentTimeMillis(); - // File tempFile = File.createTempFile("all_facets" + uniqueSuffix, ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), - // tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[i], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facet[i]); - // } - // } - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // // Move from each facet - // for (int i = 0; i < facet.length; i++) { - // List> sourceMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID); - // if (sourceMetadata.isEmpty()) { - // continue; - // } - - // String attachmentId = (String) sourceMetadata.get(0).get("ID"); - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[i], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String sourceFolderId = metadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[i]; - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - // } - - // // Verify all facets have attachments in target - // for (int i = 0; i < facet.length; i++) { - // List> targetMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[i], targetChapterID); - // assertTrue(targetMetadata.size() >= 1, "Target should have attachment in facet: " + - // facet[i]); - // } - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(74) - // void testChainMoveAttachments() throws IOException { - // System.out.println("Test (74): Chain move attachments: Source -> Target1 -> Target2"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String target1BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // String target2BookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - - // if (sourceBookID.equals("Could not create entity") - // || target1BookID.equals("Could not create entity") - // || target2BookID.equals("Could not create entity")) { - // fail("Could not create books"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - // String target1ChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target1BookID); - // String target2ChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, target2BookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = File.createTempFile("chain_move_test74_" + System.currentTimeMillis(), - // ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target1BookID); - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, target2BookID); - - // // First move: Source -> Target1 - // Map sourceMetadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = sourceMetadata.get("objectId").toString(); - // String sourceFolderId = sourceMetadata.get("folderId").toString(); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // // Move to target1 - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // target1ChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify in target1 - // List> target1Metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - // assertEquals(1, target1Metadata.size(), "Target1 should have the attachment"); - - // // Second move: Target1 -> Target2 - // String target1AttachmentId = (String) target1Metadata.get(0).get("ID"); - // Map target1AttMetadata = - // api.fetchMetadata( - // appUrl, chapterEntityName, facet[0], target1ChapterID, target1AttachmentId); - // String target1ObjectId = target1AttMetadata.get("objectId").toString(); - // String target1FolderId = target1AttMetadata.get("folderId").toString(); - - // moveObjectIds.clear(); - // moveObjectIds.add(target1ObjectId); - - // // Move to target2 - // api.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // target2ChapterID, - // target1FolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify final state - // List> target2Metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target2ChapterID); - // assertEquals(1, target2Metadata.size(), "Target2 should have the attachment"); - - // target1Metadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], target1ChapterID); - // assertEquals(0, target1Metadata.size(), "Target1 should have no attachments"); - - // List> sourceFinalMetadata = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertEquals(0, sourceFinalMetadata.size(), "Source should have no attachments"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, target1BookID); - // api.deleteEntity(appUrl, bookEntityName, target2BookID); - // } - - // @Test - // @Order(75) - // void testMoveAttachmentsWithoutSDMRole() throws IOException { - // System.out.println("Test (75): Move attachments fails without SDM role"); - - // String sourceBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (sourceBookID.equals("Could not create entity")) { - // fail("Could not create source book"); - // } - - // String sourceChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, sourceBookID); - - // // Create temp file - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalPdf = new File(classLoader.getResource("sample.pdf").getFile()); - // File tempFile = - // File.createTempFile("move_no_role_test75_" + System.currentTimeMillis(), ".pdf"); - // tempFile.deleteOnExit(); - // java.nio.file.Files.copy( - // originalPdf.toPath(), tempFile.toPath(), - // java.nio.file.StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", sourceChapterID); - // 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, chapterEntityName, facet[0], sourceChapterID, srvpath, postData, tempFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } - // String attachmentId = createResponse.get(1); - - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, sourceBookID); - - // // Get object ID and folder ID - // Map metadata = - // api.fetchMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // String sourceFolderId = metadata.get("folderId").toString(); - - // // Create target with no role user - // String targetBookID = - // apiNoRoles.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (targetBookID.equals("Could not create entity")) { - // fail("Could not create target book"); - // } - - // String targetChapterID = - // apiNoRoles.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, - // targetBookID); - - // // Save target before move - // apiNoRoles.saveEntityDraft(appUrl, bookEntityName, srvpath, targetBookID); - - // List moveObjectIds = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // String sourceFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // String targetFacet = serviceName + "." + chapterEntityName + "." + facet[0]; - // boolean moveFailed = false; - // String errorMessage = null; - - // try { - // Map moveResult = - // apiNoRoles.moveAttachment( - // appUrl, - // chapterEntityName, - // facet[0], - // targetChapterID, - // sourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null || moveResult.containsKey("error")) { - // moveFailed = true; - // errorMessage = moveResult != null ? moveResult.get("error").toString() : "null result"; - // } - // } catch (Exception e) { - // moveFailed = true; - // errorMessage = e.getMessage(); - // } - - // assertTrue(moveFailed, "Move should fail without SDM role"); - // System.out.println("Move correctly failed without SDM role: " + errorMessage); - - // // Verify source still has attachment - // List> sourceMetadataAfter = - // api.fetchEntityMetadata(appUrl, chapterEntityName, facet[0], sourceChapterID); - // assertEquals(1, sourceMetadataAfter.size(), "Source should still have attachment"); - - // // Cleanup - // api.deleteEntity(appUrl, bookEntityName, sourceBookID); - // api.deleteEntity(appUrl, bookEntityName, targetBookID); - // } - - // @Test - // @Order(76) - // void testRenameChapterAttachmentWithExtensionChange() throws IOException { - // System.out.println( - // "Test (76) : Rename chapter attachment changing extension from .pdf to .txt across all - // facets - should return extension change warning"); - - // // Step 1: Create a new book and chapter - // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (newBookID.equals("Could not create entity")) { - // fail("Could not create book"); - // } - // String newChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - // if (newChapterID.equals("Could not create entity")) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not create chapter"); - // } - // String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save book: " + saveResponse); - // } - - // // Step 2: Upload a PDF attachment to each facet in the chapter - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", newChapterID); - // 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, bookEntityName, srvpath, newBookID); - // if (!"Entity in draft mode".equals(editResponse)) { - // fail("Could not put book in draft mode for PDF upload"); - // } - - // String[] facetAttachmentIDs = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // facetAttachmentIDs[i] = - // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - // if (facetAttachmentIDs[i] == null) { - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - // } - // } - - // // Step 3: Save the book - // String savedAfterUpload = api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // if (!savedAfterUpload.equals("Saved")) { - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not save book after PDF upload: " + savedAfterUpload); - // } - - // // Step 4 & 5: Edit the book, rename each facet's attachment changing extension .pdf -> .txt - // for (int i = 0; i < facet.length; i++) { - // String editDraftResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // if (!"Entity in draft mode".equals(editDraftResponse)) { - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not put book in draft mode for rename on facet: " + facet[i]); - // } - - // String renameResponse = - // api.renameAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // newChapterID, - // facetAttachmentIDs[i], - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - // } - - // // Step 6: Save and validate the extension change warning message - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } - - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); - // } - - // // Clean up - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // } - - // @Test - // @Order(77) - // void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { - // System.out.println( - // "Test (77) : Upload chapter attachment in draft, rename changing extension before save - // across all facets - should return extension change warning"); - - // for (int i = 0; i < facet.length; i++) { - // // Step 1: Create a new book and chapter draft (do NOT save) - // String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); - // if (newBookID.equals("Could not create entity")) { - // fail("Could not create book for facet: " + facet[i]); - // } - // String newChapterID = - // api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); - // if (newChapterID.equals("Could not create entity")) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not create chapter for facet: " + facet[i]); - // } - - // // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft - // (unsaved) - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", newChapterID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // String facetAttachmentID = - // CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); - // if (facetAttachmentID == null) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not upload sample.pdf to chapter facet: " + facet[i]); - // } - - // // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not - // saved - // String renameResponse = - // api.renameAttachment( - // appUrl, - // chapterEntityName, - // facet[i], - // newChapterID, - // facetAttachmentID, - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.deleteEntityDraft(appUrl, bookEntityName, newBookID); - // fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); - // } - - // // Step 4: Save the book — should receive extension change warning, not "Saved" - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } - - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); - - // // Clean up - // api.deleteEntity(appUrl, bookEntityName, newBookID); - // } - // } + // Clean up + api.deleteEntity(appUrl, bookEntityName, newBookID); + } + + @Test + @Order(77) + void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException { + System.out.println( + "Test (77) : Upload chapter attachment in draft, rename changing extension before save across all facets - should return extension change warning"); + + for (int i = 0; i < facet.length; i++) { + // Step 1: Create a new book and chapter draft (do NOT save) + String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (newBookID.equals("Could not create entity")) { + fail("Could not create book for facet: " + facet[i]); + } + String newChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID); + if (newChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not create chapter for facet: " + facet[i]); + } + + // Step 2: Upload a PDF attachment to the chapter facet while book is still in draft (unsaved) + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", newChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + String facetAttachmentID = + CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file); + if (facetAttachmentID == null) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not upload sample.pdf to chapter facet: " + facet[i]); + } + + // Step 3: Rename the attachment changing extension from .pdf to .txt — book still not saved + String renameResponse = + api.renameAttachment( + appUrl, + chapterEntityName, + facet[i], + newChapterID, + facetAttachmentID, + "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.deleteEntityDraft(appUrl, bookEntityName, newBookID); + fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse); + } + + // Step 4: Save the book — should receive extension change warning, not "Saved" + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); + + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; + + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } + + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + + // Clean up + api.deleteEntity(appUrl, bookEntityName, newBookID); + } + } @Test @Order(78) 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 0b98fc280..5d254871d 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 @@ -7,8 +7,13 @@ import java.io.File; import java.io.IOException; 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.*; @@ -383,6891 +388,6678 @@ void testUploadSingleTXT() throws IOException { } } - // @Test - // @Order(5) - // void testUploadSingleEXE() throws IOException { - // System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); - // Boolean testStatus = false; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.exe").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables - // 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, entityID); - // if (response.equals("Entity in draft mode")) { - // // Creation of attachment, reference and footnote - // for (int i = 0; i < facet.length; i++) { - // ID3[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID, postData, file); - // } - // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); - // } - // if (!testStatus) { - // fail("Could not upload sample.exe " + response); - // } - // } + @Test + @Order(5) + void testUploadSingleEXE() throws IOException { + System.out.println("Test (5) : Upload attachment, reference, and footnote EXE"); + Boolean testStatus = false; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.exe").getFile()); - // @Test - // @Order(6) - // void testUploadPDFDuplicate() throws IOException { - // System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID); - // 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, entityID); - // if ("Entity in draft mode".equals(response)) { - // Boolean allFacetsFailedCorrectly = true; - // for (int i = 0; i < facet.length; i++) { - // List facetResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, - // file); - // allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); - // } - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!allFacetsFailedCorrectly) { - // fail("One or more facets were incorrectly accepted as new."); - // } - // } else { - // fail("Entity could not be edited to draft mode."); - // } - // } + Map postData = new HashMap<>(); + postData.put("up__ID", entityID); + postData.put("mimeType", "application/octet-stream"); // Common mime-type for executables + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // @Test - // @Order(7) - // void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { - // System.out.println( - // "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and - // footnote"); - // Boolean testStatus = false; - // // Create a new entity draft - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID2 = response; - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - - // if ("Saved".equals(response)) { - // response = api.checkEntity(appUrl, entityName, entityID2); - // if ("Entity exists".equals(response)) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Could not create entity"); - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Entity in draft mode")) { + // Creation of attachment, reference and footnote + for (int i = 0; i < facet.length; i++) { + ID3[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID, postData, file); + } + testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID, ID3); + } + if (!testStatus) { + fail("Could not upload sample.exe " + response); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID2); - // 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 draft mode - // response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); - // if ("Entity in draft mode".equals(response)) { - // // Create attachment, reference, and footnote - // for (int i = 0; i < facet.length; i++) { - // ID4[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID2, postData, file); - // } - // // Verify and save - // testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); - // } - // if (!testStatus) { - // fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); - // } - // } + @Test + @Order(6) + void testUploadPDFDuplicate() throws IOException { + System.out.println("Test (6) : Upload duplicate PDF as attachment, reference, and footnote"); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID); + 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(8) - // void testRenameEntities() { - // System.out.println("Test (8) : Rename single attachment, reference, and footnote"); - // Boolean testStatus = true; - - // try { - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - - // if ("Entity in draft mode".equals(response)) { - // String[] name = {"sample123", "reference123", "footnote123"}; - // for (int i = 0; i < facet.length; i++) { - // // Read the facet to ensure it exists - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], - // name[i]); - // if (!"Renamed".equals(response)) { - // testStatus = false; - // System.out.println(facet[i] + " was not renamed: " + response); - // } - // } - // // Save entity draft if everything is renamed - // if (testStatus) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!"Saved".equals(response)) { - // testStatus = false; - // System.out.println("Entity draft was not saved: " + response); - // } - // } else { - // // Attempt save despite potential rename failures - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } else { - // testStatus = false; - // System.out.println("Entity was not put into draft mode: " + response); - // } - // } catch (Exception e) { - // testStatus = false; - // System.out.println("Exception during renaming entities: " + e.getMessage()); - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Entity in draft mode".equals(response)) { + Boolean allFacetsFailedCorrectly = true; + for (int i = 0; i < facet.length; i++) { + List facetResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, file); + allFacetsFailedCorrectly &= checkDuplicateCreation(facet[i], facetResponse); + } + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (!allFacetsFailedCorrectly) { + fail("One or more facets were incorrectly accepted as new."); + } + } else { + fail("Entity could not be edited to draft mode."); + } + } - // if (!testStatus) { - // fail("There was an error during the rename test process."); - // } - // } + @Test + @Order(7) + void testUploadSinglePDFWithAttachmentReferenceFootnote() throws IOException { + System.out.println( + "Test (7) : Upload duplicate PDF in different entity with attachment, reference, and footnote"); + Boolean testStatus = false; + // Create a new entity draft + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID2 = response; + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // @Test - // @Order(9) - // void testCreateEntitiesWithUnsupportedCharacter() throws IOException { - // System.out.println("Test (9): Create attachments with unsupported characters"); - // boolean testStatus = false; + if ("Saved".equals(response)) { + response = api.checkEntity(appUrl, entityName, entityID2); + if ("Entity exists".equals(response)) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Could not create entity"); + } - // 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("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, entityID); - // if (!"Entity in draft mode".equals(response)) { - // fail("Entity not in draft mode: " + response); - // return; - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // for (int i = 0; i < facet.length; i++) { - // postData.put("up__ID", entityID); - // List createResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, - // tempFile); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID2); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // String check = createResponse.get(0); - // if (!"Attachment created".equals(check)) { - // System.out.println("Failed to create attachment for facet: " + facet[i]); - // continue; - // } + // Edit entity to draft mode + response = api.editEntityDraft(appUrl, entityName, srvpath, entityID2); + if ("Entity in draft mode".equals(response)) { + // Create attachment, reference, and footnote + for (int i = 0; i < facet.length; i++) { + ID4[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID2, postData, file); + } + // Verify and save + testStatus = verifyDraftAndSave(appUrl, serviceName, entityName, entityID2, ID4); + } + if (!testStatus) { + fail("Could not upload sample.pdf as an attachment, reference, or footnote: " + response); + } + } - // String restrictedName = "a/\\bc.pdf"; - // response = - // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); - // } + @Test + @Order(8) + void testRenameEntities() { + System.out.println("Test (8) : Rename single attachment, reference, and footnote"); + Boolean testStatus = true; - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" - // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // if (response.equals(expected)) { - // api.deleteEntityDraft(appUrl, entityName, entityID); - // testStatus = true; - // } + try { + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!testStatus) { - // fail("Facets renamed with restricted characters were not correctly rejected."); - // } - // } + if ("Entity in draft mode".equals(response)) { + String[] name = {"sample123", "reference123", "footnote123"}; + for (int i = 0; i < facet.length; i++) { + // Read the facet to ensure it exists + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); + if (!"Renamed".equals(response)) { + testStatus = false; + System.out.println(facet[i] + " was not renamed: " + response); + } + } + // Save entity draft if everything is renamed + if (testStatus) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Saved".equals(response)) { + testStatus = false; + System.out.println("Entity draft was not saved: " + response); + } + } else { + // Attempt save despite potential rename failures + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } else { + testStatus = false; + System.out.println("Entity was not put into draft mode: " + response); + } + } catch (Exception e) { + testStatus = false; + System.out.println("Exception during renaming entities: " + e.getMessage()); + } - // @Test - // @Order(10) - // void testRenameEntitiesWithUnsupportedCharacter() { - // System.out.println("Test (10) : Rename attachments with unsupported characters"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - // if (response.equals("Renamed")) counter++; - // } - // if (counter >= 2) { - // counter = -1; // Reset counter for the next check - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains - // unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" - // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // if (response.equals(expected)) { - // for (int i = 0; i < facet.length; i++) { - // response = - // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], - // "sample.pdf"); - // } - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment was renamed with unsupported characters"); - // } - // } + if (!testStatus) { + fail("There was an error during the rename test process."); + } + } - // @Test - // @Order(11) - // void testRenameMultipleEntityComponents() { - // System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); - // boolean testStatus = true; - - // String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!"Entity in draft mode".equals(draftResponse)) { - // fail("Entity is not in draft mode."); - // return; - // } - // String[] name = {"sample1234", "reference1234", "footnote1234"}; - // String[] name2 = {"sample12345", "reference12345", "footnote12345"}; - // for (int i = 0; i < facet.length; i++) { - // // Read the facet to ensure it exists - // testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); - // testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); - // } - // // Save the draft if all renames succeeded - // if (testStatus) { - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (!"Saved".equals(saveResponse)) { - // fail("Entity draft was not saved after renaming."); - // } - // } else { - // // Save draft even if renaming failed to preserve state - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // fail("One or more components were not renamed."); - // } - // } + @Test + @Order(9) + void testCreateEntitiesWithUnsupportedCharacter() throws IOException { + System.out.println("Test (9): Create attachments with unsupported characters"); + boolean testStatus = false; - // @Test - // @Order(12) - // void testRenameSingleDuplicate() { - // System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String[] name = {"sample1234", "reference1234", "footnote1234"}; - // String[] name2 = {"sample123456", "reference123456", "footnote123456"}; - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); - // if (response.equals("Renamed")) counter++; - // } - // if (counter >= 2) { - // counter = -1; // Reset counter for the next check - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // String.format( - // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already - // exists. Rename the object and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named - // \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An - // object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: - // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", - // name[1], name[0], name[2]); - // if (response.equals(expected)) { - // for (int i = 0; i < facet.length; i++) { - // // Attempt to rename again with a different name - // response = - // api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); - // if (response.equals("Renamed")) counter++; - // } - // } - // if (counter >= 2) { - // // If all renames were successful, save the draft - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } else { - // testStatus = false; - // fail("Attachment was renamed"); - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // @Test - // @Order(13) - // void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { - // System.out.println( - // "Test (13) : Rename multiple files out of which one file name contains unsupported - // characters"); - // boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String[] names = {"summary_1234", "reference_4567", "note/invalid"}; - - // if (response.equals("Entity in draft mode")) { - // int successCount = 0; - // for (int i = 0; i < facet.length; i++) { - // response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], - // names[i]); - // if (response.equals("Renamed")) successCount++; - // } + File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - // if (successCount >= 2) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // response = - // api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) testStatus = true; - // } - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } + Map postData = new HashMap<>(); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // if (!testStatus) { - // fail("Attachment was renamed with unsupported characters"); - // } - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Entity in draft mode".equals(response)) { + fail("Entity not in draft mode: " + response); + return; + } - // @Test - // @Order(14) - // void testRenameToValidateNames() throws IOException { - // System.out.println("Test (14) : Rename attachments to validate names"); - // String[] generatedIDs = new String[3]; - // String[] duplicateIDs = new String[1]; - // boolean testStatus = false, allRenamedSuccessfully = true; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID3 = response; + for (int i = 0; i < facet.length; i++) { + postData.put("up__ID", entityID); + List createResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID, srvpath, postData, tempFile); + + String check = createResponse.get(0); + if (!"Attachment created".equals(check)) { + System.out.println("Failed to create attachment for facet: " + facet[i]); + continue; + } - // String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; - // String duplicateName = "duplicateName.pdf"; + String restrictedName = "a/\\bc.pdf"; + response = + api.renameAttachment(appUrl, entityName, facet[i], entityID, ID4[i], restrictedName); + } - // 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"); + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // // Creation of attachment, reference and footnote - // for (int i = 0; i < facet.length; i++) { - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - // generatedIDs[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // response = - // api.renameAttachment( - // appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); - // allRenamedSuccessfully &= "Renamed".equals(response); - // } - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // // Creating duplicate name for last facet - // duplicateIDs[0] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[2], entityID3, postData, file); - // String response2 = - // api.renameAttachment( - // appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); - - // if (allRenamedSuccessfully && "Renamed".equals(response2)) { - // 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: references\\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: footnotes\\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 expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"a/\\bc.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + if (response.equals(expected)) { + api.deleteEntityDraft(appUrl, entityName, entityID); + testStatus = true; + } - // @Test - // @Order(15) - // void testRenameEntitiesWithoutSDMRole() throws IOException { - // System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); - // boolean testStatus = true; - // try { - // String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if ("Entity in draft mode".equals(apiResponse)) { - // String[] name = {"sample456", "reference456", "footnote456"}; - // for (int i = 0; i < facet.length; i++) { - // apiResponse = - // apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], - // name[i]); - // if (!"Renamed".equals(apiResponse)) { - // testStatus = false; - // } - // } - // if (testStatus) { - // apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "[{\"code\":\"\",\"message\":\"Could not update the following files. - // \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update - // attachments. Kindly contact the admin\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not - // update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required - // permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (!apiResponse.equals(expected)) { - // testStatus = false; - // } - // } else { - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // } catch (Exception e) { - // testStatus = false; - // } - // if (!testStatus) { - // fail("Attachment got renamed without SDM roles."); - // } - // } + if (!testStatus) { + fail("Facets renamed with restricted characters were not correctly rejected."); + } + } - // @Test - // @Order(16) - // void testDeleteSingleAttachment() throws IOException { - // System.out.println("Test (16) : Delete single attachment, reference, and footnote"); - // Boolean testStatus = false; - // counter = -1; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - // if (response.equals("Deleted")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // counter = -1; // Reset counter for the next check - // if (response.equals("Saved")) { - // for (int i = 0; i < facet.length; i++) { - // response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); - // if (response.equals("Could not read Attachment")) counter++; - // } - // if (counter >= 2) testStatus = true; - // else fail("Could not read deleted facets"); - // } else { - // fail("Could not save entity after deletion"); - // } - // } - // } + @Test + @Order(10) + void testRenameEntitiesWithUnsupportedCharacter() { + System.out.println("Test (10) : Rename attachments with unsupported characters"); + Boolean testStatus = false; - // @Test - // @Order(17) - // void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { - // System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Entity in draft mode")) { - // for (int i = 0; i < facet.length; i++) { - // String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - // String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - // if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; - // } - // } - // if (counter >= 2) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // if (response.equals("Saved")) { - // for (int i = 0; i < facet.length; i++) { - // String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); - // String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); - // if (response1.equals("Could not read " + facet[i]) - // && response2.equals("Could not read " + facet[i])) { - // counter++; - // } - // } - // if (counter >= 2) testStatus = true; - // else fail("Could not read deleted facets"); - // } else fail("Could not save entity after deletion"); - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String[] name = {"sample/1234", "reference1/234", "footnote1/234"}; + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + if (response.equals("Renamed")) counter++; + } + if (counter >= 2) { + counter = -1; // Reset counter for the next check + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"reference1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sample/1234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"footnote1/234\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + if (response.equals(expected)) { + for (int i = 0; i < facet.length; i++) { + response = + api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], "sample.pdf"); + } + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment was renamed with unsupported characters"); + } + } - // @Test - // @Order(18) - // void testUploadBlockedMimeType() throws IOException { - // System.out.println("Test (18) : Upload blocked mimeType .rtf"); - // Boolean testStatus = false; + @Test + @Order(11) + void testRenameMultipleEntityComponents() { + System.out.println("Test (11) : Rename multiple attachments, references, and footnotes"); + boolean testStatus = true; + + String draftResponse = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Entity in draft mode".equals(draftResponse)) { + fail("Entity is not in draft mode."); + return; + } + String[] name = {"sample1234", "reference1234", "footnote1234"}; + String[] name2 = {"sample12345", "reference12345", "footnote12345"}; + for (int i = 0; i < facet.length; i++) { + // Read the facet to ensure it exists + testStatus &= renameAndCheck(facet[i], ID2[i], entityID, name[i]); + testStatus &= renameAndCheck(facet[i], ID3[i], entityID, name2[i]); + } + // Save the draft if all renames succeeded + if (testStatus) { + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (!"Saved".equals(saveResponse)) { + fail("Entity draft was not saved after renaming."); + } + } else { + // Save draft even if renaming failed to preserve state + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + fail("One or more components were not renamed."); + } + } - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID2 = response; + @Test + @Order(12) + void testRenameSingleDuplicate() { + System.out.println("Test (12) : Rename duplicates for attachment, reference, and footnote"); + Boolean testStatus = false; + + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String[] name = {"sample1234", "reference1234", "footnote1234"}; + String[] name2 = {"sample123456", "reference123456", "footnote123456"}; + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name[i]); + if (response.equals("Renamed")) counter++; + } + if (counter >= 2) { + counter = -1; // Reset counter for the next check + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + String.format( + "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"%s\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}", + name[1], name[0], name[2]); + if (response.equals(expected)) { + for (int i = 0; i < facet.length; i++) { + // Attempt to rename again with a different name + response = + api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], name2[i]); + if (response.equals("Renamed")) counter++; + } + } + if (counter >= 2) { + // If all renames were successful, save the draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + testStatus = true; + } + } else { + testStatus = false; + fail("Attachment was renamed"); + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + @Test + @Order(13) + void testRenameMultipleEntitiesWithOneUnsupportedCharacter() { + System.out.println( + "Test (13) : Rename multiple files out of which one file name contains unsupported characters"); + boolean testStatus = false; - // 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"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String[] names = {"summary_1234", "reference_4567", "note/invalid"}; - // boolean allBlocked = true; - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, - // file); + if (response.equals("Entity in draft mode")) { + int successCount = 0; + for (int i = 0; i < facet.length; i++) { + response = api.renameAttachment(appUrl, entityName, facet[i], entityID, ID3[i], names[i]); + if (response.equals("Renamed")) successCount++; + } - // 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 (successCount >= 2) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"note/invalid\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + response = + api.renameAttachment(appUrl, entityName, facet[2], entityID, ID3[2], "note_valid"); + 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 (!expectedJson.equals(actualResponse)) { - // allBlocked = false; - // System.out.println( - // "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); - // } - // } + if (!testStatus) { + fail("Attachment was renamed with unsupported characters"); + } + } - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if ("Saved".equals(response) && allBlocked) { - // testStatus = true; - // } - // } + @Test + @Order(14) + void testRenameToValidateNames() throws IOException { + System.out.println("Test (14) : Rename attachments to validate names"); + String[] generatedIDs = new String[3]; + String[] duplicateIDs = new String[1]; + boolean testStatus = false, allRenamedSuccessfully = true; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID3 = response; - // if (!testStatus) { - // fail("Attachment got uploaded with blocked .rtf MIME type"); - // } - // } + String[] invalidNames = {"Restricted/Character", " ", "duplicateName.pdf"}; + String duplicateName = "duplicateName.pdf"; - // @Test - // @Order(19) - // void testDeleteEntity() { - // System.out.println("Test (19) : Delete entity"); - // Boolean testStatus = false; - // String response = api.deleteEntity(appUrl, entityName, entityID); - // String response2 = api.deleteEntity(appUrl, entityName, entityID2); - // if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = - // true; - // if (!testStatus) fail("Could not delete entity"); - // } + 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(20) - // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - // System.out.println("Test (20) : Rename & Update secondary property before entity is saved"); - // System.out.println("Creating entity"); + // Creation of attachment, reference and footnote + for (int i = 0; i < facet.length; i++) { + File file = new File(classLoader.getResource("sample2.pdf").getFile()); + generatedIDs[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + response = + api.renameAttachment( + appUrl, entityName, facet[i], entityID3, generatedIDs[i], invalidNames[i]); + allRenamedSuccessfully &= "Renamed".equals(response); + } + File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Creating duplicate name for last facet + duplicateIDs[0] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[2], entityID3, postData, file); + String response2 = + api.renameAttachment( + appUrl, entityName, facet[2], entityID3, duplicateIDs[0], duplicateName); + + if (allRenamedSuccessfully && "Renamed".equals(response2)) { + 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: references\\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: footnotes\\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; + } + } - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @Test + @Order(15) + void testRenameEntitiesWithoutSDMRole() throws IOException { + System.out.println("Test (15) : Rename attachments where user don't have SDM-Roles"); + boolean testStatus = true; + try { + String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Entity in draft mode".equals(apiResponse)) { + String[] name = {"sample456", "reference456", "footnote456"}; + for (int i = 0; i < facet.length; i++) { + apiResponse = + apiNoRoles.renameAttachment(appUrl, entityName, facet[i], entityID, ID[i], name[i]); + if (!"Renamed".equals(apiResponse)) { + testStatus = false; + } + } + if (testStatus) { + apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 reference123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 sample123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"code\":\"\",\"message\":\"Could not update the following files. \\n\\n\\t\\u2022 footnote123\\n\\nYou do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (!apiResponse.equals(expected)) { + testStatus = false; + } + } else { + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + } catch (Exception e) { + testStatus = false; + } + if (!testStatus) { + fail("Attachment got renamed without SDM roles."); + } + } - // if (!response.equals("Could not create entity")) { - // entityID3 = response; + @Test + @Order(16) + void testDeleteSingleAttachment() throws IOException { + System.out.println("Test (16) : Delete single attachment, reference, and footnote"); + Boolean testStatus = false; + counter = -1; - // System.out.println("Creating attachment, reference, and footnote"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + response = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + if (response.equals("Deleted")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + counter = -1; // Reset counter for the next check + if (response.equals("Saved")) { + for (int i = 0; i < facet.length; i++) { + response = api.readAttachment(appUrl, entityName, facet[i], entityID, ID[i]); + if (response.equals("Could not read Attachment")) counter++; + } + if (counter >= 2) testStatus = true; + else fail("Could not read deleted facets"); + } else { + fail("Could not save entity after deletion"); + } + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + @Test + @Order(17) + void testDeleteMultipleAttachmentsReferencesFootnotes() throws IOException { + System.out.println("Test (17) : Delete multiple attachments, references, and footnotes"); + Boolean testStatus = false; - // 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"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Entity in draft mode")) { + for (int i = 0; i < facet.length; i++) { + String response1 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + String response2 = api.deleteAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + if (response1.equals("Deleted") && response2.equals("Deleted")) counter++; + } + } + if (counter >= 2) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + if (response.equals("Saved")) { + for (int i = 0; i < facet.length; i++) { + String response1 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID2[i]); + String response2 = api.readAttachment(appUrl, entityName, facet[i], entityID, ID3[i]); + if (response1.equals("Could not read " + facet[i]) + && response2.equals("Could not read " + facet[i])) { + counter++; + } + } + if (counter >= 2) testStatus = true; + else fail("Could not read deleted facets"); + } else fail("Could not save entity after deletion"); + } - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + @Test + @Order(18) + void testUploadBlockedMimeType() throws IOException { + System.out.println("Test (18) : Upload blocked mimeType .rtf"); + Boolean testStatus = false; - // System.out.println("Attachments, References, and Footnotes created"); - - // // Use valid dropdown value for customProperty1 - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; - - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - - // // Update customProperty1 (String - dropdown value) - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - - // // Update customProperty2 (Integer) - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - - // // Update customProperty5 (DateTime) - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - - // // Update customProperty6 (Boolean) - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // counter++; - // } - // } + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID2 = response; - // if (counter >= 2) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // } - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + 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"); - // @Test - // @Order(21) - // void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { - // System.out.println("Test (21): Rename & Update secondary property after entity is saved"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // System.out.println("Editing entity"); - - // if (response.equals("Entity in draft mode")) { - // // Sample secondary properties - // String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; - // Integer secondaryPropertyInt = 42; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - - // System.out.println("Renaming and updating secondary properties for attachment"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Saved")) { - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for attachment"); - // } - // // Clean up - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); - // } - // if (!testStatus) fail("Could not update secondary properties after entity is saved"); - // } + boolean allBlocked = true; + for (int i = 0; i < facet.length; i++) { + List createResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID2, srvpath, postData, file); - // @Test - // @Order(22) - // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { - // System.out.println( - // "Test (22): 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 (response != "Could not create entity") { - // entityID3 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + 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.\"}}"; - // 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"); + if (!expectedJson.equals(actualResponse)) { + allBlocked = false; + System.out.println( + "Facet " + facet[i] + " incorrectly accepted blocked mimeType: " + actualResponse); + } + } + + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if ("Saved".equals(response) && allBlocked) { + testStatus = true; + } + } + + if (!testStatus) { + fail("Attachment got uploaded with blocked .rtf MIME type"); + } + } + + @Test + @Order(19) + void testDeleteEntity() { + System.out.println("Test (19) : Delete entity"); + Boolean testStatus = false; + String response = api.deleteEntity(appUrl, entityName, entityID); + String response2 = api.deleteEntity(appUrl, entityName, entityID2); + if (response.equals("Entity Deleted") && response2.equals("Entity Deleted")) testStatus = true; + if (!testStatus) fail("Could not delete entity"); + } + + @Test + @Order(20) + void testUpdateValidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + System.out.println("Test (20) : 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.equals("Could not create entity")) { + entityID3 = response; + + System.out.println("Creating attachment, reference, and footnote"); + + 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"); + + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + + System.out.println("Attachments, References, and Footnotes created"); + + // Use valid dropdown value for customProperty1 + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + String[] name = {"sample1234.pdf", "reference1234.pdf", "footnote1234.pdf"}; + + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + + // Update customProperty1 (String - dropdown value) + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + + // Update customProperty2 (Integer) + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + + // Update customProperty5 (DateTime) + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + + // Update customProperty6 (Boolean) + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + counter++; + } + } + + if (counter >= 2) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + } + if (response.equals("Saved")) { + testStatus = true; + } + } + + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(21) + void testUpdateValidSecondaryProperty_afterEntityIsSaved_single() { + System.out.println("Test (21): Rename & Update secondary property after entity is saved"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + System.out.println("Editing entity"); + + if (response.equals("Entity in draft mode")) { + // Sample secondary properties + String name[] = {"sample.pdf", "reference_sample.pdf", "footnote_sample.pdf"}; + Integer secondaryPropertyInt = 42; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + + System.out.println("Renaming and updating secondary properties for attachment"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name[i]); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Saved")) { + testStatus = true; + System.out.println("Renamed & updated Secondary properties for attachment"); + } + // Clean up + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (!deleteEntityResponse.equals("Entity Deleted")) fail("Could not delete entity"); + } + if (!testStatus) fail("Could not update secondary properties after entity is saved"); + } + + @Test + @Order(22) + void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_single() throws IOException { + System.out.println( + "Test (22): 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 (response != "Could not create entity") { + entityID3 = response; + 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"); + + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + // Prepare test data + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, or footnote"); + } + + @Test + @Order(23) + void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { + System.out.println( + "Test (23): 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.equals("Entity in draft mode")) { + String name1 = "sample.pdf"; + Integer secondaryPropertyInt = 12; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testidinvalid"; + + for (int i = 0; i < facet.length; i++) { + // Rename and update secondary properties + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for invalid ID + String updateSecondaryPropertyResponse4 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) counter++; + } + if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals("sample.pdf", FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for attachment, reference, footnote is unsuccessfull"); + } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (!deleteEntityResponse.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } + if (!testStatus) + fail( + "Could not update secondary property after entity is saved for attachment, reference, or footnote"); + } + + @Test + @Order(24) + void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (24): Rename & Update valid secondary properties for multiple facets 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"); + 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"); + + System.out.println("Creating attachment, reference, and footnote PDF"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + + System.out.println("Creating attachment, reference, and footnote TXT"); + file = new File(classLoader.getResource("sample.txt").getFile()); + postData.put("mimeType", "application/txt"); + for (int i = 0; i < facet.length; i++) { + ID2[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + + System.out.println("Creating attachment, reference, and footnote EXE"); + file = new File(classLoader.getResource("sample.exe").getFile()); + postData.put("mimeType", "application/exe"); + for (int i = 0; i < facet.length; i++) { + ID3[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } + Boolean Updated1[] = new Boolean[3]; + Boolean Updated2[] = new Boolean[3]; + Boolean Updated3[] = new Boolean[3]; + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // PDF + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + // 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, facet[i], entityID3, ID2[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } + + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + for (int i = 0; i < facet.length; i++) { + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponseEXE3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); + + if (updateSecondaryPropertyResponseEXE1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated") + && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + 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"); + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } - // // Prepare test data - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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 for attachment, reference, - // or footnote"); - // } + @Test + @Order(25) + void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { + System.out.println( + "Test (25): Rename & Update valid secondary properties for multiple facets after entity is saved"); + System.out.println("Editing entity"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Entity in draft mode")) { + Boolean Updated1[] = new Boolean[3]; + Boolean Updated2[] = new Boolean[3]; + Boolean Updated3[] = new Boolean[3]; + + String name1 = "sample1.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } - // @Test - // @Order(23) - // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_single() throws IOException { - // System.out.println( - // "Test (23): 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.equals("Entity in draft mode")) { - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt = 12; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testidinvalid"; - - // for (int i = 0; i < facet.length; i++) { - // // Rename and update secondary properties - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], 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, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for invalid ID - // String updateSecondaryPropertyResponse4 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidProperty); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) counter++; - // } - // if (counter >= 2) response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals("sample.pdf", FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for attachment, reference, footnote is - // unsuccessfull"); - // } - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (!deleteEntityResponse.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } - // if (!testStatus) - // fail( - // "Could not update secondary property after entity is saved for attachment, reference, - // or footnote"); - // } + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + // 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, facet[i], entityID3, ID2[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } - // @Test - // @Order(24) - // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (24): Rename & Update valid secondary properties for multiple facets 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; + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + for (int i = 0; i < facet.length; i++) { + // Update secondary properties for Drop down + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponseEXE3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyDate); + + if (updateSecondaryPropertyResponseEXE1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated") + && updateSecondaryPropertyResponseEXE3.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } - // System.out.println("Entity created"); - // ClassLoader classLoader = getClass().getClassLoader(); + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + 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"); + } + } - // 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_beforeEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (26): Rename & Update invalid and valid secondary properties for multiple facets before entity is saved"); + System.out.println("Creating entity"); - // System.out.println("Creating attachment, reference, and footnote PDF"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // System.out.println("Creating attachment, reference, and footnote TXT"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // postData.put("mimeType", "application/txt"); - // for (int i = 0; i < facet.length; i++) { - // ID2[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + if (!"Could not create entity".equals(response)) { + entityID3 = response; + System.out.println("Entity created"); - // System.out.println("Creating attachment, reference, and footnote EXE"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // postData.put("mimeType", "application/exe"); - // for (int i = 0; i < facet.length; i++) { - // ID3[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } - // Boolean Updated1[] = new Boolean[3]; - // Boolean Updated2[] = new Boolean[3]; - // Boolean Updated3[] = new Boolean[3]; - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // // PDF - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], 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 updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID3); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // // 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, facet[i], entityID3, ID2[i], - // bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + // Create PDF attachments + postData.put("mimeType", "application/pdf"); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + for (int i = 0; i < facet.length; i++) { + ID[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // for (int i = 0; i < facet.length; i++) { - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponseEXE3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyDate); - - // if (updateSecondaryPropertyResponseEXE1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated") - // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // 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"); - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + // Create TXT attachments + postData.put("mimeType", "application/txt"); + file = new File(classLoader.getResource("sample.txt").getFile()); + for (int i = 0; i < facet.length; i++) { + ID2[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } - // @Test - // @Order(25) - // void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { - // System.out.println( - // "Test (25): Rename & Update valid secondary properties for multiple facets after entity - // is saved"); - // System.out.println("Editing entity"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Entity in draft mode")) { - // Boolean Updated1[] = new Boolean[3]; - // Boolean Updated2[] = new Boolean[3]; - // Boolean Updated3[] = new Boolean[3]; - - // String name1 = "sample1.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } + // Create EXE attachments + postData.put("mimeType", "application/exe"); + file = new File(classLoader.getResource("sample.exe").getFile()); + for (int i = 0; i < facet.length; i++) { + ID3[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], entityID3, postData, file); + } - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // // 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, facet[i], entityID3, ID2[i], - // bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + Boolean[] Updated1 = new Boolean[3]; + Boolean[] Updated2 = new Boolean[3]; + Boolean[] Updated3 = new Boolean[3]; - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // for (int i = 0; i < facet.length; i++) { - // // Update secondary properties for Drop down - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponseEXE3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyDate); - - // if (updateSecondaryPropertyResponseEXE1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated") - // && updateSecondaryPropertyResponseEXE3.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } + String name1 = "sample1234.pdf"; + String dropdownValue = + integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // 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"); - // } - // } + // Update PDF properties + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String renameResp = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + String upd2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + String upd3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + String upd4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + String updInvalid = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + if ("Renamed".equals(renameResp) + && "Updated".equals(upd1) + && "Updated".equals(upd2) + && "Updated".equals(upd3) + && "Updated".equals(upd4) + && "Updated".equals(updInvalid)) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } - // @Test - // @Order(26) - // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (26): Rename & Update invalid and valid secondary properties for multiple facets - // before entity is saved"); - // System.out.println("Creating entity"); + // Update TXT properties + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); + String upd = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], bodyBool); + if ("Updated".equals(upd)) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // Update EXE properties + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValueExe = integrationTestUtils.getDropDownValue(); + String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - // if (!"Could not create entity".equals(response)) { - // entityID3 = response; - // System.out.println("Entity created"); + for (int i = 0; i < facet.length; i++) { + RequestBody bodyDropdownExe = + RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); + RequestBody bodyIntExe = + RequestBody.create( + MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); + + String upd1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); + String upd2 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); + + if ("Updated".equals(upd1) && "Updated".equals(upd2)) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID3); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) + && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - // // Create PDF attachments - // postData.put("mimeType", "application/pdf"); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - // // Create TXT attachments - // postData.put("mimeType", "application/txt"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID2[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + // Verify PDF metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals(expectedNames[0], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertNull(metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } - // // Create EXE attachments - // postData.put("mimeType", "application/exe"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // for (int i = 0; i < facet.length; i++) { - // ID3[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], entityID3, postData, file); - // } + // Verify TXT metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + assertEquals(expectedNames[1], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertNull(metadata.get("customProperty1_code")); + assertNull(metadata.get("customProperty2")); + assertTrue((Boolean) metadata.get("customProperty6")); + assertNull(metadata.get("customProperty5")); + } - // Boolean[] Updated1 = new Boolean[3]; - // Boolean[] Updated2 = new Boolean[3]; - // Boolean[] Updated3 = new Boolean[3]; - - // String name1 = "sample1234.pdf"; - // String dropdownValue = - // integrationTestUtils.getDropDownValue(); // returns a plain string like "option-123" - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - - // // Update PDF properties - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String renameResp = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty2\" : " + secondaryPropertyInt1 + " }"); - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // "{ \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\" }"); - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // String upd2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // String upd3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // String upd4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - // String updInvalid = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - // if ("Renamed".equals(renameResp) - // && "Updated".equals(upd1) - // && "Updated".equals(upd2) - // && "Updated".equals(upd3) - // && "Updated".equals(upd4) - // && "Updated".equals(updInvalid)) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } + // Verify EXE metadata + for (int i = 0; i < facet.length; i++) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + assertEquals(expectedNames[2], metadata.get("fileName")); + assertNull(metadata.get("customProperty3")); + assertNull(metadata.get("customProperty4")); + assertEquals( + dropdownValueExe, + metadata.get("customProperty1_code")); // Adjust expected value if needed + assertEquals(1234, metadata.get("customProperty2")); + } - // // Update TXT properties - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty6\" : true }"); - // String upd = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID2[i], - // bodyBool); - // if ("Updated".equals(upd)) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + 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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update unsuccessful for invalid properties and successful for valid attachments"); + } + } + } - // // Update EXE properties - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValueExe = integrationTestUtils.getDropDownValue(); - // String jsonDropdownExe = "{ \"customProperty1_code\" : \"" + dropdownValueExe + "\" }"; - - // for (int i = 0; i < facet.length; i++) { - // RequestBody bodyDropdownExe = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdownExe); - // RequestBody bodyIntExe = - // RequestBody.create( - // MediaType.parse("application/json"), "{ \"customProperty2\" : 1234 }"); - - // String upd1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdownExe); - // String upd2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyIntExe); - - // if ("Updated".equals(upd1) && "Updated".equals(upd2)) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } - // if (Arrays.stream(Updated1).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated2).allMatch(Boolean.TRUE::equals) - // && Arrays.stream(Updated3).allMatch(Boolean.TRUE::equals)) { - - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // String[] expectedNames = {"sample.pdf", "sample.txt", "sample.exe"}; - - // // Verify PDF metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals(expectedNames[0], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertNull(metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } + @Test + @Order(27) + void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (27): 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.equals("Entity in draft mode")) { + Boolean Updated1[] = new Boolean[3]; + Boolean Updated2[] = new Boolean[3]; + Boolean Updated3[] = new Boolean[3]; + String name1 = "sample.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + String dropdownValue = integrationTestUtils.getDropDownValue(); + System.out.println("drop down value is: " + dropdownValue); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + + // PDF + System.out.println("Renaming and updating secondary properties for PDF"); + for (int i = 0; i < facet.length; i++) { + String response1 = + api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); + // Update secondary properties for String + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); + // Update secondary properties for LocalDateTime + RequestBody bodyDate = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyDate); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyBool); + // Update invalid secondary property + String updateSecondaryPropertyResponse5 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); + + if (response1.equals("Renamed") + && updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponse2.equals("Updated") + && updateSecondaryPropertyResponse3.equals("Updated") + && updateSecondaryPropertyResponse4.equals("Updated") + && updateSecondaryPropertyResponse5.equals("Updated")) { + Updated1[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); + } + } + // TXT + System.out.println("Renaming and updating secondary properties for TXT"); + for (int i = 0; i < facet.length; i++) { + // 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, facet[i], entityID3, ID2[i], bodyBool); + if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { + Updated2[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); + } + } - // // Verify TXT metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - // assertEquals(expectedNames[1], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertNull(metadata.get("customProperty1_code")); - // assertNull(metadata.get("customProperty2")); - // assertTrue((Boolean) metadata.get("customProperty6")); - // assertNull(metadata.get("customProperty5")); - // } + Integer secondaryPropertyInt3 = 12; + // EXE + System.out.println("Renaming and updating secondary properties for EXE"); + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + for (int i = 0; i < facet.length; i++) { + // Update secondary properties for String + System.out.println("drop down value is: " + dropdownValue1); + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], bodyInt); + + if (updateSecondaryPropertyResponse1.equals("Updated") + && updateSecondaryPropertyResponseEXE2.equals("Updated")) { + Updated3[i] = true; + System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); + } + } - // // Verify EXE metadata - // for (int i = 0; i < facet.length; i++) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - // assertEquals(expectedNames[2], metadata.get("fileName")); - // assertNull(metadata.get("customProperty3")); - // assertNull(metadata.get("customProperty4")); - // assertEquals( - // dropdownValueExe, - // metadata.get("customProperty1_code")); // Adjust expected value if needed - // assertEquals(1234, metadata.get("customProperty2")); - // } + if (Updated1[0] + && Updated1[1] + && Updated1[2] + && Updated2[0] + && Updated2[1] + && Updated2[2] + && Updated3[0] + && Updated3[1] + && Updated3[2]) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; + // for PDF + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); + assertEquals(name[0], FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertNull(FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + // for TXT + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); + assertEquals(name[1], FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertNull(FacetMetadata.get("customProperty1_code")); + assertNull(FacetMetadata.get("customProperty2")); + assertFalse((Boolean) FacetMetadata.get("customProperty6")); + assertNull(FacetMetadata.get("customProperty5")); + } + // for EXE + for (int i = 0; i < facet.length; i++) { + Map FacetMetadata = + api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); + assertEquals(name[2], FacetMetadata.get("fileName")); + assertNull(FacetMetadata.get("customProperty3")); + assertNull(FacetMetadata.get("customProperty4")); + assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); + assertEquals(12, FacetMetadata.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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessful for invalid properties and successful for valid - // attachments"); - // } - // } - // } + 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: references\\nPage: IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); + } + 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 (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + @Test + @Order(28) + void testNAttachments_NewEntity() throws IOException { + System.out.println( + "Test (28): 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, facet[0], entityID4, srvpath, postData1, file); + if (createResponse1.get(0).equals("Attachment created")) { + ID[0] = createResponse1.get(1); + System.out.println("Attachment created"); + } - // @Test - // @Order(27) - // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (27): 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.equals("Entity in draft mode")) { - // Boolean Updated1[] = new Boolean[3]; - // Boolean Updated2[] = new Boolean[3]; - // Boolean Updated3[] = new Boolean[3]; - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // System.out.println("drop down value is: " + dropdownValue); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - - // // PDF - // System.out.println("Renaming and updating secondary properties for PDF"); - // for (int i = 0; i < facet.length; i++) { - // String response1 = - // api.renameAttachment(appUrl, entityName, facet[i], entityID3, ID[i], name1); - // // Update secondary properties for String - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], bodyInt); - // // Update secondary properties for LocalDateTime - // RequestBody bodyDate = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyDate); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID[i], - // bodyBool); - // // Update invalid secondary property - // String updateSecondaryPropertyResponse5 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID[i], invalidPropertyPDF); - - // if (response1.equals("Renamed") - // && updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponse2.equals("Updated") - // && updateSecondaryPropertyResponse3.equals("Updated") - // && updateSecondaryPropertyResponse4.equals("Updated") - // && updateSecondaryPropertyResponse5.equals("Updated")) { - // Updated1[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " PDF"); - // } - // } - // // TXT - // System.out.println("Renaming and updating secondary properties for TXT"); - // for (int i = 0; i < facet.length; i++) { - // // 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, facet[i], entityID3, ID2[i], - // bodyBool); - // if (updateSecondaryPropertyResponseTXT1.equals("Updated")) { - // Updated2[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " TXT"); - // } - // } + 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, facet[0], entityID4, srvpath, postData2, file); + if (createResponse2.get(0).equals("Attachment created")) { + ID2[0] = createResponse2.get(1); + System.out.println("Attachment created"); + } - // Integer secondaryPropertyInt3 = 12; - // // EXE - // System.out.println("Renaming and updating secondary properties for EXE"); - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // for (int i = 0; i < facet.length; i++) { - // // Update secondary properties for String - // System.out.println("drop down value is: " + dropdownValue1); - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], entityID3, ID3[i], bodyDropdown1); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty(appUrl, entityName, facet[i], entityID3, ID3[i], - // bodyInt); - - // if (updateSecondaryPropertyResponse1.equals("Updated") - // && updateSecondaryPropertyResponseEXE2.equals("Updated")) { - // Updated3[i] = true; - // System.out.println("Renamed & updated Secondary properties for " + facet[i] + " EXE"); - // } - // } + 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, facet[0], entityID4, srvpath, postData3, file); + if (createResponse3.get(0).equals("Attachment created")) { + ID[0] = createResponse3.get(1); + System.out.println("Attachment created"); + } - // if (Updated1[0] - // && Updated1[1] - // && Updated1[2] - // && Updated2[0] - // && Updated2[1] - // && Updated2[2] - // && Updated3[0] - // && Updated3[1] - // && Updated3[2]) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // String name[] = {"sample.pdf", "sample.txt", "sample.exe"}; - // // for PDF - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID[i]); - // assertEquals(name[0], FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertNull(FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - // // for TXT - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID2[i]); - // assertEquals(name[1], FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertNull(FacetMetadata.get("customProperty1_code")); - // assertNull(FacetMetadata.get("customProperty2")); - // assertFalse((Boolean) FacetMetadata.get("customProperty6")); - // assertNull(FacetMetadata.get("customProperty5")); - // } - // // for EXE - // for (int i = 0; i < facet.length; i++) { - // Map FacetMetadata = - // api.fetchMetadata(appUrl, entityName, facet[i], entityID3, ID3[i]); - // assertEquals(name[2], FacetMetadata.get("fileName")); - // assertNull(FacetMetadata.get("customProperty3")); - // assertNull(FacetMetadata.get("customProperty4")); - // assertEquals(dropdownValue1, FacetMetadata.get("customProperty1_code")); - // assertEquals(12, FacetMetadata.get("customProperty2")); - // } + 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, facet[0], entityID4, srvpath, postData3, file); + if (createResponse4.get(0).equals("Attachment created")) { + ID4[0] = createResponse4.get(1); + System.out.println("Attachment created"); + } - // 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: references\\nPage: - // IntegrationTestEntity\",\"numericSeverity\":3},{\"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},{\"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: footnotes\\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"); - // } - // 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 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, facet[0], entityID4, srvpath, postData3, file); + if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { + testStatus = true; + ID5[0] = 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(28) - // void testNAttachments_NewEntity() throws IOException { - // System.out.println( - // "Test (28): 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; + @Test + @Order(29) + void testUploadNAttachments() throws IOException { + System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); - // System.out.println("Entity created"); + 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, facet[0], 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"); + } + } + } - // System.out.println("Creating attachment PDF"); - // ClassLoader classLoader = getClass().getClassLoader(); + @Test + @Order(30) + void testDiscardDraftWithoutAttachments() { + System.out.println("Test (30) : Discard draft without adding attachments"); + Boolean testStatus = false; - // 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"); + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID6 = response; + response = api.deleteEntityDraft(appUrl, entityName, entityID6); + if (response.equals("Entity Draft Deleted")) { + testStatus = true; + } + } + if (!testStatus) { + fail("Draft was not discarded properly"); + } + } - // List createResponse1 = - // api.createAttachment(appUrl, entityName, facet[0], entityID4, srvpath, postData1, - // file); - // if (createResponse1.get(0).equals("Attachment created")) { - // ID[0] = createResponse1.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(31) + void testDiscardDraftWithAttachments() throws IOException { + System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); + 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, facet[0], entityID4, srvpath, postData2, - // file); - // if (createResponse2.get(0).equals("Attachment created")) { - // ID2[0] = createResponse2.get(1); - // System.out.println("Attachment created"); - // } + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID6 = response; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID6); + 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 < facet.length; i++) { + List createResponse = + api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, file); + if ("Attachment created".equals(createResponse.get(0))) { + System.out.println("Attachment created in facet: " + facet[i]); + } else { + System.out.println("Attachment creation failed in facet: " + facet[i]); + } + } + response = api.deleteEntityDraft(appUrl, entityName, entityID6); + if ("Entity Draft Deleted".equals(response)) { + testStatus = true; + } + } + if (!testStatus) { + fail("Draft with attachments was not discarded properly"); + } + } - // 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, facet[0], entityID4, srvpath, postData3, - // file); - // if (createResponse3.get(0).equals("Attachment created")) { - // ID[0] = createResponse3.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(32) + void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { + System.out.println("Test (32): Upload to all facets, delete one, and create 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, facet[0], entityID4, srvpath, postData3, - // file); - // if (createResponse4.get(0).equals("Attachment created")) { - // ID4[0] = createResponse4.get(1); - // System.out.println("Attachment created"); - // } + boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // 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, facet[0], entityID4, srvpath, postData3, - // file); - // if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { - // testStatus = true; - // ID5[0] = 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"); - // } - // } + if (!"Could not create entity".equals(response)) { + entityID5 = response; + ClassLoader classLoader = getClass().getClassLoader(); + + File file1 = + new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + File file2 = + new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); + + Map postData2 = new HashMap<>(postData1); + postData2.put("up__ID", entityID5); + postData2.put("mimeType", "text/plain"); + postData2.put("createdAt", new Date().toString()); + postData2.put("createdBy", "test@test.com"); + postData2.put("modifiedBy", "test@test.com"); + + boolean allCreated = true; + for (int i = 0; i < facet.length; i++) { + List response1 = + api.createAttachment( + appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); + List response2 = + api.createAttachment( + appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); + + if (response1.get(0).equals("Attachment created") + && response2.get(0).equals("Attachment created")) { + ID4[i] = response1.get(1); // to keep one + ID5[i] = response2.get(1); // will delete this one + } else { + allCreated = false; + break; + } - // @Test - // @Order(29) - // void testUploadNAttachments() throws IOException { - // System.out.println("Test (29): Upload maximum 4 attachments in an exsisting entity"); + String deleteResponse = + api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); + if (!"Deleted".equals(deleteResponse)) { + allCreated = false; + break; + } + } - // 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, facet[0], 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"); - // } - // } - // } + if (allCreated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } - // @Test - // @Order(30) - // void testDiscardDraftWithoutAttachments() { - // System.out.println("Test (30) : Discard draft without adding attachments"); - // Boolean testStatus = false; + if (!testStatus) { + fail("Failed to upload multiple facet entries, delete one per facet and create entity"); + } + } - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID6 = response; - // response = api.deleteEntityDraft(appUrl, entityName, entityID6); - // if (response.equals("Entity Draft Deleted")) { - // testStatus = true; - // } - // } - // if (!testStatus) { - // fail("Draft was not discarded properly"); - // } - // } + @Test + @Order(33) + void testUpdateEntityDraft() throws IOException { + System.out.println("Test (33): Update entity draft with new facet content"); + boolean testStatus = false; - // @Test - // @Order(31) - // void testDiscardDraftWithAttachments() throws IOException { - // System.out.println("Test (31): Discard draft with attachments, references, and footnotes"); - // boolean testStatus = false; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID6 = response; - // 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", entityID6); - // 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 < facet.length; i++) { - // List createResponse = - // api.createAttachment(appUrl, entityName, facet[i], entityID6, srvpath, postData, - // file); - // if ("Attachment created".equals(createResponse.get(0))) { - // System.out.println("Attachment created in facet: " + facet[i]); - // } else { - // System.out.println("Attachment creation failed in facet: " + facet[i]); - // } - // } - // response = api.deleteEntityDraft(appUrl, entityName, entityID6); - // if ("Entity Draft Deleted".equals(response)) { - // testStatus = true; - // } - // } - // if (!testStatus) { - // fail("Draft with attachments was not discarded properly"); - // } - // } + 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(32) - // void testDraftUpdateUploadTwoDeleteOneAndCreate() throws IOException { - // System.out.println("Test (32): Upload to all facets, delete one, and create entity"); + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); + if ("Entity in draft mode".equals(response)) { + boolean allCreated = true; - // boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + for (int i = 0; i < facet.length; i++) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); + if (!"Attachment created".equals(createResponse.get(0))) { + allCreated = false; + } + } - // if (!"Could not create entity".equals(response)) { - // entityID5 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - - // File file1 = - // new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // File file2 = - // new File(Objects.requireNonNull(classLoader.getResource("sample.txt")).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"); - - // Map postData2 = new HashMap<>(postData1); - // postData2.put("up__ID", entityID5); - // postData2.put("mimeType", "text/plain"); - // postData2.put("createdAt", new Date().toString()); - // postData2.put("createdBy", "test@test.com"); - // postData2.put("modifiedBy", "test@test.com"); - - // boolean allCreated = true; - // for (int i = 0; i < facet.length; i++) { - // List response1 = - // api.createAttachment( - // appUrl, entityName, facet[i], entityID5, srvpath, postData1, file1); - // List response2 = - // api.createAttachment( - // appUrl, entityName, facet[i], entityID5, srvpath, postData2, file2); - - // if (response1.get(0).equals("Attachment created") - // && response2.get(0).equals("Attachment created")) { - // ID4[i] = response1.get(1); // to keep one - // ID5[i] = response2.get(1); // will delete this one - // } else { - // allCreated = false; - // break; - // } + if (allCreated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + if ("Saved".equals(response)) { + testStatus = true; + } + } + } + api.deleteEntity(appUrl, entityName, entityID5); + if (!testStatus) { + fail("Failed to update draft with new attachments for all facets"); + } + } - // String deleteResponse = - // api.deleteAttachment(appUrl, entityName, facet[i], entityID5, ID5[i]); - // if (!"Deleted".equals(deleteResponse)) { - // allCreated = false; - // break; - // } - // } + @Test + @Order(34) + void testUploadAttachmentWithoutSDMRole() throws IOException { + System.out.println("Test (34): Upload attachment across facets without SDM role"); + boolean testStatus = true; + + String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID7 = response; + 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", 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 (allCreated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } + for (int i = 0; i < facet.length; i++) { + List createResponse = + apiNoRoles.createAttachment( + appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); + String check = createResponse.get(0); + String expectedError = + "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions to upload attachments. Please contact your administrator for access.\"}}"; + if (!expectedError.equals(check)) { + testStatus = false; + } + } + } + api.deleteEntityDraft(appUrl, entityName, entityID7); + if (!testStatus) { + fail("Attachment uploaded without SDM role for one or more facets"); + } + } - // if (!testStatus) { - // fail("Failed to upload multiple facet entries, delete one per facet and create entity"); - // } - // } + @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<>(); + for (int i = 0; i < 3; i++) { + attachments.add(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(33) - // void testUpdateEntityDraft() throws IOException { - // System.out.println("Test (33): Update entity draft with new facet content"); - // boolean testStatus = false; + for (int i = 0; i < facet.length; i++) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + copyAttachmentSourceEntity, + srvpath, + postData, + file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + } + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facet[i], 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"); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + if (sourceObjectIds.size() == 6) { + String copyResponse; + int i = 0; + for (String facetName : facet) { + if (i != 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } + copyResponse = + api.copyAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + i += 2; + if (copyResponse.equals("Attachments copied successfully")) { + // Fetch copied attachment IDs from target draft + List> copiedMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + List copiedAttachmentIds = + copiedMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + 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"); + } + } - // 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 ("Entity in draft mode".equals(response)) { - // boolean allCreated = true; - - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], entityID5, srvpath, postData, tempFile); - // if (!"Attachment created".equals(createResponse.get(0))) { - // allCreated = false; - // } - // } + @Test + @Order(36) + void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (36): Copy incorrect 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")) { + if (sourceObjectIds.size() == 6) { + int i = 0; + for (String facet : facet) { + try { + List currentFacetObjectIds = + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + currentFacetObjectIds.add("incorrectObjectId"); + if (currentFacetObjectIds.size() != 3) { + fail("Not enough object IDs to copy attachments for facet: " + facet); + } + api.copyAttachment( + appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, currentFacetObjectIds); + fail("Copy attachments did not throw an error"); + } catch (IOException e) { + i += 2; + } + } + 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 (allCreated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } - // } - // api.deleteEntity(appUrl, entityName, entityID5); - // if (!testStatus) { - // fail("Failed to update draft with new attachments for all facets"); - // } - // } + @Test + @Order(37) + void testCopyAttachmentWithNotesField() throws IOException { + System.out.println( + "Test (37): Create entity with attachments containing notes in multiple facets, copy to new entity and verify notes field"); + Boolean testStatus = false; - // @Test - // @Order(34) - // void testUploadAttachmentWithoutSDMRole() throws IOException { - // System.out.println("Test (34): Upload attachment across facets without SDM role"); - // boolean testStatus = true; + copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // String response = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID7 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + String notesValue = "This is a test note for copy attachment verification"; + MediaType mediaType = MediaType.parse("application/json"); + + for (String facetName : facet) { + 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); - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } - // 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 sourceAttachmentId = createResponse.get(1); - // for (int i = 0; i < facet.length; i++) { - // List createResponse = - // apiNoRoles.createAttachment( - // appUrl, entityName, facet[i], entityID7, srvpath, postData, tempFile); - // String check = createResponse.get(0); - // String expectedError = - // "{\"error\":{\"code\":\"500\",\"message\":\"You do not have the required permissions - // to upload attachments. Please contact your administrator for access.\"}}"; - // if (!expectedError.equals(check)) { - // testStatus = false; - // } - // } - // } - // api.deleteEntityDraft(appUrl, entityName, entityID7); - // if (!testStatus) { - // fail("Attachment uploaded without SDM role for one or more facets"); - // } - // } + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - // @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<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(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"); + String updateResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateBody); - // for (int i = 0; i < facet.length; i++) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // copyAttachmentSourceEntity, - // srvpath, - // postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // } - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facet[i], 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"); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - // if (sourceObjectIds.size() == 6) { - // String copyResponse; - // int i = 0; - // for (String facetName : facet) { - // if (i != 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } - // copyResponse = - // api.copyAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - // i += 2; - // if (copyResponse.equals("Attachments copied successfully")) { - // // Fetch copied attachment IDs from target draft - // List> copiedMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // List copiedAttachmentIds = - // copiedMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // 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"); - // } - // } + if (!updateResponse.equals("Updated")) { + fail("Could not update attachment notes field in facet: " + facetName); + } + } - // @Test - // @Order(36) - // void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { - // System.out.println( - // "Test (36): Copy incorrect 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")) { - // if (sourceObjectIds.size() == 6) { - // int i = 0; - // for (String facet : facet) { - // try { - // List currentFacetObjectIds = - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - // currentFacetObjectIds.add("incorrectObjectId"); - // if (currentFacetObjectIds.size() != 3) { - // fail("Not enough object IDs to copy attachments for facet: " + facet); - // } - // api.copyAttachment( - // appUrl, entityName, facet, copyAttachmentTargetEntityEmpty, - // currentFacetObjectIds); - // fail("Copy attachments did not throw an error"); - // } catch (IOException e) { - // i += 2; - // } - // } - // 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"); - // } - // } + List objectIdsToStore = new ArrayList<>(); + for (String facetName : facet) { + List> sourceAttachmentsMetadata = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); - // @Test - // @Order(37) - // void testCopyAttachmentWithNotesField() throws IOException { - // System.out.println( - // "Test (37): Create entity with attachments containing notes in multiple facets, copy to - // new entity and verify notes field"); - // Boolean testStatus = false; + if (sourceAttachmentsMetadata.isEmpty()) { + fail("No attachments found in source entity for facet: " + facetName); + } - // copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyCustomSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // String notesValue = "This is a test note for copy attachment verification"; - // MediaType mediaType = MediaType.parse("application/json"); + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId for facet: " + facetName); + } - // for (String facetName : facet) { - // 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 sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + objectIdsToStore.add(sourceObjectId); + + 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 for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + sourceNoteValue); + } + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + int startIndex = sourceObjectIds.size(); + sourceObjectIds.addAll(objectIdsToStore); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity"); + } - // String sourceAttachmentId = createResponse.get(1); + copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); + int facetIndex = 0; + for (String facetName : facet) { + if (facetIndex > 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } - // String updateResponse = - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // copyCustomSourceEntity, - // sourceAttachmentId, - // updateBody); + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - // if (!updateResponse.equals("Updated")) { - // fail("Could not update attachment notes field in facet: " + facetName); - // } - // } + String copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // List objectIdsToStore = new ArrayList<>(); - // for (String facetName : facet) { - // List> sourceAttachmentsMetadata = - // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyCustomSourceEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity for facet: " + facetName); + } - // if (sourceAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in source entity for facet: " + facetName); - // } + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity for facet: " + facetName); + } - // Map sourceAttachmentMetadata = sourceAttachmentsMetadata.get(0); + facetIndex++; + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId for facet: " + facetName); - // } + for (String facetName : facet) { + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // objectIdsToStore.add(sourceObjectId); - - // 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 for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + sourceNoteValue); - // } - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity for facet: " + facetName); + } - // int startIndex = sourceObjectIds.size(); - // sourceObjectIds.addAll(objectIdsToStore); + 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 for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + copiedNoteValue); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyCustomTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment from target entity for facet: " + facetName); + } else { + testStatus = true; + } + } - // int facetIndex = 0; - // for (String facetName : facet) { - // if (facetIndex > 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } + if (!testStatus) { + fail( + "Could not verify that notes field was copied from source to target attachment for all facets"); + } + } - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + @Test + @Order(38) + void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { + System.out.println( + "Test (38): Verify that secondary properties are preserved when copying attachments between entities across multiple facets"); + Boolean testStatus = false; - // String copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity for facet: " + facetName); - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample1.pdf").getFile()); - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity for facet: " + facetName); - // } + List objectIdsToStore = new ArrayList<>(); - // facetIndex++; - // } + for (String facetName : facet) { + 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"); - // for (String facetName : facet) { - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity for facet: " + facetName); - // } + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } - // 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 for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + copiedNoteValue); - // } + String sourceAttachmentId = createResponse.get(1); + + 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 for facet: " + facetName); + } - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + 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 for facet: " + facetName); + } + } - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment from target entity for facet: " + facetName); - // } else { - // testStatus = true; - // } - // } + // Save source entity to persist attachments before fetching metadata + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after creating attachments"); + } - // if (!testStatus) { - // fail( - // "Could not verify that notes field was copied from source to target attachment for all - // facets"); - // } - // } + Integer customProperty2Value = 12345; + for (String facetName : facet) { + List> sourceAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - // @Test - // @Order(38) - // void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { - // System.out.println( - // "Test (38): Verify that secondary properties are preserved when copying attachments - // between entities across multiple facets"); - // Boolean testStatus = false; - - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyCustomSourceEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity"); - // } + Map sourceAttachmentMetadata = + sourceAttachmentsMetadata.stream() + .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample1.pdf").getFile()); + if (sourceAttachmentMetadata == null) { + fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); + } - // List objectIdsToStore = new ArrayList<>(); + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId for facet: " + facetName); + } - // for (String facetName : facet) { - // 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 sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + objectIdsToStore.add(sourceObjectId); + + 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 for facet " + + facetName + + ". Expected: true, Got: " + + sourceCustomProperty6); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } + int startIndex = sourceObjectIds.size(); + sourceObjectIds.addAll(objectIdsToStore); - // String sourceAttachmentId = createResponse.get(1); - - // 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 for facet: " + - // facetName); - // } + int facetIndex = 0; + for (String facetName : facet) { + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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 for facet: " + facetName); - // } - // } + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - // // Save source entity to persist attachments before fetching metadata - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after creating attachments"); - // } + String copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // Integer customProperty2Value = 12345; - // for (String facetName : facet) { - // List> sourceAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity for facet: " + facetName); + } - // Map sourceAttachmentMetadata = - // sourceAttachmentsMetadata.stream() - // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + // Fetch copied attachment IDs from target draft + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity for facet: " + facetName); + } - // if (sourceAttachmentMetadata == null) { - // fail("Could not find attachment with filename 'sample1.pdf' in facet: " + facetName); - // } + facetIndex++; + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId for facet: " + facetName); - // } + for (String facetName : facet) { + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // objectIdsToStore.add(sourceObjectId); - - // 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 for facet " - // + facetName - // + ". Expected: true, Got: " - // + sourceCustomProperty6); - // } + Map copiedAttachmentMetadata = + targetAttachmentsMetadata.stream() + .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } - // } + if (copiedAttachmentMetadata == null) { + fail( + "Could not find the copied attachment with file in target entity for facet: " + + facetName); + } - // int startIndex = sourceObjectIds.size(); - // sourceObjectIds.addAll(objectIdsToStore); + Boolean copiedCustomProperty6 = + copiedAttachmentMetadata.get("customProperty6") != null + ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + : null; + Integer copiedCustomProperty2 = + copiedAttachmentMetadata.get("customProperty2") != null + ? (Integer) copiedAttachmentMetadata.get("customProperty2") + : null; + + if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + fail( + "DocumentInfoRecordBoolean was not properly copied for facet " + + facetName + + ". Expected: true, Got: " + + copiedCustomProperty6); + } - // int facetIndex = 0; - // for (String facetName : facet) { - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // String copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment from target entity for facet: " + facetName); + } else { + testStatus = true; + } + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity for facet: " + facetName); - // } + if (!testStatus) { + fail( + "Could not verify that all secondary properties were copied from source to target attachment for all facets"); + } + } - // // Fetch copied attachment IDs from target draft - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity for facet: " + facetName); - // } + @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 across multiple facets"); + Boolean testStatus = false; - // facetIndex++; - // } + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity"); + } - // for (String facetName : facet) { - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample2.pdf").getFile()); + + String notesValue = "This attachment has both notes and secondary properties for testing"; + MediaType mediaType = MediaType.parse("application/json"); + Integer customProperty2Value = 99999; + List objectIdsToStore = new ArrayList<>(); + + for (String facetName : facet) { + 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); - // Map copiedAttachmentMetadata = - // targetAttachmentsMetadata.stream() - // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in facet: " + facetName); + } - // if (copiedAttachmentMetadata == null) { - // fail( - // "Could not find the copied attachment with file in target entity for facet: " - // + facetName); - // } + String sourceAttachmentId = createResponse.get(1); - // Boolean copiedCustomProperty6 = - // copiedAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - // : null; - // Integer copiedCustomProperty2 = - // copiedAttachmentMetadata.get("customProperty2") != null - // ? (Integer) copiedAttachmentMetadata.get("customProperty2") - // : null; - - // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean was not properly copied for facet " - // + facetName - // + ". Expected: true, Got: " - // + copiedCustomProperty6); - // } + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + String updateNotesResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateNotesBody); - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update attachment notes field for facet: " + facetName); + } - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment from target entity for facet: " + facetName); - // } else { - // testStatus = 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 for facet: " + facetName); + } - // if (!testStatus) { - // fail( - // "Could not verify that all secondary properties were copied from source to target - // attachment for all facets"); - // } - // } + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); - // @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 across multiple facets"); - // 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 (!updateSecondaryPropertyResponse2.equals("Updated")) { + fail("Could not update attachment customProperty2 field for facet: " + facetName); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // Save source entity to persist attachments before fetching metadata and copying + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after creating attachments"); + } - // String notesValue = "This attachment has both notes and secondary properties for testing"; - // MediaType mediaType = MediaType.parse("application/json"); - // Integer customProperty2Value = 99999; - // List objectIdsToStore = new ArrayList<>(); + for (String facetName : facet) { + List> sourceAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); - // for (String facetName : facet) { - // 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"); + Map sourceAttachmentMetadata = + sourceAttachmentsMetadata.stream() + .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + if (sourceAttachmentMetadata == null) { + fail("Could not find attachment with file in facet: " + facetName); + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in facet: " + facetName); - // } + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId for facet: " + facetName); + } - // String sourceAttachmentId = createResponse.get(1); + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + objectIdsToStore.add(sourceObjectId); + + 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 for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + sourceNoteValue); + } - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + 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 for facet " + + facetName + + ". Expected: true, Got: " + + sourceCustomProperty6); + } - // String updateNotesResponse = - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // copyCustomSourceEntity, - // sourceAttachmentId, - // updateNotesBody); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } + } - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update attachment notes field for facet: " + facetName); - // } + int startIndex = sourceObjectIds.size(); + sourceObjectIds.addAll(objectIdsToStore); - // 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 for facet: " + - // facetName); - // } + int facetIndex = 0; + for (String facetName : facet) { + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // 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 for facet: " + facetName); - // } - // } + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); - // // Save source entity to persist attachments before fetching metadata and copying - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after creating attachments"); - // } + String copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // for (String facetName : facet) { - // List> sourceAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomSourceEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity for facet: " + facetName); + } - // Map sourceAttachmentMetadata = - // sourceAttachmentsMetadata.stream() - // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity for facet: " + facetName); + } - // if (sourceAttachmentMetadata == null) { - // fail("Could not find attachment with file in facet: " + facetName); - // } + facetIndex++; + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId for facet: " + facetName); - // } + for (String facetName : facet) { + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // objectIdsToStore.add(sourceObjectId); - - // 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 for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + sourceNoteValue); - // } + Map copiedAttachmentMetadata = + targetAttachmentsMetadata.stream() + .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // 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 for facet " - // + facetName - // + ". Expected: true, Got: " - // + sourceCustomProperty6); - // } + if (copiedAttachmentMetadata == null) { + fail( + "Could not find the copied attachment with file in target entity for facet: " + + facetName); + } - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } - // } + String copiedNoteValue = + copiedAttachmentMetadata.get("note") != null + ? copiedAttachmentMetadata.get("note").toString() + : null; + + if (!notesValue.equals(copiedNoteValue)) { + fail( + "Notes field was not properly copied for facet " + + facetName + + ". Expected: " + + notesValue + + ", Got: " + + copiedNoteValue); + } - // int startIndex = sourceObjectIds.size(); - // sourceObjectIds.addAll(objectIdsToStore); + Boolean copiedCustomProperty6 = + copiedAttachmentMetadata.get("customProperty6") != null + ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + : null; + Integer copiedCustomProperty2 = + copiedAttachmentMetadata.get("customProperty2") != null + ? (Integer) copiedAttachmentMetadata.get("customProperty2") + : null; + + if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + fail( + "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " + + facetName + + ". Expected: true, Got: " + + copiedCustomProperty6); + } + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied for facet " + + facetName + + ". Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // int facetIndex = 0; - // for (String facetName : facet) { - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment from target entity for facet: " + facetName); + } else { + testStatus = true; + } + } + api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); + api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); + if (!testStatus) { + fail( + "Could not verify that notes field and all secondary properties were copied from source to target attachment for all facets"); + } + } - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(startIndex + facetIndex)); + @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<>(); + for (int i = 0; i < 3; i++) { + attachments.add(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"), "sample3.pdf"); + Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); + File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + copyAttachmentSourceEntity, + srvpath, + postData, + file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + } + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); + attachmentsMetadata.add(fetchAttachmentMetadataResponse); + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } + } - // String copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + sourceObjectIds.clear(); + for (Map metadata : attachmentsMetadata) { + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + + if (sourceObjectIds.size() == 6) { + String copyResponse; + int i = 0; + for (String facetName : facet) { + if (i != 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } + List currentFacetObjectIds = + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + if (currentFacetObjectIds.size() != 2) { + fail("Not enough object IDs to copy attachments for facet: " + facet); + } + copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, currentFacetObjectIds); + i += 2; + if (copyResponse.equals("Attachments copied successfully")) { + // Fetch copied attachment IDs from target draft + List> copiedMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + List copiedAttachmentIds = + copiedMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + 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"); + } + } + } + } 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"); + } + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity for facet: " + facetName); - // } + @Test + @Order(41) + void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { + System.out.println("Test (41): Copy attachments from one entity to another new entity"); + 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")) { + if (sourceObjectIds.size() == 6) { + int i = 0; + for (String facetName : facet) { + List currentFacetObjectIds = + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); + currentFacetObjectIds.add("incorrectObjectId"); + if (currentFacetObjectIds.size() != 3) { + fail("Not enough object IDs to copy attachments for facet: " + facet); + } + try { + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error"); + } catch (IOException e) { + i += 2; + } + } + 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 saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity for facet: " + facetName); - // } + @Test + @Order(42) + void testCreateLinkSuccess() throws IOException { + System.out.println("Test (42): Create link in entity"); + List attachments = new ArrayList<>(); - // facetIndex++; - // } + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // for (String facetName : facet) { - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + for (String facetName : facet) { + 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")) { + fail("Could not create links for facet : " + facetName + createLinkResponse1); + } + } - // Map copiedAttachmentMetadata = - // targetAttachmentsMetadata.stream() - // .filter(attachment -> "sample2.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 for facet: " - // + facetName); - // } + for (String facetName : facet) { + 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); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open created link in facet : " + facetName); + } + } + } + } - // String copiedNoteValue = - // copiedAttachmentMetadata.get("note") != null - // ? copiedAttachmentMetadata.get("note").toString() - // : null; - - // if (!notesValue.equals(copiedNoteValue)) { - // fail( - // "Notes field was not properly copied for facet " - // + facetName - // + ". Expected: " - // + notesValue - // + ", Got: " - // + copiedNoteValue); - // } + @Test + @Order(43) + void testCreateLinkDifferentEntity() throws IOException { + System.out.println("Test (43): Create link with same name in different entity"); - // Boolean copiedCustomProperty6 = - // copiedAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - // : null; - // Integer copiedCustomProperty2 = - // copiedAttachmentMetadata.get("customProperty2") != null - // ? (Integer) copiedAttachmentMetadata.get("customProperty2") - // : null; - - // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean (customProperty6) was not properly copied for facet " - // + facetName - // + ". Expected: true, Got: " - // + copiedCustomProperty6); - // } - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied for facet " - // + facetName - // + ". Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + String createLinkDifferentEntity = + api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkDifferentEntity.equals("Could not edit entity")) { + fail("Could not create entity"); + } - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment from target entity for facet: " + facetName); - // } else { - // testStatus = true; - // } - // } - // api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); - // api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); - // if (!testStatus) { - // fail( - // "Could not verify that notes field and all secondary properties were copied from source - // to target attachment for all facets"); - // } - // } + String linkName = "sample"; + String linkUrl = "https://example.com"; + for (String facetName : facet) { + 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"); + } + } - // @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<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(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"), "sample3.pdf"); - // Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); - // File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample4.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 (int i = 0; i < facet.length; i++) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // copyAttachmentSourceEntity, - // srvpath, - // postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // } - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facet[i], copyAttachmentSourceEntity, attachment); - // attachmentsMetadata.add(fetchAttachmentMetadataResponse); - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } - // } + String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkDifferentEntity); + if (!response.equals("Saved")) { + fail("Could not save 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"); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - - // if (sourceObjectIds.size() == 6) { - // String copyResponse; - // int i = 0; - // for (String facetName : facet) { - // if (i != 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } - // List currentFacetObjectIds = - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - // if (currentFacetObjectIds.size() != 2) { - // fail("Not enough object IDs to copy attachments for facet: " + facet); - // } - // copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, - // currentFacetObjectIds); - // i += 2; - // if (copyResponse.equals("Attachments copied successfully")) { - // // Fetch copied attachment IDs from target draft - // List> copiedMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // List copiedAttachmentIds = - // copiedMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // 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"); - // } - // } - // } - // } 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"); - // } - // } + response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // @Test - // @Order(41) - // void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { - // System.out.println("Test (41): Copy attachments from one entity to another new entity"); - // 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")) { - // if (sourceObjectIds.size() == 6) { - // int i = 0; - // for (String facetName : facet) { - // List currentFacetObjectIds = - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size())); - // currentFacetObjectIds.add("incorrectObjectId"); - // if (currentFacetObjectIds.size() != 3) { - // fail("Not enough object IDs to copy attachments for facet: " + facet); - // } - // try { - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error"); - // } catch (IOException e) { - // i += 2; - // } - // } - // 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"); - // } - // } + @Test + @Order(44) + void testCreateLinkFailure() throws IOException { + System.out.println("Test (41): Create link fails due to invalid URL and name"); + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (editEntityResponse.equals("Could not edit entity")) { + fail("Could not edit entity"); + } + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "example.com"; + try { + String response = + 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 < 6; i++) { + api.createLink( + appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + linkUrl); + } + System.out.println("Created 5 links in facet: " + facetName); + if (!facetName.equals("footnotes")) { + 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); + if (facetName.equals("references")) { + assertEquals("Cannot upload more than 5 attachments.", errorMessage); + } else if (facetName.equals("attachments")) { + assertEquals("Cannot upload more than 4 attachments.", errorMessage); + } + } + } - // @Test - // @Order(42) - // void testCreateLinkSuccess() throws IOException { - // System.out.println("Test (42): Create link in entity"); - // List attachments = new ArrayList<>(); + String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!response.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"); - // } + response = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // for (String facetName : facet) { - // 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")) { - // fail("Could not create links for facet : " + facetName + createLinkResponse1); - // } - // } + @Test + @Order(45) + void testCreateLinkNoSDMRoles() throws IOException { + System.out.println("Test (42): Create link fails due to no SDM roles assigned"); - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + String createLinkEntityNoSDMRoles = + apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { + fail("Could not create entity"); + } - // for (String facetName : facet) { - // 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); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open created link in facet : " + facetName); - // } - // } - // } - // } + for (String facetName : facet) { + 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); + } + } - // @Test - // @Order(43) - // void testCreateLinkDifferentEntity() throws IOException { - // System.out.println("Test (43): Create link with same name in different entity"); + String response = + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); + if (!response.equals("Saved")) { + fail("Could not save entity"); + } - // String createLinkDifferentEntity = - // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkDifferentEntity.equals("Could not edit entity")) { - // fail("Could not create entity"); - // } + response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // String linkName = "sample"; - // String linkUrl = "https://example.com"; - // for (String facetName : facet) { - // 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"); - // } - // } + @Test + @Order(46) + void testDeleteLink() throws IOException { + System.out.println("Test (43): Delete link in entity"); + List> attachments = new ArrayList<>(); - // String response = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkDifferentEntity); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } + String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + for (String facetName : facet) { + 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 for facet : " + facetName); + } + } - // @Test - // @Order(44) - // void testCreateLinkFailure() throws IOException { - // System.out.println("Test (41): Create link fails due to invalid URL and name"); - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (editEntityResponse.equals("Could not edit entity")) { - // fail("Could not edit entity"); - // } - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "example.com"; - // try { - // String response = - // 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 < 6; i++) { - // api.createLink( - // appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + - // linkUrl); - // } - // System.out.println("Created 5 links in facet: " + facetName); - // if (!facetName.equals("footnotes")) { - // 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); - // if (facetName.equals("references")) { - // assertEquals("Cannot upload more than 5 attachments.", errorMessage); - // } else if (facetName.equals("attachments")) { - // assertEquals("Cannot upload more than 4 attachments.", errorMessage); - // } - // } - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } - // response = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // @Test - // @Order(45) - // void testCreateLinkNoSDMRoles() throws IOException { - // System.out.println("Test (42): Create link fails due to no SDM roles assigned"); + int index = 0; + for (String facetName : facet) { + String deleteLinkResponse = + api.deleteAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); + System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); + if (!deleteLinkResponse.equals("Deleted")) { + fail("Could not delete created link"); + } + index += 1; + } - // String createLinkEntityNoSDMRoles = - // apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntityNoSDMRoles.equals("Could not edit entity")) { - // fail("Could not create entity"); - // } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // for (String facetName : facet) { - // 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); - // } - // } + index = 0; + attachments.clear(); + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + System.out.println( + "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); + if (attachments.get(index).size() != 0) { + fail("Link wasn't deleted"); + } + index += 1; + } - // String response = - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } + String response = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } - // response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (44): Rename link in entity"); + List> attachments = new ArrayList<>(); - // @Test - // @Order(46) - // void testDeleteLink() throws IOException { - // System.out.println("Test (43): Delete link in entity"); - // List> attachments = new ArrayList<>(); + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + for (String facetName : facet) { + 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"); + } + } - // for (String facetName : facet) { - // 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 for facet : " + facetName); - // } - // } + 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"); - // } + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } - // for (String facetName : facet) { - // attachments.add( - // 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 editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + int index = 0; + for (String facetName : facet) { + successfullyRenamedAttachments.add(attachments.get(index).get(0)); + String renameLinkResponse = + api.renameAttachment( + appUrl, + entityName, + facetName, + createLinkEntity, + attachments.get(index).get(0), + "sampleRenamed"); + if (!renameLinkResponse.equals("Renamed")) { + fail("Could not Renamed created link"); + } + index += 1; + } - // int index = 0; - // for (String facetName : facet) { - // String deleteLinkResponse = - // api.deleteAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(index).get(0)); - // System.out.println("Delete response for facet " + facetName + ": " + deleteLinkResponse); - // if (!deleteLinkResponse.equals("Deleted")) { - // fail("Could not delete created link"); - // } - // index += 1; - // } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + @Test + @Order(48) + void testRenameLinkDuplicate() throws IOException { + System.out.println("Test (45): 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"); + } - // index = 0; - // attachments.clear(); - // for (String facetName : facet) { - // attachments.add( - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // System.out.println( - // "Attachments after deletion in facet " + facetName + ": " + attachments.get(index)); - // if (attachments.get(index).size() != 0) { - // fail("Link wasn't deleted"); - // } - // index += 1; - // } + int index = 0; + for (String facetName : facet) { + 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 response = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (saveResponse.equals("Could not save entity")) { + fail("Could not save entity"); + } - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (44): Rename link in entity"); - // List> attachments = new ArrayList<>(); + index = 0; + List facetAttachments; + for (String facetName : facet) { + int lambdaIndex = index; + facetAttachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .filter( + item -> + !successfullyRenamedAttachments + .get(lambdaIndex) + .equals(item.get("ID"))) // skip unwanted filename + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + index += 1; + attachments.add(facetAttachments.get(0)); + } - // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + System.out.println("Attachments to be renamed: " + attachments); + String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!response.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // for (String facetName : facet) { - // 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"); - // } - // } + index = 0; + for (String facetName : facet) { + api.renameAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(index), "sampleRenamed"); + index += 1; + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + String saveError = 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: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + ObjectMapper mapper = new ObjectMapper(); + assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); - // for (String facetName : facet) { - // attachments.add( - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // } + String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); + if (!deleteEntityResponse.equals("Entity Draft Deleted")) { + fail("Entity draft not deleted"); + } + } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + @Test + @Order(49) + void testRenameLinkUnsupportedCharacters() throws IOException { + System.out.println( + "Test (46): Rename link in entity fails due to unsupported characters in name"); + List> attachments = new ArrayList<>(); - // int index = 0; - // for (String facetName : facet) { - // successfullyRenamedAttachments.add(attachments.get(index).get(0)); - // String renameLinkResponse = - // api.renameAttachment( - // appUrl, - // entityName, - // facetName, - // createLinkEntity, - // attachments.get(index).get(0), - // "sampleRenamed"); - // if (!renameLinkResponse.equals("Renamed")) { - // fail("Could not Renamed created link"); - // } - // index += 1; - // } + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } - // } + String linkName = "sample2"; + String linkUrl = "https://www.example.com"; - // @Test - // @Order(48) - // void testRenameLinkDuplicate() throws IOException { - // System.out.println("Test (45): 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"); - // } + for (String facetName : facet) { + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } + } - // int index = 0; - // for (String facetName : facet) { - // 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 saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (saveResponse.equals("Could not save entity")) { - // fail("Could not save entity"); - // } + for (String facetName : facet) { + attachments.add( + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList())); + } - // index = 0; - // List facetAttachments; - // for (String facetName : facet) { - // int lambdaIndex = index; - // facetAttachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .filter( - // item -> - // !successfullyRenamedAttachments - // .get(lambdaIndex) - // .equals(item.get("ID"))) // skip unwanted filename - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // index += 1; - // attachments.add(facetAttachments.get(0)); - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // System.out.println("Attachments to be renamed: " + attachments); - // String response = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!response.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + int index = 0; + for (String facetName : facet) { + api.renameAttachment( + appUrl, + entityName, + facetName, + createLinkEntity, + attachments.get(index).get(0), + "sampleRenamed//"); + index += 1; + } - // index = 0; - // for (String facetName : facet) { - // api.renameAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(index), - // "sampleRenamed"); - // index += 1; - // } + String error = + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + String expectedError = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + ObjectMapper mapper = new ObjectMapper(); + assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); - // String saveError = 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: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"An object named - // \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An - // object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: - // footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // 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"); - // } - // } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!deleteEntityResponse.equals("Entity Deleted")) { + fail("Entity draft not deleted"); + } + } - // @Test - // @Order(49) - // void testRenameLinkUnsupportedCharacters() throws IOException { - // System.out.println( - // "Test (46): Rename link in entity fails due to unsupported characters in name"); - // List> attachments = new ArrayList<>(); + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (47): Edit existing link in entity"); + List> attachmentsPerFacet = new ArrayList<>(); - // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (editLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // String linkName = "sample2"; - // String linkUrl = "https://www.example.com"; + for (String facetName : facet) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facetName); + } + } - // for (String facetName : facet) { - // 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 saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // 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"); + } - // for (String facetName : facet) { - // attachments.add( - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList())); - // } + for (String facetName : facet) { + List attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).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"); - // } + if (attachments.isEmpty()) { + fail("Could not find link in facet: " + facetName); + } + attachmentsPerFacet.add(attachments); + } - // int index = 0; - // for (String facetName : facet) { - // api.renameAttachment( - // appUrl, - // entityName, - // facetName, - // createLinkEntity, - // attachments.get(index).get(0), - // "sampleRenamed//"); - // index += 1; - // } + int index = 0; + for (String facetName : facet) { + String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); + } + index++; + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + + int verificationIndex = 0; + for (String facetName : facet) { + List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); + for (String attachmentId : attachmentsInFacet) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open edited link " + attachmentId + " in facet: " + facetName); + } + } + verificationIndex++; + } + } - // String error = - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // String expectedError = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: references\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" - // contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: - // attachments\\nPage: - // IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: footnotes\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // ObjectMapper mapper = new ObjectMapper(); - // assertEquals(mapper.readTree(expectedError), mapper.readTree(error)); - - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!deleteEntityResponse.equals("Entity Deleted")) { - // fail("Entity draft not deleted"); - // } - // } + @Test + @Order(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (48): Edit existing link with invalid url"); + List> attachmentsPerFacet = new ArrayList<>(); + + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (47): Edit existing link in entity"); - // List> attachmentsPerFacet = new ArrayList<>(); + for (String facetName : facet) { + List attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (editLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + if (attachments.isEmpty()) { + fail("Could not edit link in facet: " + facetName); + } + attachmentsPerFacet.add(attachments); + } - // for (String facetName : facet) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facetName); - // } - // } + int index = 0; + for (String facetName : facet) { + try { + String linkId = attachmentsPerFacet.get(index).get(0); + String updatedUrl = "https://editedexample"; + index++; + String editLinkResponse = + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + System.out.println("response " + editLinkResponse); + fail("Edit link did not throw an error for invalid url 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("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); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + @Test + @Order(52) + void testEditLinkFailureEmptyURL() throws IOException { + System.out.println("Test (49): Edit existing link with an empty url"); + List> attachmentsPerFacet = new ArrayList<>(); + + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + for (String facetName : facet) { + List attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // for (String facetName : facet) { - // List 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 in facet: " + facetName); + } + attachmentsPerFacet.add(attachments); + } - // if (attachments.isEmpty()) { - // fail("Could not find link in facet: " + facetName); - // } - // attachmentsPerFacet.add(attachments); - // } + int index = 0; + for (String facetName : facet) { + try { + String linkId = attachmentsPerFacet.get(index).get(0); + String updatedUrl = ""; + index++; + + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + fail("Edit link did not throw an error for empty url 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"); + String expected = "Provide the missing value."; + assertEquals("409008", errorCode); + assertEquals(expected, errorMessage); + } + } + api.deleteEntity(appUrl, entityName, editLinkEntity); + } - // int index = 0; - // for (String facetName : facet) { - // String linkId = attachmentsPerFacet.get(index).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 in facet: " + facetName); - // } - // index++; - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - - // int verificationIndex = 0; - // for (String facetName : facet) { - // List attachmentsInFacet = attachmentsPerFacet.get(verificationIndex); - // for (String attachmentId : attachmentsInFacet) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open edited link " + attachmentId + " in facet: " + facetName); - // } - // } - // verificationIndex++; - // } - // } + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); - // @Test - // @Order(51) - // void testEditLinkFailureInvalidURL() throws IOException { - // System.out.println("Test (48): Edit existing link with invalid url"); - // List> attachmentsPerFacet = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + Boolean testStatus = false; - // for (String facetName : facet) { - // List attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (editLinkEntity.equals("Could not create entity")) { + fail("Could not edit entity"); + } - // if (attachments.isEmpty()) { - // fail("Could not edit link in facet: " + facetName); - // } - // attachmentsPerFacet.add(attachments); - // } + for (String facetName : facet) { + String linkName = "sampleNoRole_" + facetName; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in facet: " + facetName); + } + } - // int index = 0; - // for (String facetName : facet) { - // try { - // String linkId = attachmentsPerFacet.get(index).get(0); - // String updatedUrl = "https://editedexample"; - // index++; - // String editLinkResponse = - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // System.out.println("response " + editLinkResponse); - // fail("Edit link did not throw an error for invalid url 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("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); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // @Test - // @Order(52) - // void testEditLinkFailureEmptyURL() throws IOException { - // System.out.println("Test (49): Edit existing link with an empty url"); - // List> attachmentsPerFacet = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + String editEntityResponse = + apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // for (String facetName : facet) { - // List attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + for (String facetName : facet) { + List 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 in facet: " + facetName); - // } - // attachmentsPerFacet.add(attachments); - // } + if (attachments.isEmpty()) { + fail("Could not find link in facet: " + facetName); + } - // int index = 0; - // for (String facetName : facet) { - // try { - // String linkId = attachmentsPerFacet.get(index).get(0); - // String updatedUrl = ""; - // index++; - - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // fail("Edit link did not throw an error for empty url 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"); - // String expected = "Provide the missing value."; - // assertEquals("409008", errorCode); - // assertEquals(expected, errorMessage); - // } - // } - // api.deleteEntity(appUrl, entityName, editLinkEntity); - // } + String linkId = attachments.get(0); + String updatedUrl = "https://www.editedexample.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; + } + } + api.deleteEntity(appUrl, entityName, editLinkEntity); + if (!testStatus) { + fail("Link got edited without SDM roles"); + } + } - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (50): Edit link fails due to no SDM roles assigned"); + @Test + @Order(54) + void testCopyLinkSuccessNewEntity() throws IOException { + System.out.println("Test (51): Copy link from one entity to another new entity"); + List> attachmentsByFacet = new ArrayList<>(); + String linkUrl = "https://www.example.com"; + for (int i = 0; i < facet.length; i++) { + attachmentsByFacet.add(new ArrayList<>()); + } - // Boolean testStatus = false; + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (editLinkEntity.equals("Could not create entity")) { - // fail("Could not edit entity"); - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entities"); + } - // for (String facetName : facet) { - // String linkName = "sampleNoRole_" + facetName; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in facet: " + facetName); - // } - // } + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // String editEntityResponse = - // apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + sourceObjectIds.clear(); + for (int i = 0; i < facet.length; i++) { + List objectIds = + api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + sourceObjectIds.addAll(objectIds); + } - // for (String facetName : facet) { - // List attachments = - // apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + if (sourceObjectIds.size() != facet.length) { + fail( + "Could not fetch object Ids for all attachments. Expected: " + + facet.length + + ", Found: " + + sourceObjectIds.size()); + } - // if (attachments.isEmpty()) { - // fail("Could not find link in facet: " + facetName); - // } + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft for facet: " + facetName); + } - // String linkId = attachments.get(0); - // String updatedUrl = "https://www.editedexample.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; - // } - // } - // api.deleteEntity(appUrl, entityName, editLinkEntity); - // if (!testStatus) { - // fail("Link got edited without SDM roles"); - // } - // } + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // @Test - // @Order(54) - // void testCopyLinkSuccessNewEntity() throws IOException { - // System.out.println("Test (51): Copy link from one entity to another new entity"); - // List> attachmentsByFacet = new ArrayList<>(); - // String linkUrl = "https://www.example.com"; - // for (int i = 0; i < facet.length; i++) { - // attachmentsByFacet.add(new ArrayList<>()); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity after copying attachments for facet " + facetName); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entities"); - // } + List> attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch in facet " + facetName); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + System.out.println("Attachment type and URL validated for facet " + facetName); + + List attachments = + attachmentsMetadata.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + for (String attachment : attachments) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open copied link in facet: " + facetName); + } + } - // sourceObjectIds.clear(); - // for (int i = 0; i < facet.length; i++) { - // List objectIds = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // sourceObjectIds.addAll(objectIds); - // } + objectIdIndex++; + } - // if (sourceObjectIds.size() != facet.length) { - // fail( - // "Could not fetch object Ids for all attachments. Expected: " - // + facet.length - // + ", Found: " - // + sourceObjectIds.size()); - // } + String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + if (!deleteTargetResponse.equals("Entity Deleted")) { + fail("Could not delete target entity"); + } + } - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft for facet: " + facetName); - // } + @Test + @Order(55) + void testCopyLinkUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (52): Copy invalid type of link from one entity to another new entity"); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + if (!editResponse.equals("Entity in draft mode") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not edit source entity or create target entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - // } + sourceObjectIds.add("incorrectObjectId"); - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity after copying attachments for facet " + facetName); - // } + for (String facetName : facet) { + try { + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error for facet: " + facetName); + } catch (IOException e) { + System.out.println("Successfully caught expected error for facet: " + facetName); + } + } + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + } - // List> attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + @Test + @Order(56) + void testCopyLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println("Test (53): Copy link from a new entity to an existing target entity"); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + List> attachmentsByFacet = new ArrayList<>(); + String linkUrl = "https://www.example.com"; + for (int i = 0; i < facet.length; i++) { + attachmentsByFacet.add(new ArrayList<>()); + } - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch in facet " + facetName); + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - // System.out.println("Attachment type and URL validated for facet " + facetName); + for (int i = 0; i < facet.length; i++) { + String linkName = "newsample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } - // List attachments = - // attachmentsMetadata.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // for (String attachment : attachments) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open copied link in facet: " + facetName); - // } - // } + sourceObjectIds.clear(); + for (String facetName : facet) { + List objectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + sourceObjectIds.addAll(objectIds); + } - // objectIdIndex++; - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Ids for any attachments"); + } - // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // if (!deleteTargetResponse.equals("Entity Deleted")) { - // fail("Could not delete target entity"); - // } - // } + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft for facet: " + facetName); + } - // @Test - // @Order(55) - // void testCopyLinkUnsuccessfulNewEntity() throws IOException { - // System.out.println( - // "Test (52): Copy invalid type of link from one entity to another new entity"); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // if (!editResponse.equals("Entity in draft mode") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not edit source entity or create target entity"); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + } - // sourceObjectIds.add("incorrectObjectId"); + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity after copying attachments for facet " + facetName); + } - // for (String facetName : facet) { - // try { - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error for facet: " + facetName); - // } catch (IOException e) { - // System.out.println("Successfully caught expected error for facet: " + facetName); - // } - // } - // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // } + List> attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // @Test - // @Order(56) - // void testCopyLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println("Test (53): Copy link from a new entity to an existing target entity"); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // List> attachmentsByFacet = new ArrayList<>(); - // String linkUrl = "https://www.example.com"; - // for (int i = 0; i < facet.length; i++) { - // attachmentsByFacet.add(new ArrayList<>()); - // } + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch in facet " + facetName); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + System.out.println("Attachment type and URL validated for facet " + facetName); + + List attachments = + attachmentsMetadata.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + for (String attachment : attachments) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open copied link in facet: " + facetName); + } + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + objectIdIndex++; + } - // for (int i = 0; i < facet.length; i++) { - // String linkName = "newsample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + @Test + @Order(57) + void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println( + "Test (54): Copy invalid type of link from new entity to existing target entity"); + String linkUrl = "https://www.example.com"; - // sourceObjectIds.clear(); - // for (String facetName : facet) { - // List objectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // sourceObjectIds.addAll(objectIds); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Ids for any attachments"); - // } + for (int i = 0; i < facet.length; i++) { + String linkName = "newsample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit entities"); + } + for (String facetName : facet) { + List sourceObjectIds = new ArrayList<>(); + sourceObjectIds.add("incorrectObjectId"); + try { + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error for facet: " + facetName); + } catch (IOException e) { + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + } - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft for facet: " + facetName); - // } + @Test + @Order(58) + void testCopyLinkSuccessNewEntityDraft() throws IOException { + System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); + List> attachmentsByFacet = new ArrayList<>(); + String linkUrl = "https://www.example.com"; + for (int i = 0; i < facet.length; i++) { + attachmentsByFacet.add(new ArrayList<>()); + } - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entities"); + } - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity after copying attachments for facet " + facetName); - // } + for (int i = 0; i < facet.length; i++) { + String linkName = "sample" + i; + String createLinkResponse = + api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link for facet: " + facet[i]); + } + } - // List> attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + sourceObjectIds.clear(); + for (int i = 0; i < facet.length; i++) { + List objectIds = + api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + sourceObjectIds.addAll(objectIds); + } - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch in facet " + facetName); + if (sourceObjectIds.size() != facet.length) { + fail( + "Could not fetch object Ids for all attachments. Expected: " + + facet.length + + ", Found: " + + sourceObjectIds.size()); + } - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - // System.out.println("Attachment type and URL validated for facet " + facetName); + int objectIdIndex = 0; + for (String facetName : facet) { + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft for facet: " + facetName); + } - // List attachments = - // attachmentsMetadata.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); - // for (String attachment : attachments) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open copied link in facet: " + facetName); - // } - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); + } - // objectIdIndex++; - // } + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity after copying attachments for facet " + facetName); + } - // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // } + List> attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // @Test - // @Order(57) - // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println( - // "Test (54): Copy invalid type of link from new entity to existing target entity"); - // String linkUrl = "https://www.example.com"; + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch in facet " + facetName); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); + System.out.println("Attachment type and URL validated for facet " + facetName); + + List attachments = + attachmentsMetadata.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + for (String attachment : attachments) { + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open copied link in facet: " + facetName); + } + } - // for (int i = 0; i < facet.length; i++) { - // String linkName = "newsample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit entities"); - // } - // for (String facetName : facet) { - // List sourceObjectIds = new ArrayList<>(); - // sourceObjectIds.add("incorrectObjectId"); - // try { - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error for facet: " + facetName); - // } catch (IOException e) { - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // } + objectIdIndex++; + } - // @Test - // @Order(58) - // void testCopyLinkSuccessNewEntityDraft() throws IOException { - // System.out.println("Test (55): Copy link from one entity to another new entity draft mode"); - // List> attachmentsByFacet = new ArrayList<>(); - // String linkUrl = "https://www.example.com"; - // for (int i = 0; i < facet.length; i++) { - // attachmentsByFacet.add(new ArrayList<>()); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @Test + @Order(59) + void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { + System.out.println( + "Test (56): Copy attachments from one entity to another new entity draft mode"); + List> attachments = new ArrayList<>(); + for (int i = 0; i < 3; i++) { + attachments.add(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(); - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entities"); - // } + for (int i = 0; i < facet.length; i++) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + copyAttachmentSourceEntity, + srvpath, + postData, + file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.get(i).add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + } + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (int i = 0; i < attachments.size(); i++) { + for (String attachment : attachments.get(i)) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facet[i], 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"); + } + } - // for (int i = 0; i < facet.length; i++) { - // String linkName = "sample" + i; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facet[i], copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link for facet: " + facet[i]); - // } - // } + if (sourceObjectIds.size() == 6) { + String copyResponse; + int i = 0; + for (String facetName : facet) { + if (i != 0) { + String editResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } + } + copyResponse = + api.copyAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); + i += 2; + if (copyResponse.equals("Attachments copied successfully")) { + // Fetch copied attachment IDs from target draft + List> copiedMetadataResponse = + api.fetchEntityMetadataDraft( + appUrl, entityName, facetName, copyAttachmentTargetEntity); + List copiedAttachmentIds = + copiedMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (saveEntityResponse.equals("Saved")) { + List> fetchEntityMetadataResponse; + fetchEntityMetadataResponse = + api.fetchEntityMetadataDraft( + 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); + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + @Test + @Order(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println( + "Test (60): View changelog for newly created attachment in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // Create a new entity for changelog test + changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); + assertNotEquals("Could not create entity", changelogEntityID[i]); + + // 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"); + + // Create attachment + Map postData = new HashMap<>(); + postData.put("up__ID", changelogEntityID[i]); + postData.put("mimeType", "text/plain"); + 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[i], srvpath, postData, file); - // sourceObjectIds.clear(); - // for (int i = 0; i < facet.length; i++) { - // List objectIds = - // api.fetchEntityMetadataDraft(appUrl, entityName, facet[i], - // copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // sourceObjectIds.addAll(objectIds); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + changelogAttachmentID[i] = createResponse.get(1); - // if (sourceObjectIds.size() != facet.length) { - // fail( - // "Could not fetch object Ids for all attachments. Expected: " - // + facet.length - // + ", Found: " - // + sourceObjectIds.size()); - // } + assertEquals("Attachment created", status, "Attachment should be created successfully"); + assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); + assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); - // int objectIdIndex = 0; - // for (String facetName : facet) { - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft for facet: " + facetName); - // } + // Fetch changelog for the newly created attachment + Map changelogResponse = + api.fetchChangelog( + appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - // List subListToCopy = sourceObjectIds.subList(objectIdIndex, objectIdIndex + 1); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, subListToCopy); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachments for facet " + facetName + ": " + copyResponse); - // } + // 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"); + } + } - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity after copying attachments for facet " + facetName); - // } + @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 in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // 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[i], + changelogAttachmentID[i], + 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[i], + changelogAttachmentID[i], + bodyInt); + assertEquals( + "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - // List> attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // Edit entity again to fetch changelog + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch in facet " + facetName); + // Fetch changelog after modifications + Map changelogResponse = + api.fetchChangelog( + appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch in facet " + facetName); - // System.out.println("Attachment type and URL validated for facet " + facetName); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // List attachments = - // attachmentsMetadata.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + // 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)"); - // for (String attachment : attachments) { - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open copied link in facet: " + facetName); - // } - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); - // objectIdIndex++; - // } + // 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)"); + + // Save the entity so test 62 can edit it + String saveResponseFinal = + api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); + } + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.deleteEntity(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 in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // Edit entity to put it in draft mode (entity was saved at end of test 61) + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + 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[i], + changelogAttachmentID[i], + newFileName); + assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); + + // Save entity after rename + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + + // Edit entity again and fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // Fetch changelog after rename + Map changelogAfterRename = + api.fetchChangelog( + appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); + + 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"); - // @Test - // @Order(59) - // void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { - // System.out.println( - // "Test (56): Copy attachments from one entity to another new entity draft mode"); - // List> attachments = new ArrayList<>(); - // for (int i = 0; i < 3; i++) { - // attachments.add(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"); + @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[i]); + } + } - // sourceObjectIds.clear(); - - // for (int i = 0; i < facet.length; i++) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // copyAttachmentSourceEntity, - // srvpath, - // postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.get(i).add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // } - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (int i = 0; i < attachments.size(); i++) { - // for (String attachment : attachments.get(i)) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facet[i], 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(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 in all three facets"); + + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; + + // 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.pdf").getFile()); + assertTrue(file.exists(), "Sample file should exist"); + + // 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, 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"); - // if (sourceObjectIds.size() == 6) { - // String copyResponse; - // int i = 0; - // for (String facetName : facet) { - // if (i != 0) { - // String editResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } - // } - // copyResponse = - // api.copyAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // sourceObjectIds.subList(i, Math.min(i + 2, sourceObjectIds.size()))); - // i += 2; - // if (copyResponse.equals("Attachments copied successfully")) { - // // Fetch copied attachment IDs from target draft - // List> copiedMetadataResponse = - // api.fetchEntityMetadataDraft( - // appUrl, entityName, facetName, copyAttachmentTargetEntity); - // List copiedAttachmentIds = - // copiedMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (saveEntityResponse.equals("Saved")) { - // List> fetchEntityMetadataResponse; - // fetchEntityMetadataResponse = - // api.fetchEntityMetadataDraft( - // 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); - // } + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println( - // "Test (60): View changelog for newly created attachment in all three facets"); + // 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"); - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; + // Fetch changelog after initial save + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // // Create a new entity for changelog test - // changelogEntityID[i] = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(changelogEntityID[i], "Failed to create changelog test entity"); - // assertNotEquals("Could not create entity", changelogEntityID[i]); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // // 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"); + // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + + // customProperty2) + assertEquals( + 3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); - // // Create attachment - // Map postData = new HashMap<>(); - // postData.put("up__ID", changelogEntityID[i]); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, changelogEntityID[i], srvpath, postData, file); - - // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - // String status = createResponse.get(0); - // changelogAttachmentID[i] = createResponse.get(1); - - // assertEquals("Attachment created", status, "Attachment should be created successfully"); - // assertNotNull(changelogAttachmentID[i], "Attachment ID should not be null"); - // assertNotEquals("", changelogAttachmentID[i], "Attachment ID should not be empty"); - - // // Fetch changelog for the newly created attachment - // Map changelogResponse = - // api.fetchChangelog( - // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - // 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"); - // } - // } + // Save entity again without any modifications + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // @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 in all three facets"); - - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; - - // // 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[i], - // changelogAttachmentID[i], - // 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[i], - // changelogAttachmentID[i], - // bodyInt); - // assertEquals( - // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - - // // Save the entity - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - - // // Edit entity again to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after modifications - // Map changelogResponse = - // api.fetchChangelog( - // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - // 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)"); - - // // Save the entity so test 62 can edit it - // String saveResponseFinal = - // api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - // assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); - // } - // } + // 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"); - // @Test - // @Order(62) - // void testChangelogAfterRenamingAttachment() throws IOException { - // System.out.println( - // "Test (62): Rename attachment and verify changelog increases with rename entry in all - // three facets"); - - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; - - // // Edit entity to put it in draft mode (entity was saved at end of test 61) - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // 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[i], - // changelogAttachmentID[i], - // newFileName); - // assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); - - // // Save entity after rename - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID[i]); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - - // // Edit entity again and fetch changelog - // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID[i]); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after rename - // Map changelogAfterRename = - // api.fetchChangelog( - // appUrl, entityName, facetName, changelogEntityID[i], changelogAttachmentID[i]); - - // 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[i]); - // } - // } + // Fetch changelog after second save + Map changelogAfterSecondSave = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // @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 in all three facets"); + assertNotNull( + changelogAfterSecondSave, "Changelog response should not be null after second save"); - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; + // 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"); - // // 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); + @SuppressWarnings("unchecked") + List> changeLogsAfterSecondSave = + (List>) changelogAfterSecondSave.get("changeLogs"); + assertEquals( + 3, + changeLogsAfterSecondSave.size(), + "Should still have exactly 3 changelog entries after second save"); - // // 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"); + // Clean up the entity + api.deleteEntity(appUrl, entityName, newEntityID); + } + } - // // 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"); + @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 in all three facets"); - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); + for (int i = 0; i < 3; i++) { + String facetName = facet[i]; - // 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); - // } - // } + // 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(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 in all three facets"); + // 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"); - // for (int i = 0; i < 3; i++) { - // String facetName = facet[i]; + // 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 a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(newEntityID, "Failed to create new entity"); - // assertNotEquals("Could not create entity", newEntityID); + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // // 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"); + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + String newAttachmentID = createResponse.get(1); - // // 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"); + 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"); - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); + // Save the entity immediately without any modifications + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // 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); - // } - // } + // 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"); - // @Test - // @Order(65) - // void testMoveAttachmentsWithSourceFacet() throws IOException { - // System.out.println( - // "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // Save entity again without modifying the attachment + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source 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"); - // 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())); + // Fetch changelog for the attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); - // 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"); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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 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); + } + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println( + "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } + for (int i = 0; i < facet.length; i++) { + 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 all objectIds from source entity"); - // } + 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity"); + } - // // Save target before move - // String saveTargetBeforeMoveTest65 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveTest65.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all attachments after move"); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after - // move"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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"); + // Save target before move + String saveTargetBeforeMoveTest65 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveTest65.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveTest65); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // 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> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + sourceAttachmentIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all 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"); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, sourceMetadataAfterMove.size(), "Source entity should have no attachments after move"); - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + @Test + @Order(66) + public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + System.out.println( + "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } + for (int i = 0; i < facet.length; i++) { + 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 all objectIds from source entity"); - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save 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"); - - // File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - // List targetCreateResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // srvpath, - // targetPostData, - // duplicateFile); - - // if (!targetCreateResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment on target entity"); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - - // int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target should have duplicate skipped, other attachments moved"); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // int expectedSourceCount = - // sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); - // assertEquals( - // expectedSourceCount, - // sourceMetadataAfterMove.size(), - // "Source should have duplicate attachment remaining"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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"); - // @Test - // @Order(67) - // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - // System.out.println( - // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + List targetCreateResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + srvpath, + targetPostData, + duplicateFile); + + if (!targetCreateResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment on target entity"); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // 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> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + 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"); + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target should have duplicate skipped, other attachments moved"); + + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + int expectedSourceCount = + sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); + assertEquals( + expectedSourceCount, + sourceMetadataAfterMove.size(), + "Source should have duplicate attachment remaining"); - // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update notes for attachment: " + attachmentId); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // Integer customProperty2Value = 54321; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], 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"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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); - // } - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); - // } + 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update notes for attachment: " + attachmentId); + } + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + Integer customProperty2Value = 54321; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + + for (String attachmentId : sourceAttachmentIds) { + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + if (!updateCustomPropertyResponse.equals("Updated")) { + fail("Could not update custom property for attachment: " + attachmentId); + } + } - // // Save target before move - // String saveTargetBeforeMoveTest67 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveTest67.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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); + } + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all attachments after move"); - - // 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, facet[i], moveTargetEntity, - // targetAttachmentId); - - // 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); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // 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); - // } - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); + // Save target before move + String saveTargetBeforeMoveTest67 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveTest67.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveTest67); + } - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // @Test - // @Order(68) - // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (68): Move valid attachments from Source Entity to Target Entity without - // sourceFacet"); + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + sourceAttachmentIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all attachments after move"); + + 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, facet[i], moveTargetEntity, targetAttachmentId); + + 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); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + 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); + } + } - // 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> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, sourceMetadataAfterMove.size(), "Source entity has no 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"); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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(68) + public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + System.out.println( + "Test (68): Move valid attachments from Source Entity to Target Entity without sourceFacet"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // 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()); - // } - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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 object IDs for all attachments"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + 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 target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // moveObjectIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all moved attachments"); - - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // String readResponse = - // api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read moved attachment from 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"); + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // moveObjectIds.size(), - // sourceMetadataAfterMove.size(), - // "Source entity should still have attachments in UI when sourceFacet is not specified"); - - // for (Map metadata : sourceMetadataAfterMove) { - // String objectId = (String) metadata.get("objectId"); - // assertTrue( - // moveObjectIds.contains(objectId), - // "Source entity should still show attachment with objectId: " + objectId); - // } + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + moveObjectIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all moved attachments"); + + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + String readResponse = + api.readAttachment(appUrl, entityName, facet[i], moveTargetEntity, targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read moved attachment from 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"); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + moveObjectIds.size(), + sourceMetadataAfterMove.size(), + "Source entity should still have attachments in UI when sourceFacet is not specified"); + + for (Map metadata : sourceMetadataAfterMove) { + String objectId = (String) metadata.get("objectId"); + assertTrue( + moveObjectIds.contains(objectId), + "Source entity should still show attachment with objectId: " + objectId); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // 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(69) + public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + System.out.println( + "Test (69): Move attachments into existing Target Entity when duplicate exists 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"); + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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 saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // 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()); - // } - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + 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()); + } + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target 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"); - - // List createTargetResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // srvpath, - // targetPostData, - // files.get(0)); - // if (!createTargetResponse.get(0).equals("Attachment created")) { - // fail("Could not create duplicate attachment in target 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"); - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + List createTargetResponse = + api.createAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + srvpath, + targetPostData, + files.get(0)); + if (!createTargetResponse.get(0).equals("Attachment created")) { + fail("Could not create duplicate attachment in target entity"); + } - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int initialTargetCount = targetMetadataBeforeMove.size(); - - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int initialTargetCount = targetMetadataBeforeMove.size(); + + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // int nonDuplicateCount = moveObjectIds.size() - 1; - // int expectedTargetCount = initialTargetCount + nonDuplicateCount; + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target entity should have initial attachments plus non-duplicate moved attachments"); + int nonDuplicateCount = moveObjectIds.size() - 1; + int expectedTargetCount = initialTargetCount + nonDuplicateCount; - // assertTrue( - // targetMetadataAfterMove.size() > initialTargetCount, - // "Target should have more attachments after move (non-duplicates added)"); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have initial attachments plus non-duplicate moved attachments"); - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // moveObjectIds.size(), - // sourceMetadataAfterMove.size(), - // "Source entity should still have all attachments in UI when sourceFacet is not - // specified"); + assertTrue( + targetMetadataAfterMove.size() > initialTargetCount, + "Target should have more attachments after move (non-duplicates added)"); - // 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); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + moveObjectIds.size(), + sourceMetadataAfterMove.size(), + "Source entity should still have all attachments in UI when sourceFacet is not specified"); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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); + } - // @Test - // @Order(70) - // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - // throws Exception { - // System.out.println( - // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + @Test + @Order(70) + public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + throws Exception { + System.out.println( + "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - // 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())); + for (int i = 0; i < facet.length; i++) { + 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"); + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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 sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update notes for attachment: " + attachmentId); + } + } - // 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, facet[i], moveSourceEntity, attachmentId, updateNotesBody); - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update notes for attachment: " + attachmentId); - // } - // } + Integer customProperty2Value = 54321; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + + for (String attachmentId : sourceAttachmentIds) { + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); + if (!updateCustomPropertyResponse.equals("Updated")) { + fail("Could not update custom property for attachment: " + attachmentId); + } + } - // Integer customProperty2Value = 54321; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, bodyInt); - // if (!updateCustomPropertyResponse.equals("Updated")) { - // fail("Could not update custom property for attachment: " + attachmentId); - // } - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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); + } + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + List> sourceMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not 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"); + } - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have " + expectedTargetCount + " attachments after move"); + + 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, facet[i], moveTargetEntity, targetAttachmentId); + + 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); + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target entity should have " + expectedTargetCount + " attachments after move"); - - // 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, facet[i], moveTargetEntity, - // targetAttachmentId); - - // 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); - // } + 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 (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); - // } - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + sourceCountBeforeMove, + sourceMetadataAfterMove.size(), + "Source entity should still have " + + sourceCountBeforeMove + + " attachments (without sourceFacet)"); + + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // sourceCountBeforeMove, - // sourceMetadataAfterMove.size(), - // "Source entity should still have " - // + sourceCountBeforeMove - // + " attachments (without sourceFacet)"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + @Test + @Order(71) + public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + System.out.println( + "Test (71): Move attachments with invalid or undefined secondary properties"); - // @Test - // @Order(71) - // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - // System.out.println( - // "Test (71): Move attachments with invalid or undefined secondary properties"); + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // 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())); + String validAttachmentId = sourceAttachmentIds.get(0); + Integer validCustomProperty2Value = 12345; + RequestBody validPropertyBody = + RequestBody.create( + "{\"customProperty2\": " + validCustomProperty2Value + "}", + MediaType.parse("application/json")); + + String validPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, validPropertyBody); + if (!validPropertyResponse.equals("Updated")) { + fail("Could not update valid property for attachment: " + validAttachmentId); + } - // 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"); + String invalidAttachmentId = sourceAttachmentIds.get(1); + RequestBody invalidPropertyBody = + RequestBody.create( + "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); + + api.updateSecondaryProperty( + appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, invalidPropertyBody); + + String undefinedAttachmentId = sourceAttachmentIds.get(2); + RequestBody undefinedPropertyBody = + RequestBody.create( + "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", + MediaType.parse("application/json")); + + api.updateSecondaryProperty( + appUrl, + entityName, + facet[i], + moveSourceEntity, + undefinedAttachmentId, + undefinedPropertyBody); + + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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); + } + } - // String validAttachmentId = sourceAttachmentIds.get(0); - // Integer validCustomProperty2Value = 12345; - // RequestBody validPropertyBody = - // RequestBody.create( - // "{\"customProperty2\": " + validCustomProperty2Value + "}", - // MediaType.parse("application/json")); - - // String validPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], moveSourceEntity, validAttachmentId, - // validPropertyBody); - // if (!validPropertyResponse.equals("Updated")) { - // fail("Could not update valid property for attachment: " + validAttachmentId); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // String invalidAttachmentId = sourceAttachmentIds.get(1); - // RequestBody invalidPropertyBody = - // RequestBody.create( - // "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); - - // api.updateSecondaryProperty( - // appUrl, entityName, facet[i], moveSourceEntity, invalidAttachmentId, - // invalidPropertyBody); - - // String undefinedAttachmentId = sourceAttachmentIds.get(2); - // RequestBody undefinedPropertyBody = - // RequestBody.create( - // "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", - // MediaType.parse("application/json")); - - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facet[i], - // moveSourceEntity, - // undefinedAttachmentId, - // undefinedPropertyBody); - - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + List> sourceMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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); - // } - // } + 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 saveTargetBeforeMoveResponseTest72 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); + } - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // // Save target before move - // String saveTargetBeforeMoveResponseTest72 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponseTest72.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest72); - // } + assertTrue( + targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + assertEquals( + sourceCountBeforeMove, + targetMetadataAfterMove.size(), + "All attachments should move (invalid properties are ignored)"); + + 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, facet[i], moveTargetEntity, targetAttachmentId); + + if (detailedMetadata.containsKey("customProperty2") + && detailedMetadata.get("customProperty2") != null) { + assertEquals( + validCustomProperty2Value, + detailedMetadata.get("customProperty2"), + "Valid customProperty2 should be preserved"); + } + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterMove.size(), + "Source entity should have no attachments after move with sourceFacet"); - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - - // assertTrue( - // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after - // move"); - // assertEquals( - // sourceCountBeforeMove, - // targetMetadataAfterMove.size(), - // "All attachments should move (invalid properties are ignored)"); - - // 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, facet[i], moveTargetEntity, - // targetAttachmentId); - - // if (detailedMetadata.containsKey("customProperty2") - // && detailedMetadata.get("customProperty2") != null) { - // assertEquals( - // validCustomProperty2Value, - // detailedMetadata.get("customProperty2"), - // "Valid customProperty2 should be preserved"); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterMove.size(), - // "Source entity should have no attachments after move with sourceFacet"); + @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"); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create 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"); + 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create 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); + } - // 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())); + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // 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"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // 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 editSourceResponse = + api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!editSourceResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity back to draft mode"); + } - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + 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 saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // 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"); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertTrue( + targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + assertEquals( + sourceCountBeforeMove, + targetMetadataAfterMove.size(), + "Target should have " + sourceCountBeforeMove + " attachments after move"); + + 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()); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String saveSourceAfterMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceAfterMoveResponse.equals("Saved")) { + fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); + } - // // Save target before move - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], 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()); + } - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertTrue( - // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after - // move"); - // assertEquals( - // sourceCountBeforeMove, - // targetMetadataAfterMove.size(), - // "Target should have " + sourceCountBeforeMove + " attachments after move"); - - // 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()); - // } + @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"); - // String saveSourceAfterMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceAfterMoveResponse.equals("Saved")) { - // fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); - // } + for (int i = 0; i < facet.length; i++) { + 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, facet[i], 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()); - // } + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + 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"); - // @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"); + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment in source entity"); + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String attachmentId = createResponse.get(1); + assertNotNull(attachmentId, "Attachment ID should not be null"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + 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", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + List> metadataBeforeRename = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); + assertEquals( + "sample.txt", + metadataBeforeRename.get(0).get("fileName"), + "Original filename should be sample.txt"); + + String editSourceResponse = + api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!editSourceResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity to draft mode"); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], moveSourceEntity, srvpath, postData, originalFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in source entity"); - // } + String newFileName = "testEdited.txt"; + String renameResponse = + api.renameAttachment( + appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); + assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); - // String attachmentId = createResponse.get(1); - // assertNotNull(attachmentId, "Attachment ID should not be null"); + saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after rename: " + saveSourceResponse); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + List> metadataAfterRename = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); + assertEquals( + newFileName, + metadataAfterRename.get(0).get("fileName"), + "Filename should be updated to " + newFileName); + + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); + moveObjectIds.add(objectId); + + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // List> metadataBeforeRename = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); - // assertEquals( - // "sample.txt", - // metadataBeforeRename.get(0).get("fileName"), - // "Original filename should be sample.txt"); - - // String editSourceResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!editSourceResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity to draft mode"); - // } + // Save target before move + String saveTargetBeforeMoveResponseTest73 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); + } - // String newFileName = "testEdited.txt"; - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facet[i], moveSourceEntity, attachmentId, newFileName); - // assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after rename: " + saveSourceResponse); - // } + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); - // List> metadataAfterRename = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); - // assertEquals( - // newFileName, - // metadataAfterRename.get(0).get("fileName"), - // "Filename should be updated to " + newFileName); - - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], 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 = new ArrayList<>(); - // moveObjectIds.add(objectId); - - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterMove.size(), + "Source entity should have no attachments after move with sourceFacet"); - // // Save target before move - // String saveTargetBeforeMoveResponseTest73 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponseTest73.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponseTest73); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + @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"); - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], 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); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterMove.size(), - // "Source entity should have no attachments after move with sourceFacet"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + for (int i = 0; i < facet.length; i++) { + 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"); + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // for (int i = 0; i < facet.length; i++) { - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // 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())); + int sourceCountInitial = sourceAttachmentIds.size(); + assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // 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"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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"); - // } - // } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity 1"); + } - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + 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"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult1 = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult1 == null) { + fail("Move operation from source to target 1 returned null result"); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + List> target1MetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertTrue( + target1MetadataAfterMove.size() > 0, + "Target entity 1 should have attachments after move"); + assertEquals( + sourceCountInitial, + target1MetadataAfterMove.size(), + "Target 1 should have " + sourceCountInitial + " attachments"); + + 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()); + } - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity 1"); - // } + List> sourceMetadataAfterFirstMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterFirstMove.size(), + "Source entity should have no attachments after move to target 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"); - // } + String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity2.equals("Could not create entity")) { + fail("Could not create target entity 2"); + } - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult1 = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult1 == null) { - // fail("Move operation from source to target 1 returned null result"); - // } + // 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"); + } - // List> target1MetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertTrue( - // target1MetadataAfterMove.size() > 0, - // "Target entity 1 should have attachments after move"); - // assertEquals( - // sourceCountInitial, - // target1MetadataAfterMove.size(), - // "Target 1 should have " + sourceCountInitial + " attachments"); - - // 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()); - // } + List target1AttachmentIds = new ArrayList<>(); + for (Map metadata : target1MetadataAfterMove) { + String attachmentId = metadata.get("ID").toString(); + target1AttachmentIds.add(attachmentId); + } - // List> sourceMetadataAfterFirstMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterFirstMove.size(), - // "Source entity should have no attachments after move to target 1"); + moveObjectIds = new ArrayList<>(); + String target1FolderId = null; + for (String attachmentId : target1AttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + 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()); + } + } - // String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity2.equals("Could not create entity")) { - // fail("Could not create target entity 2"); - // } + assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); + + Map moveResult2 = + api.moveAttachment( + appUrl, + entityName, + facet[i], + moveTargetEntity2, + target1FolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult2 == null) { + fail("Move operation from target 1 to target 2 returned null result"); + } - // // 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"); - // } + List> target2MetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); + assertTrue( + target2MetadataAfterMove.size() > 0, + "Target entity 2 should have attachments after move"); + assertEquals( + sourceCountInitial, + target2MetadataAfterMove.size(), + "Target 2 should have " + sourceCountInitial + " attachments"); + + 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()); + } - // List target1AttachmentIds = new ArrayList<>(); - // for (Map metadata : target1MetadataAfterMove) { - // String attachmentId = metadata.get("ID").toString(); - // target1AttachmentIds.add(attachmentId); - // } + List> target1MetadataAfterSecondMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + 0, + target1MetadataAfterSecondMove.size(), + "Target entity 1 should have no attachments after move to target 2"); - // moveObjectIds = new ArrayList<>(); - // String target1FolderId = null; - // for (String attachmentId : target1AttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveTargetEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // 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()); - // } - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity2); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); - - // Map moveResult2 = - // api.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // moveTargetEntity2, - // target1FolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult2 == null) { - // fail("Move operation from target 1 to target 2 returned null result"); - // } + @Test + @Order(75) + public void testMoveAttachmentsWithoutSDMRole() throws Exception { + System.out.println("Test (75): Move attachments when user does not have SDM Role"); - // List> target2MetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity2); - // assertTrue( - // target2MetadataAfterMove.size() > 0, - // "Target entity 2 should have attachments after move"); - // assertEquals( - // sourceCountInitial, - // target2MetadataAfterMove.size(), - // "Target 2 should have " + sourceCountInitial + " attachments"); - - // 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()); - // } + for (int i = 0; i < facet.length; i++) { + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // List> target1MetadataAfterSecondMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // 0, - // target1MetadataAfterSecondMove.size(), - // "Target entity 1 should have no attachments after move to target 2"); + 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 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"); + + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facet[i], 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"); + } + } - // api.deleteEntity(appUrl, entityName, moveTargetEntity2); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // @Test - // @Order(75) - // public void testMoveAttachmentsWithoutSDMRole() throws Exception { - // System.out.println("Test (75): Move attachments when user does not have SDM Role"); + int sourceCountInitial = sourceAttachmentIds.size(); + assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + moveObjectIds = new ArrayList<>(); + moveSourceFolderId = null; + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // for (int i = 0; i < facet.length; i++) { - // 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"); + } - // 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(moveSourceFolderId, "Source folder ID 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"); + moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity with no SDM role"); + } - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facet[i], 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 = + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + String sourceFacet = serviceName + "." + entityName + "." + facet[i]; + String targetFacet = serviceName + "." + entityName + "." + facet[i]; + Map moveResult = null; + boolean moveOperationFailed = false; + String errorMessage = null; + + try { + moveResult = + apiNoRoles.moveAttachment( + appUrl, + entityName, + facet[i], + 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(); + } - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - // moveObjectIds = new ArrayList<>(); - // moveSourceFolderId = null; - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facet[i], moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + 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); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); + assertEquals( + sourceCountInitial, + sourceMetadataAfterMove.size(), + "Source should still have all attachments after failed move"); - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); + assertEquals( + 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed move"); - // moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity with no SDM role"); - // } + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } + } - // // Save target before move - // String saveTargetBeforeMoveResponse = - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + @Test + @Order(76) + void testRenameAttachmentWithExtensionChange() throws IOException { + System.out.println( + "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - should return extension change warning"); - // String sourceFacet = serviceName + "." + entityName + "." + facet[i]; - // String targetFacet = serviceName + "." + entityName + "." + facet[i]; - // Map moveResult = null; - // boolean moveOperationFailed = false; - // String errorMessage = null; - - // try { - // moveResult = - // apiNoRoles.moveAttachment( - // appUrl, - // entityName, - // facet[i], - // 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 1: Create a new entity + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (newEntityID.equals("Could not create entity")) { + fail("Could not create entity"); + } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!saveResponse.equals("Saved")) { + fail("Could not save new entity: " + saveResponse); + } - // 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); - - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveSourceEntity); - // assertEquals( - // sourceCountInitial, - // sourceMetadataAfterMove.size(), - // "Source should still have all attachments after failed move"); - - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facet[i], moveTargetEntity); - // assertEquals( - // 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed - // move"); - - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } - // } + // Step 2: Upload a PDF attachment to each facet + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // @Test - // @Order(76) - // void testRenameAttachmentWithExtensionChange() throws IOException { - // System.out.println( - // "Test (76) : Rename attachment changing extension from .pdf to .txt across all facets - - // should return extension change warning"); + 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"); - // // Step 1: Create a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (newEntityID.equals("Could not create entity")) { - // fail("Could not create entity"); - // } - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save new entity: " + saveResponse); - // } + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!"Entity in draft mode".equals(editResponse)) { + fail("Could not put entity in draft mode for PDF upload"); + } - // // Step 2: Upload a PDF attachment to each facet - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // 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"); - - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!"Entity in draft mode".equals(editResponse)) { - // fail("Could not put entity in draft mode for PDF upload"); - // } + String[] facetAttachmentIDs = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + facetAttachmentIDs[i] = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + if (facetAttachmentIDs[i] == null) { + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not upload sample.pdf to facet: " + facet[i]); + } + } - // String[] facetAttachmentIDs = new String[facet.length]; - // for (int i = 0; i < facet.length; i++) { - // facetAttachmentIDs[i] = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - // if (facetAttachmentIDs[i] == null) { - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not upload sample.pdf to facet: " + facet[i]); - // } - // } + // Step 3: Save the entity + String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!savedAfterUpload.equals("Saved")) { + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not save entity after PDF upload: " + savedAfterUpload); + } - // // Step 3: Save the entity - // String savedAfterUpload = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!savedAfterUpload.equals("Saved")) { - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not save entity after PDF upload: " + savedAfterUpload); - // } + // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> .txt + for (int i = 0; i < facet.length; i++) { + String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + if (!"Entity in draft mode".equals(editDraftResponse)) { + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not put entity in draft mode for rename on facet: " + facet[i]); + } - // // Step 4 & 5: Edit the entity, rename each facet's attachment changing extension .pdf -> - // .txt - // for (int i = 0; i < facet.length; i++) { - // String editDraftResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // if (!"Entity in draft mode".equals(editDraftResponse)) { - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not put entity in draft mode for rename on facet: " + facet[i]); - // } + String renameResponse = + api.renameAttachment( + appUrl, + entityName, + facet[i], + newEntityID, + facetAttachmentIDs[i], + "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + api.deleteEntity(appUrl, entityName, newEntityID); + fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + } - // String renameResponse = - // api.renameAttachment( - // appUrl, - // entityName, - // facet[i], - // newEntityID, - // facetAttachmentIDs[i], - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // api.deleteEntity(appUrl, entityName, newEntityID); - // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - // } + // Step 6: Save and validate the extension change warning message + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - // // Step 6: Save and validate the extension change warning message - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); - // } + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } - // // Clean up - // api.deleteEntity(appUrl, entityName, newEntityID); - // } + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + } - // @Test - // @Order(77) - // void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { - // System.out.println( - // "Test (77) : Upload attachment in draft, rename changing extension before save across all - // facets - should return extension change warning"); + // Clean up + api.deleteEntity(appUrl, entityName, newEntityID); + } - // for (int i = 0; i < facet.length; i++) { - // // Step 1: Create a new entity draft (do NOT save it yet) - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (newEntityID.equals("Could not create entity")) { - // fail("Could not create entity for facet: " + facet[i]); - // } + @Test + @Order(77) + void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException { + System.out.println( + "Test (77) : Upload attachment in draft, rename changing extension before save across all facets - should return extension change warning"); - // // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); + for (int i = 0; i < facet.length; i++) { + // Step 1: Create a new entity draft (do NOT save it yet) + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (newEntityID.equals("Could not create entity")) { + fail("Could not create entity for facet: " + facet[i]); + } - // 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"); + // Step 2: Upload a PDF attachment while entity is still in draft (unsaved) + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + 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"); + + String facetAttachmentID = + CreateandReturnFacetID( + appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); + if (facetAttachmentID == null) { + api.deleteEntityDraft(appUrl, entityName, newEntityID); + fail("Could not upload sample.pdf to facet: " + facet[i]); + } - // String facetAttachmentID = - // CreateandReturnFacetID( - // appUrl, serviceName, entityName, facet[i], newEntityID, postData, file); - // if (facetAttachmentID == null) { - // api.deleteEntityDraft(appUrl, entityName, newEntityID); - // fail("Could not upload sample.pdf to facet: " + facet[i]); - // } + // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not saved + String renameResponse = + api.renameAttachment( + appUrl, entityName, facet[i], newEntityID, facetAttachmentID, "renamed_document.txt"); + if (!"Renamed".equals(renameResponse)) { + api.deleteEntityDraft(appUrl, entityName, newEntityID); + fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); + } - // // Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not - // saved - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facet[i], newEntityID, facetAttachmentID, - // "renamed_document.txt"); - // if (!"Renamed".equals(renameResponse)) { - // api.deleteEntityDraft(appUrl, entityName, newEntityID); - // fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse); - // } + // Step 4: Save — should receive extension change warning, not "Saved" + String saveWithWarningResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]); - // // Step 4: Save — should receive extension change warning, not "Saved" - // String saveWithWarningResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + - // facet[i]); - - // String expectedMessage = - // "Changing the file extension is not allowed. The file \"renamed_document.txt\" must - // retain its original extension \".pdf\"."; - - // com.fasterxml.jackson.databind.JsonNode messagesNode = - // new ObjectMapper().readTree(saveWithWarningResponse); - // assertTrue( - // messagesNode.isArray(), - // "sap-messages response should be a JSON array for facet: " + facet[i]); - - // boolean foundExtensionError = false; - // for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { - // if (messageNode.has("message")) { - // String message = messageNode.get("message").asText(); - // if (message.contains("Changing the file extension is not allowed")) { - // foundExtensionError = true; - // assertEquals( - // expectedMessage, - // message, - // "Extension change error message does not match for facet: " + facet[i]); - // break; - // } - // } - // } + String expectedMessage = + "Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\"."; - // assertTrue( - // foundExtensionError, - // "Expected extension change warning not found for facet: " - // + facet[i] - // + ". Full response: " - // + saveWithWarningResponse); + com.fasterxml.jackson.databind.JsonNode messagesNode = + new ObjectMapper().readTree(saveWithWarningResponse); + assertTrue( + messagesNode.isArray(), + "sap-messages response should be a JSON array for facet: " + facet[i]); + + boolean foundExtensionError = false; + for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) { + if (messageNode.has("message")) { + String message = messageNode.get("message").asText(); + if (message.contains("Changing the file extension is not allowed")) { + foundExtensionError = true; + assertEquals( + expectedMessage, + message, + "Extension change error message does not match for facet: " + facet[i]); + break; + } + } + } - // // Clean up - // api.deleteEntity(appUrl, entityName, newEntityID); - // } - // } + assertTrue( + foundExtensionError, + "Expected extension change warning not found for facet: " + + facet[i] + + ". Full response: " + + saveWithWarningResponse); + + // Clean up + api.deleteEntity(appUrl, entityName, newEntityID); + } + } @Test @Order(78) @@ -7704,7 +7496,7 @@ void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOExcepti // fail("Failed to parse error response for references facet: " + e.getMessage()); // } // } else { - // fail("Attachment got created in references facet with file size exceeding maximum + // fail("Attachment got created in references facet with file size exceeding maximum // // limit"); // } // } else { 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 7f076c1a9..444a63841 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 @@ -2,14 +2,18 @@ import static org.junit.jupiter.api.Assertions.*; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.File; import java.io.IOException; 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.*; From d196cb8de4c7399cd9ebd2d3b893ee8f19b238d4 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 18:30:14 +0530 Subject: [PATCH 12/13] fix for test failure --- .../IntegrationTest_Chapters_MultipleFacet.java | 17 ++++++++++++++--- .../cds/sdm/IntegrationTest_MultipleFacet.java | 8 ++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) 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 9ae9dcf92..d3481ed47 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 @@ -6598,6 +6598,7 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { ClassLoader classLoader = getClass().getClassLoader(); Map> facetAttachmentIds = new HashMap<>(); + int facetIndex = 0; for (String facetName : facet) { List ids = new ArrayList<>(); Map postData = new HashMap<>(); @@ -6607,10 +6608,13 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { postData.put("modifiedBy", "test@test.com"); postData.put("mimeType", "application/pdf"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + File pdfOrig = new File(classLoader.getResource("sample.pdf").getFile()); + File pdfFile = File.createTempFile("sample_ch_" + facetIndex + "_pdf_", ".pdf"); + Files.copy(pdfOrig.toPath(), pdfFile.toPath(), StandardCopyOption.REPLACE_EXISTING); List r1 = api.createAttachment( appUrl, chapterEntityName, facetName, draftChapterID, srvpath, postData, pdfFile); + pdfFile.delete(); if (!r1.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); fail("Could not upload sample.pdf for facet " + facetName); @@ -6619,10 +6623,13 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { ids.add(r1.get(1)); postData.put("mimeType", "application/txt"); - File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + File txtOrig = new File(classLoader.getResource("sample.txt").getFile()); + File txtFile = File.createTempFile("sample_ch_" + facetIndex + "_txt_", ".txt"); + Files.copy(txtOrig.toPath(), txtFile.toPath(), StandardCopyOption.REPLACE_EXISTING); List r2 = api.createAttachment( appUrl, chapterEntityName, facetName, draftChapterID, srvpath, postData, txtFile); + txtFile.delete(); if (!r2.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); fail("Could not upload sample.txt for facet " + facetName); @@ -6631,10 +6638,13 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { ids.add(r2.get(1)); postData.put("mimeType", "application/exe"); - File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + File exeOrig = new File(classLoader.getResource("sample.exe").getFile()); + File exeFile = File.createTempFile("sample_ch_" + facetIndex + "_exe_", ".exe"); + Files.copy(exeOrig.toPath(), exeFile.toPath(), StandardCopyOption.REPLACE_EXISTING); List r3 = api.createAttachment( appUrl, chapterEntityName, facetName, draftChapterID, srvpath, postData, exeFile); + exeFile.delete(); if (!r3.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); fail("Could not upload sample.exe for facet " + facetName); @@ -6642,6 +6652,7 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { } ids.add(r3.get(1)); facetAttachmentIds.put(facetName, ids); + facetIndex++; } for (String facetName : facet) { 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 5d254871d..cbb344311 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 @@ -7085,12 +7085,16 @@ void testDownloadAttachmentsAcrossMultipleFacets() throws IOException { 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()); + File originalPdfFile = new File(classLoader.getResource("sample.pdf").getFile()); String[] facetAttachmentIDs = new String[facet.length]; for (int i = 0; i < facet.length; i++) { + File tempFacetFile = File.createTempFile("sample_mf_facet" + i + "_", ".pdf"); + Files.copy( + originalPdfFile.toPath(), tempFacetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); List createResp = api.createAttachment( - appUrl, entityName, facet[i], downloadTestEntityID, srvpath, postData, pdfFile); + appUrl, entityName, facet[i], downloadTestEntityID, srvpath, postData, tempFacetFile); + tempFacetFile.delete(); if (!createResp.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); fail("Could not upload pdf to facet: " + facet[i]); From 6f8b3a86a5c94232bd5a373018f7f1f08c423227 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 2 Jun 2026 19:06:23 +0530 Subject: [PATCH 13/13] Fix to multifacet & chapters --- .../IntegrationTest_Chapters_MultipleFacet.java | 9 +++++++-- .../sap/cds/sdm/IntegrationTest_MultipleFacet.java | 14 ++++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) 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 d3481ed47..664a60438 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 @@ -6950,7 +6950,9 @@ void testDownloadButtonDisabledWithLinkInActiveState() throws IOException { } ClassLoader classLoader = getClass().getClassLoader(); + File origPdfFile = new File(classLoader.getResource("sample.pdf").getFile()); Map facetPdfId = new HashMap<>(); + int facetIdx = 0; for (String facetName : facet) { Map postData = new HashMap<>(); postData.put("up__ID", testChapterID); @@ -6959,16 +6961,19 @@ void testDownloadButtonDisabledWithLinkInActiveState() throws IOException { postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + File tempPdf = File.createTempFile("sample_ch_link_" + facetIdx + "_", ".pdf"); + Files.copy(origPdfFile.toPath(), tempPdf.toPath(), StandardCopyOption.REPLACE_EXISTING); List pdfResponse = api.createAttachment( - appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, pdfFile); + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, tempPdf); + tempPdf.delete(); 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)); + facetIdx++; String linkResp = api.createLink( 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 cbb344311..fd0e0d7bf 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 @@ -7302,12 +7302,15 @@ void testDownloadAttachmentsAcrossMultipleFacetsInDraftState() throws IOExceptio 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()); + File origPdfFile = new File(classLoader.getResource("sample.pdf").getFile()); String[] draftFacetAttachmentIDs = new String[facet.length]; for (int i = 0; i < facet.length; i++) { + File tempFacetFile = File.createTempFile("sample_mf_draft_facet" + i + "_", ".pdf"); + Files.copy(origPdfFile.toPath(), tempFacetFile.toPath(), StandardCopyOption.REPLACE_EXISTING); List createResp = api.createAttachment( - appUrl, entityName, facet[i], draftEntityID, srvpath, postData, pdfFile); + appUrl, entityName, facet[i], draftEntityID, srvpath, postData, tempFacetFile); + tempFacetFile.delete(); if (!createResp.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, entityName, draftEntityID); fail("Could not upload pdf to facet in draft state: " + facet[i]); @@ -7364,10 +7367,13 @@ void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOExcepti postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + File origPdf = new File(classLoader.getResource("sample.pdf").getFile()); + File tempPdf = File.createTempFile("sample_mf_draftlink_", ".pdf"); + Files.copy(origPdf.toPath(), tempPdf.toPath(), StandardCopyOption.REPLACE_EXISTING); List createResponse = api.createAttachment( - appUrl, entityName, facet[0], testEntityID, srvpath, postData, pdfFile); + appUrl, entityName, facet[0], testEntityID, srvpath, postData, tempPdf); + tempPdf.delete(); if (!createResponse.get(0).equals("Attachment created")) { api.deleteEntityDraft(appUrl, entityName, testEntityID); fail("Could not upload pdf to facet: " + facet[0]);