diff --git a/core/src/main/java/dev/fitko/fitconnect/core/schema/SchemaResourceProvider.java b/core/src/main/java/dev/fitko/fitconnect/core/schema/SchemaResourceProvider.java index d8876612660325f1fd026ba9204fc76ef370fd2c..3bb86093a27a6a15acfe7a4aec315e56b85d1d64 100644 --- a/core/src/main/java/dev/fitko/fitconnect/core/schema/SchemaResourceProvider.java +++ b/core/src/main/java/dev/fitko/fitconnect/core/schema/SchemaResourceProvider.java @@ -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) { diff --git a/core/src/test/java/dev/fitko/fitconnect/core/schema/SchemaResourceProviderTest.java b/core/src/test/java/dev/fitko/fitconnect/core/schema/SchemaResourceProviderTest.java index 17033c7819dce275cdc00954b50e91dab9ebfa5e..bdb641c6f8f001b2c4e162f628e0ebe5b1916fac 100644 --- a/core/src/test/java/dev/fitko/fitconnect/core/schema/SchemaResourceProviderTest.java +++ b/core/src/test/java/dev/fitko/fitconnect/core/schema/SchemaResourceProviderTest.java @@ -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