Skip to content

Commit

Permalink
feat: Add mapping for iconUrl when publish applications #468 (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
astsiapanay authored Sep 6, 2024
1 parent 9f68c8c commit b1ab791
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ private void copyReviewToTargetResources(List<Publication.Resource> resources) {

private void replaceSourceToReviewLinks(List<Publication.Resource> resources) {
List<ResourceDescription> reviewConversations = new ArrayList<>();
List<ResourceDescription> reviewPublications = new ArrayList<>();
List<ResourceDescription> reviewApplications = new ArrayList<>();
Map<String, String> attachmentsMap = new HashMap<>();
for (Publication.Resource resource : resources) {
String sourceUrl = resource.getSourceUrl();
Expand All @@ -544,7 +544,7 @@ private void replaceSourceToReviewLinks(List<Publication.Resource> 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) {
Expand All @@ -553,8 +553,9 @@ private void replaceSourceToReviewLinks(List<Publication.Resource> 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));
}
}

Expand All @@ -579,7 +580,8 @@ private void replaceReviewToTargetLinks(List<Publication.Resource> 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));
}
}

Expand Down
33 changes: 30 additions & 3 deletions src/main/java/com/epam/aidial/core/service/PublicationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.experimental.UtilityClass;

import java.util.Map;
import java.util.Optional;

@UtilityClass
public class PublicationUtil {
Expand Down Expand Up @@ -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<String, String> 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<String, String> 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<String, String> attachmentsMapping) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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());
Expand Down

0 comments on commit b1ab791

Please sign in to comment.