From b1ab7913789dfe8482c7541cd1af1dc01a61e06a Mon Sep 17 00:00:00 2001 From: Aliaksandr Stsiapanay Date: Fri, 6 Sep 2024 15:43:30 +0300 Subject: [PATCH] feat: Add mapping for iconUrl when publish applications #468 (#473) --- .../core/service/PublicationService.java | 12 +++--- .../aidial/core/service/PublicationUtil.java | 33 ++++++++++++++-- .../core/service/PublicationUtilTest.java | 38 +++++++++++++++++++ 3 files changed, 75 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/epam/aidial/core/service/PublicationService.java b/src/main/java/com/epam/aidial/core/service/PublicationService.java index c43f887e6..518e67a68 100644 --- a/src/main/java/com/epam/aidial/core/service/PublicationService.java +++ b/src/main/java/com/epam/aidial/core/service/PublicationService.java @@ -535,7 +535,7 @@ private void copyReviewToTargetResources(List resources) { private void replaceSourceToReviewLinks(List resources) { List reviewConversations = new ArrayList<>(); - List reviewPublications = new ArrayList<>(); + List reviewApplications = new ArrayList<>(); Map attachmentsMap = new HashMap<>(); for (Publication.Resource resource : resources) { String sourceUrl = resource.getSourceUrl(); @@ -544,7 +544,7 @@ private void replaceSourceToReviewLinks(List resources) { ResourceDescription from = ResourceDescription.fromPrivateUrl(sourceUrl, encryption); ResourceDescription to = ResourceDescription.fromPrivateUrl(reviewUrl, encryption); - collectLinksForReplacement(reviewConversations, reviewPublications, attachmentsMap, from, to); + collectLinksForReplacement(reviewConversations, reviewApplications, attachmentsMap, from, to); } for (ResourceDescription reviewConversation : reviewConversations) { @@ -553,8 +553,9 @@ private void replaceSourceToReviewLinks(List resources) { ); } - for (ResourceDescription reviewPublication : reviewPublications) { - this.resources.computeResource(reviewPublication, body -> PublicationUtil.replaceApplicationIdentity(body, reviewPublication, false)); + for (ResourceDescription reviewApplication : reviewApplications) { + this.resources.computeResource(reviewApplication, body -> + PublicationUtil.replaceApplicationLinks(body, reviewApplication, false, attachmentsMap)); } } @@ -579,7 +580,8 @@ private void replaceReviewToTargetLinks(List resources) { } for (ResourceDescription publicApplication : publicApplications) { - this.resources.computeResource(publicApplication, body -> PublicationUtil.replaceApplicationIdentity(body, publicApplication, false)); + this.resources.computeResource(publicApplication, body -> + PublicationUtil.replaceApplicationLinks(body, publicApplication, false, attachmentsMap)); } } diff --git a/src/main/java/com/epam/aidial/core/service/PublicationUtil.java b/src/main/java/com/epam/aidial/core/service/PublicationUtil.java index a83d1c1b5..334dbfe56 100644 --- a/src/main/java/com/epam/aidial/core/service/PublicationUtil.java +++ b/src/main/java/com/epam/aidial/core/service/PublicationUtil.java @@ -10,6 +10,7 @@ import lombok.experimental.UtilityClass; import java.util.Map; +import java.util.Optional; @UtilityClass public class PublicationUtil { @@ -49,15 +50,41 @@ public String replaceConversationLinks(String conversationBody, ResourceDescript return conversation.toString(); } - public String replaceApplicationIdentity(String applicationBody, ResourceDescription targetResource, boolean preserveReference) { - JsonObject application = new JsonObject(applicationBody); + public String replaceApplicationLinks(String body, ResourceDescription targetResource, boolean preserveReference, Map attachmentsMapping) { + return Optional.ofNullable(body).map(JsonObject::new) + .map(app -> PublicationUtil.replaceApplicationIdentity(app, targetResource, preserveReference)) + .map(app -> PublicationUtil.replaceApplicationLinks(app, attachmentsMapping)) + .map(JsonObject::toString) + .orElse(null); + } + + private JsonObject replaceApplicationLinks(JsonObject application, Map attachmentsMapping) { + String iconUrl = application.getString("icon_url"); + if (iconUrl != null) { + String decodedIconUrl = UrlUtil.decodePath(iconUrl); + String toReplace = attachmentsMapping.get(decodedIconUrl); + if (toReplace != null) { + application.put("icon_url", toReplace); + } + } + return application; + } + + public String replaceApplicationIdentity(String body, ResourceDescription targetResource, boolean preserveReference) { + return Optional.ofNullable(body).map(JsonObject::new) + .map(app -> PublicationUtil.replaceApplicationIdentity(app, targetResource, preserveReference)) + .map(JsonObject::toString) + .orElse(null); + } + + private JsonObject replaceApplicationIdentity(JsonObject application, ResourceDescription targetResource, boolean preserveReference) { application.put("name", targetResource.getUrl()); if (!preserveReference) { application.put("reference", ApplicationUtil.generateReference()); } - return application.toString(); + return application; } private void replaceAttachments(JsonArray messages, Map attachmentsMapping) { diff --git a/src/test/java/com/epam/aidial/core/service/PublicationUtilTest.java b/src/test/java/com/epam/aidial/core/service/PublicationUtilTest.java index 100c1f8e7..7a79c43cf 100644 --- a/src/test/java/com/epam/aidial/core/service/PublicationUtilTest.java +++ b/src/test/java/com/epam/aidial/core/service/PublicationUtilTest.java @@ -340,6 +340,44 @@ void testReplaceApplicationIdentity() { assertNotEquals("id1", actualApplication.getReference()); } + @Test + void testReplaceApplicationLinks() { + String application = """ + { + "name":"applications/3CcedGxCx23EwiVbVmscVktScRyf46KypuBQ65miviST/my-custom-application", + "endpoint":"http://application1/v1/completions", + "display_name":"My Custom Application", + "display_version":"1.0", + "icon_url":"abc/files/myfolder/icon.svg", + "description":"My Custom Application Description", + "reference":"id1", + "forward_auth_token":false, + "defaults": {} + } + """; + Map attachmentMapping = Map.of("abc/files/myfolder/icon.svg", "public/folder/icon.svg"); + ResourceDescription targetResource1 = ResourceDescription.fromDecoded(ResourceType.APPLICATION, "bucketName", "bucket/location/", "my-app"); + verifyJson(""" + { + "name":"applications/bucketName/my-app", + "endpoint":"http://application1/v1/completions", + "display_name":"My Custom Application", + "display_version":"1.0", + "icon_url":"public/folder/icon.svg", + "description":"My Custom Application Description", + "reference":"id1", + "forward_auth_token":false, + "defaults": {} + } + """, PublicationUtil.replaceApplicationLinks(application, targetResource1, true, attachmentMapping)); + + Application actualApplication = ProxyUtil.convertToObject( + PublicationUtil.replaceApplicationIdentity(application, targetResource1, false), + Application.class, true); + assertEquals("applications/bucketName/my-app", actualApplication.getName()); + assertNotEquals("id1", actualApplication.getReference()); + } + private static void verifyJson(String expected, String actual) { try { assertEquals(ProxyUtil.MAPPER.readTree(expected).toPrettyString(), ProxyUtil.MAPPER.readTree(actual).toPrettyString());