diff --git a/server/src/main/java/com/epam/aidial/core/server/controller/DeploymentPostController.java b/server/src/main/java/com/epam/aidial/core/server/controller/DeploymentPostController.java index c9a8e642..926a673d 100644 --- a/server/src/main/java/com/epam/aidial/core/server/controller/DeploymentPostController.java +++ b/server/src/main/java/com/epam/aidial/core/server/controller/DeploymentPostController.java @@ -15,7 +15,7 @@ import com.epam.aidial.core.server.function.CollectRequestAttachmentsFn; import com.epam.aidial.core.server.function.CollectRequestDataFn; import com.epam.aidial.core.server.function.CollectResponseAttachmentsFn; -import com.epam.aidial.core.server.function.enhancement.AppendCustomApplicationPropertiesFn; +import com.epam.aidial.core.server.function.enhancement.AppendApplicationPropertiesFn; import com.epam.aidial.core.server.function.enhancement.ApplyDefaultDeploymentSettingsFn; import com.epam.aidial.core.server.function.enhancement.EnhanceAssistantRequestFn; import com.epam.aidial.core.server.function.enhancement.EnhanceModelRequestFn; @@ -73,7 +73,7 @@ public DeploymentPostController(Proxy proxy, ProxyContext context) { new ApplyDefaultDeploymentSettingsFn(proxy, context), new EnhanceAssistantRequestFn(proxy, context), new EnhanceModelRequestFn(proxy, context), - new AppendCustomApplicationPropertiesFn(proxy, context)); + new AppendApplicationPropertiesFn(proxy, context)); } public Future handle(String deploymentId, String deploymentApi) { diff --git a/server/src/main/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFn.java b/server/src/main/java/com/epam/aidial/core/server/function/enhancement/AppendApplicationPropertiesFn.java similarity index 61% rename from server/src/main/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFn.java rename to server/src/main/java/com/epam/aidial/core/server/function/enhancement/AppendApplicationPropertiesFn.java index 27820acb..3a7154d1 100644 --- a/server/src/main/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFn.java +++ b/server/src/main/java/com/epam/aidial/core/server/function/enhancement/AppendApplicationPropertiesFn.java @@ -7,14 +7,15 @@ import com.epam.aidial.core.server.function.BaseRequestFunction; import com.epam.aidial.core.server.util.ApplicationTypeSchemaUtils; import com.epam.aidial.core.server.util.ProxyUtil; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.extern.slf4j.Slf4j; import java.util.Map; @Slf4j -public class AppendCustomApplicationPropertiesFn extends BaseRequestFunction { - public AppendCustomApplicationPropertiesFn(Proxy proxy, ProxyContext context) { +public class AppendApplicationPropertiesFn extends BaseRequestFunction { + public AppendApplicationPropertiesFn(Proxy proxy, ProxyContext context) { super(proxy, context); } @@ -25,11 +26,14 @@ public Boolean apply(ObjectNode tree) { return false; } Map props = ApplicationTypeSchemaUtils.getCustomServerProperties(context.getConfig(), application); - ObjectNode customAppPropertiesNode = ProxyUtil.MAPPER.createObjectNode(); - for (Map.Entry entry : props.entrySet()) { - customAppPropertiesNode.putPOJO(entry.getKey(), entry.getValue()); + ObjectNode customFieldsNode = ProxyUtil.MAPPER.createObjectNode(); + customFieldsNode.set("application_properties", ProxyUtil.MAPPER.valueToTree(props)); + JsonNode currentCustomFields = tree.get("custom_fields"); + if (currentCustomFields != null) { + ((ObjectNode) currentCustomFields).setAll(customFieldsNode); + } else { + tree.set("custom_fields", customFieldsNode); } - tree.set("custom_application_properties", customAppPropertiesNode); return true; } } diff --git a/server/src/test/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFnTest.java b/server/src/test/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFnTest.java index bba46b65..fad6e533 100644 --- a/server/src/test/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFnTest.java +++ b/server/src/test/java/com/epam/aidial/core/server/function/enhancement/AppendCustomApplicationPropertiesFnTest.java @@ -6,6 +6,7 @@ import com.epam.aidial.core.server.Proxy; import com.epam.aidial.core.server.ProxyContext; import com.epam.aidial.core.server.util.ProxyUtil; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.node.ObjectNode; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -38,7 +39,7 @@ public class AppendCustomApplicationPropertiesFnTest { private Application application; - private AppendCustomApplicationPropertiesFn function; + private AppendApplicationPropertiesFn function; private final String schema = "{" + "\"$schema\": \"https://dial.epam.com/application_type_schemas/schema#\"," @@ -72,13 +73,13 @@ public class AppendCustomApplicationPropertiesFnTest { @BeforeEach void setUp() { MockitoAnnotations.openMocks(this); - function = new AppendCustomApplicationPropertiesFn(proxy, context); + function = new AppendApplicationPropertiesFn(proxy, context); application = new Application(); when(context.getConfig()).thenReturn(config); } @Test - void apply_appendsCustomProperties_whenApplicationHasCustomSchemaId() { + void apply_appendsCustomProperties_whenApplicationHasCustomSchemaIdAndNoCustomFieldsPassed() { String serverFile = "files/public/valid-file-path/valid-sub-path/valid%20file%20name2.ext"; when(context.getDeployment()).thenReturn(application); application.setCustomAppSchemaId(URI.create("customSchemaId")); @@ -90,10 +91,10 @@ void apply_appendsCustomProperties_whenApplicationHasCustomSchemaId() { ObjectNode tree = ProxyUtil.MAPPER.createObjectNode(); boolean result = function.apply(tree); assertTrue(result); - assertNotNull(tree.get("custom_application_properties")); + assertNotNull(tree.get("custom_fields")); assertEquals(serverFile, - tree.get("custom_application_properties").get("serverFile").asText()); - assertFalse(tree.get("custom_application_properties").has("clientFile")); + tree.get("custom_fields").get("application_properties").get("serverFile").asText()); + assertFalse(tree.get("custom_fields").get("application_properties").has("clientFile")); } @Test @@ -105,7 +106,7 @@ void apply_returnsFalse_whenDeploymentIsNotApplication() { boolean result = function.apply(tree); assertFalse(result); - assertNull(tree.get("custom_application_properties")); + assertNull(tree.get("custom_fields")); } @Test @@ -117,11 +118,11 @@ void apply_returnsFalse_whenApplicationHasNoCustomSchemaId() { boolean result = function.apply(tree); assertFalse(result); - assertNull(tree.get("custom_application_properties")); + assertNull(tree.get("custom_fields")); } @Test - void apply_returnsFalse_whenCustomPropertiesAreEmpty() { + void apply_returnsTrue_whenCustomPropertiesAreEmptyAndApplicationHasCustomSchemaId() { when(context.getDeployment()).thenReturn(application); application.setCustomAppSchemaId(URI.create("customSchemaId")); Map customProps = new HashMap<>(); @@ -131,6 +132,35 @@ void apply_returnsFalse_whenCustomPropertiesAreEmpty() { ObjectNode tree = ProxyUtil.MAPPER.createObjectNode(); boolean result = function.apply(tree); assertTrue(result); - assertNotNull(tree.get("custom_application_properties")); + assertNotNull(tree.get("custom_fields")); + } + + @Test + void apply_appendsCustomProperties_whenApplicationHasCustomSchemaIdAndCustomFieldsPassed() throws JsonProcessingException { + String serverFile = "files/public/valid-file-path/valid-sub-path/valid%20file%20name2.ext"; + when(context.getDeployment()).thenReturn(application); + application.setCustomAppSchemaId(URI.create("customSchemaId")); + Map customProps = new HashMap<>(); + customProps.put("clientFile", "files/public/valid-file-path/valid-sub-path/valid%20file%20name1.ext"); + customProps.put("serverFile", serverFile); + application.setCustomProperties(customProps); + when(config.getCustomApplicationSchema(eq(URI.create("customSchemaId")))).thenReturn(schema); + ObjectNode tree = (ObjectNode) ProxyUtil.MAPPER.readTree(""" + { + "custom_fields": { + "foo": "bar" + } + } + """); + boolean result = function.apply(tree); + assertTrue(result); + assertNotNull(tree.get("custom_fields")); + assertEquals(serverFile, + tree.get("custom_fields").get("application_properties").get("serverFile").asText()); + assertFalse(tree.get("custom_fields").get("application_properties").has("clientFile")); + assertTrue(tree.get("custom_fields").has("foo")); + assertEquals("bar", + tree.get("custom_fields").get("foo").asText()); + } } \ No newline at end of file