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

928: switched order of schema loading logic, adjusted tests

parent d1361f65
No related branches found
No related tags found
No related merge requests found
......@@ -118,20 +118,20 @@ public class SchemaResourceProvider implements SchemaProvider {
@Override
public String loadSubmissionDataSchema(final URI schemaUri) throws SchemaNotFoundException {
if (schemaUri.toString().matches("http.+")) {
final String schema = submissionDataSchemas.get(schemaUri);
if (schema != null) {
return schema;
}
LOGGER.warn("Fetching schema " + schemaUri + " from local files failed, attempting to retrieve a remote version.");
if (schemaUri.getScheme().equals("https")) {
try {
return restTemplate.getForEntity(schemaUri, String.class).getBody();
} catch (Exception exception) {
LOGGER.warn("Fetching schema from " + schemaUri + " failed, falling back to pre-stored version.");
throw new SchemaNotFoundException("Submission data schema " + schemaUri + " is not available.");
}
}
final String schema = submissionDataSchemas.get(schemaUri);
if (schema == null) {
throw new SchemaNotFoundException("Submission data schema " + schemaUri + " is not available.");
}
return schema;
throw new SchemaNotFoundException("Fetching schema " + schemaUri + " from remote was skipped, since the URI does not support HTTPS.");
}
private void addSetSchema(final String schema) {
......
......@@ -17,7 +17,9 @@ import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
......@@ -102,36 +104,32 @@ class SchemaResourceProviderTest {
}
@Test
void loadSubmissionDataSchemaFromRemote() {
void loadSubmissionDataSchemaFromMemoryWorks() {
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.getForEntity(eq(URI.create("https://test.json")), any())).thenReturn(ResponseEntity.ok("schema"));
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(restTemplate,
new SchemaResources(List.of(), List.of(), List.of(), null));
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("https://test.json"));
String schema = schemaResourceProvider.loadSubmissionDataSchema(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json"));
assertThat(schema, equalTo("schema"));
assertThat(schema, containsString("\"$id\": \"https://schema.fitko.de/fim/s00000096_1.0.schema.json\""));
}
@Test
void loadSubmissionDataSchemaFromRemoteFailedButFromMemoryWorks() {
void loadSubmissionDataSchemaFromMemoryFailsButRemoteWorks() {
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!"));
when(restTemplate.getForEntity(eq(URI.create("https://test.json")), any())).thenReturn(ResponseEntity.ok("schema"));
SchemaResourceProvider schemaResourceProvider = new SchemaResourceProvider(restTemplate,
new SchemaResources(List.of(), List.of(), List.of(), "submission-data-test-schema"));
new SchemaResources(List.of(), List.of(), List.of(), null));
String schema = schemaResourceProvider.loadSubmissionDataSchema(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json"));
String schema = schemaResourceProvider.loadSubmissionDataSchema(URI.create("https://test.json"));
assertThat(schema, containsString("\"$id\": \"https://schema.fitko.de/fim/s00000096_1.0.schema.json\""));
assertThat(schema, equalTo("schema"));
}
@Test
void loadSubmissionDataSchemaFromRemoteAndMemoryFailed() {
void loadSubmissionDataSchemaFromMemoryAndRemoteFails() {
RestTemplate restTemplate = mock(RestTemplate.class);
when(restTemplate.getForEntity(eq(URI.create("https://schema.fitko.de/fim/s00000096_1.0.schema.json")),
......@@ -147,25 +145,14 @@ class SchemaResourceProviderTest {
}
@Test
void loadSubmissionDataSchemaFromMemory() {
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"));
assertThat(schema, containsString("\"$id\": \"urn:test:submission_data_schema.json\""));
}
@Test
void loadSubmissionDataSchemaFromMemoryFailed() {
void loadSubmissionDataSchemaFromRemoteIsRejectedBecauseNoHttps() {
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")));
() -> schemaResourceProvider.loadSubmissionDataSchema(URI.create("http://test.json")));
assertThat(exception.getMessage(), equalTo("Submission data schema urn:test:submission_data_schema.json is not available."));
assertThat(exception.getMessage(), equalTo("Fetching schema http://test.json from remote was skipped, since the URI does not support HTTPS."));
}
}
\ 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