Skip to content

Commit

Permalink
getCustomServerProperties throws when no application properties set
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-zinchenko committed Jan 16, 2025
1 parent 5cefd71 commit ecfbede
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ static String getCustomApplicationSchemaOrThrow(Config config, Application appli
}

@SuppressWarnings("unchecked")
private static Map<String, Object> filterProperties(Map<String, Object> customProps, String schema, String collectorName) {
private static Map<String, Object> filterProperties(Map<String, Object> applicationProperties, String schema, String collectorName) {
try {
JsonSchema appSchema = SCHEMA_FACTORY.getSchema(schema);
CollectorContext collectorContext = new CollectorContext();
String customPropsJson = ProxyUtil.MAPPER.writeValueAsString(customProps);
Set<ValidationMessage> validationResult = appSchema.validate(customPropsJson, InputFormat.JSON,
String applicationPropertiesJson = ProxyUtil.MAPPER.writeValueAsString(applicationProperties);
Set<ValidationMessage> validationResult = appSchema.validate(applicationPropertiesJson, InputFormat.JSON,
e -> e.setCollectorContext(collectorContext));
if (!validationResult.isEmpty()) {
throw new ApplicationTypeSchemaValidationException("Failed to validate custom app against the schema", validationResult);
Expand All @@ -76,7 +76,7 @@ private static Map<String, Object> filterProperties(Map<String, Object> customPr
}
Map<String, Object> result = new HashMap<>();
for (String propertyName : propsCollector.collect()) {
result.put(propertyName, customProps.get(propertyName));
result.put(propertyName, applicationProperties.get(propertyName));
}
return result;
} catch (ApplicationTypeSchemaValidationException e) {
Expand All @@ -91,6 +91,9 @@ public static Map<String, Object> getCustomServerProperties(Config config, Appli
if (customApplicationSchema == null) {
return Collections.emptyMap();
}
if (application.getApplicationProperties() == null) {
throw new ApplicationTypeSchemaValidationException("Typed application's properties not set");
}
return filterProperties(application.getApplicationProperties(), customApplicationSchema, "server");
}

Expand Down
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.epam.aidial.core.server.validation.ApplicationTypeSchemaValidationException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -21,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -78,6 +80,15 @@ void setUp() {
when(context.getConfig()).thenReturn(config);
}

@Test
void apply_throws_whenApplicationHasCustomSchemaIdAndNoCustomFieldsPassedAndApplicationPropertiesIsNull() {
when(context.getDeployment()).thenReturn(application);
application.setApplicationTypeSchemaId(URI.create("customSchemaId"));
when(config.getCustomApplicationSchema(eq(URI.create("customSchemaId")))).thenReturn(schema);
ObjectNode tree = ProxyUtil.MAPPER.createObjectNode();
assertThrows(ApplicationTypeSchemaValidationException.class, () -> function.apply(tree));
}

@Test
void apply_appendsCustomProperties_whenApplicationHasCustomSchemaIdAndNoCustomFieldsPassed() {
String serverFile = "files/public/valid-file-path/valid-sub-path/valid%20file%20name2.ext";
Expand Down

0 comments on commit ecfbede

Please sign in to comment.