Skip to content
Snippets Groups Projects
Commit b1b11ea1 authored by Henry Borasch's avatar Henry Borasch
Browse files

fall back to memory when schema loading fails, extended and andjusted tests

parent 422aef83
No related branches found
No related tags found
2 merge requests!139928 - Validate Submission Data,!128Add Schema URI via Builder, check against existing destinations
......@@ -117,7 +117,7 @@ public class SchemaResourceProvider implements SchemaProvider {
try {
return restTemplate.getForEntity(schemaUri, String.class).getBody();
} catch (Exception exception) {
throw new SchemaNotFoundException("Submission data schema " + schemaUri + " is not available.", exception);
LOGGER.warn("Fetching schema from " + schemaUri + " failed, falling back to pre-stored version.");
}
}
......
......@@ -116,26 +116,41 @@ class SchemaResourceProviderTest {
}
@Test
void loadSubmissionDataSchemaFromRemoteFailed() {
void loadSubmissionDataSchemaFromRemoteFailedButFromMemoryWorks() {
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.getForEntity(eq(URI.create("https://test.json")), any())).thenThrow(new RuntimeException("Something went wrong!"));
when(restTemplate.getForEntity(eq(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json")),
any())).thenThrow(new RuntimeException("Something went wrong!"));
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(restTemplate,
new SchemaResources(List.of(), List.of(), List.of(), "submission-data-test-schema"));
String schema = schemaResourceProvider.loadSubmissionDataSchema(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json"));
assertThat(schema, containsString("\"$id\": \"https://schema.fitko.de/fim/s00000096_1.0.schema.json\""));
}
@Test
void loadSubmissionDataSchemaFromRemoteAndMemoryFailed() {
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.getForEntity(eq(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json")),
any())).thenThrow(new RuntimeException("Something went wrong!"));
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(restTemplate,
new SchemaResources(List.of(), List.of(), List.of(), null));
Exception exception = assertThrows(SchemaNotFoundException.class,
() -> schemaResourceProvider.loadSubmissionDataSchema(URI.create("https://test.json")));
() -> schemaResourceProvider.loadSubmissionDataSchema(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json")));
assertThat(exception.getMessage(), equalTo("Submission data schema https://test.json is not available."));
assertThat(exception.getMessage(), equalTo("Submission data schema https://schema.fitko.de/fim/s00000096_1.0.schema.json is not available."));
}
@Test
void loadSubmissionDataSchemaFromMemory() {
SchemaResources schemaResources = new SchemaResources(List.of(), List.of(), List.of(), "submission-data-test-schema");
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(mock(RestTemplate.class), schemaResources);
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(mock(RestTemplate.class),
new SchemaResources(List.of(), List.of(), List.of(), "submission-data-test-schema"));
String schema = schemaResourceProvider.loadSubmissionDataSchema(URI.create("urn:test:submission_data_schema.json"));
......@@ -145,9 +160,8 @@ class SchemaResourceProviderTest {
@Test
void loadSubmissionDataSchemaFromMemoryFailed() {
SchemaResources schemaResources = new SchemaResources(List.of(), List.of(), List.of(), null);
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(mock(RestTemplate.class), schemaResources);
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(mock(RestTemplate.class),
new SchemaResources(List.of(), List.of(), List.of(), null));
Exception exception = assertThrows(SchemaNotFoundException.class,
() -> schemaResourceProvider.loadSubmissionDataSchema(URI.create("urn:test:submission_data_schema.json")));
......
......@@ -1194,38 +1194,20 @@ class DefaultValidationServiceTest {
}
@Test
void testValidJson() throws IOException {
// Given
SchemaProvider schemaProvider = mock(SchemaProvider.class);
when(schemaProvider.loadSubmissionDataSchema(any())).thenReturn("{}");
DefaultValidationService defaultValidationService = new DefaultValidationService(
getApplicationConfig(true, false), new HashService(), schemaProvider,
FileUtil.loadContentOfFilesInDirectory("trusted-test-root-certificates"));
final var validJson = getResource("/valid_json_data.json");
void testValidJson() {
// When
final ValidationResult validationResult = defaultValidationService.validateSubmissionDataSchema(validJson, URI.create("urn:something"));
final ValidationResult validationResult = underTest.validateJsonFormat("{\"test\":\"test\"}");
// Then
assertTrue(validationResult.isValid());
}
@Test
void testInvalidJson() throws IOException {
// Given
SchemaProvider schemaProvider = mock(SchemaProvider.class);
when(schemaProvider.loadSubmissionDataSchema(any())).thenReturn("{}");
DefaultValidationService defaultValidationService = new DefaultValidationService(
getApplicationConfig(true, false), new HashService(), schemaProvider,
FileUtil.loadContentOfFilesInDirectory("trusted-test-root-certificates"));
final var invalidJson = getResource("/invalid_json_data.json");
void testInvalidJson() {
// When
final ValidationResult validationResult = defaultValidationService.validateSubmissionDataSchema(invalidJson, URI.create("urn:something"));
final ValidationResult validationResult = underTest.validateJsonFormat("{test:\"test\"}");
// Then
assertFalse(validationResult.isValid());
......
{
"valid": false,
xstandards: ["xbau", "xsozial"]
}
\ No newline at end of file
{
"valid": true,
"xstandards": ["xbau", "xsozial"]
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment