Skip to content

Commit

Permalink
chore: Fix naming of a field to transfer server-side application prop…
Browse files Browse the repository at this point in the history
…erties (#630)
  • Loading branch information
sergey-zinchenko authored Dec 30, 2024
1 parent 1baecd8 commit 6f03973
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectNode> {
public AppendCustomApplicationPropertiesFn(Proxy proxy, ProxyContext context) {
public class AppendApplicationPropertiesFn extends BaseRequestFunction<ObjectNode> {
public AppendApplicationPropertiesFn(Proxy proxy, ProxyContext context) {
super(proxy, context);
}

Expand All @@ -25,11 +26,14 @@ public Boolean apply(ObjectNode tree) {
return false;
}
Map<String, Object> props = ApplicationTypeSchemaUtils.getCustomServerProperties(context.getConfig(), application);
ObjectNode customAppPropertiesNode = ProxyUtil.MAPPER.createObjectNode();
for (Map.Entry<String, Object> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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#\","
Expand Down Expand Up @@ -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"));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<String, Object> customProps = new HashMap<>();
Expand All @@ -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<String, Object> 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());

}
}

0 comments on commit 6f03973

Please sign in to comment.