From 8ebb0f297f0dcf5745a81842113cd30febd3b89b Mon Sep 17 00:00:00 2001 From: Jonas Bulcke <127748878+jobulcke@users.noreply.github.com> Date: Tue, 19 Nov 2024 18:31:33 +0100 Subject: [PATCH] chore: make version header to all endpoints accessible (#1422) --- .../AdminVersionHeaderControllerAdvice.java | 21 ---------- .../AdminEventStreamsRestControllerTest.java | 30 +++++++++----- .../versioning/VersionHeaderFilter.java | 23 +++++++++++ .../versioning/VersionHeaderFilterConfig.java | 18 ++++++++ .../src/main/java/module-info.java | 1 + .../VersionHeaderControllerAdvice.java | 21 ---------- .../EventStreamControllerTest.java | 5 +-- .../rest/treenode/TreeNodeControllerTest.java | 4 +- .../PostgresBucketisationIntegrationTest.java | 3 +- .../ingest/PostgresIngestIntegrationTest.java | 3 ++ .../pagination/postgres/PaginationSteps.java | 3 +- .../resources/application-postgres-test.yml | 8 +++- .../rest/MemberIngestControllerTest.java | 41 ++++++++++++++----- 13 files changed, 109 insertions(+), 72 deletions(-) delete mode 100644 ldes-server-admin/ldes-server-admin-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/versioning/AdminVersionHeaderControllerAdvice.java create mode 100644 ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilter.java create mode 100644 ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilterConfig.java delete mode 100644 ldes-server-fetch/ldes-server-fetch-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/versioning/VersionHeaderControllerAdvice.java diff --git a/ldes-server-admin/ldes-server-admin-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/versioning/AdminVersionHeaderControllerAdvice.java b/ldes-server-admin/ldes-server-admin-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/versioning/AdminVersionHeaderControllerAdvice.java deleted file mode 100644 index c695dfc46..000000000 --- a/ldes-server-admin/ldes-server-admin-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/versioning/AdminVersionHeaderControllerAdvice.java +++ /dev/null @@ -1,21 +0,0 @@ -package be.vlaanderen.informatievlaanderen.ldes.server.admin.rest.versioning; - -import jakarta.servlet.http.HttpServletResponse; -import org.springframework.boot.info.BuildProperties; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ModelAttribute; - -@ControllerAdvice -public class AdminVersionHeaderControllerAdvice { - private static final String APP_VERSION_HEADER_KEY = "X-App-Version"; - private final String appVersion; - - public AdminVersionHeaderControllerAdvice(BuildProperties buildProperties) { - this.appVersion = buildProperties.getVersion(); - } - - @ModelAttribute - public void addVersionHeader(HttpServletResponse response) { - response.setHeader(APP_VERSION_HEADER_KEY, appVersion); - } -} diff --git a/ldes-server-admin/ldes-server-admin-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/controllers/AdminEventStreamsRestControllerTest.java b/ldes-server-admin/ldes-server-admin-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/controllers/AdminEventStreamsRestControllerTest.java index b1ea3d948..0c3afa72a 100644 --- a/ldes-server-admin/ldes-server-admin-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/controllers/AdminEventStreamsRestControllerTest.java +++ b/ldes-server-admin/ldes-server-admin-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/admin/rest/controllers/AdminEventStreamsRestControllerTest.java @@ -6,7 +6,6 @@ import be.vlaanderen.informatievlaanderen.ldes.server.admin.rest.converters.EventStreamHttpConverter; import be.vlaanderen.informatievlaanderen.ldes.server.admin.rest.converters.EventStreamListHttpConverter; import be.vlaanderen.informatievlaanderen.ldes.server.admin.rest.exceptionhandling.AdminRestResponseEntityExceptionHandler; -import be.vlaanderen.informatievlaanderen.ldes.server.admin.rest.versioning.AdminVersionHeaderControllerAdvice; import be.vlaanderen.informatievlaanderen.ldes.server.admin.spi.*; import be.vlaanderen.informatievlaanderen.ldes.server.domain.converter.HttpModelConverter; import be.vlaanderen.informatievlaanderen.ldes.server.domain.converter.PrefixAdderImpl; @@ -18,6 +17,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.ViewSpecification; import be.vlaanderen.informatievlaanderen.ldes.server.domain.rest.HostNamePrefixConstructorConfig; import be.vlaanderen.informatievlaanderen.ldes.server.domain.rest.RelativeUriPrefixConstructor; +import be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning.VersionHeaderFilterConfig; import org.apache.jena.rdf.model.Model; import org.apache.jena.riot.RDFDataMgr; import org.junit.jupiter.api.BeforeEach; @@ -28,8 +28,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.info.BuildProperties; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; @@ -45,6 +46,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Properties; import java.util.stream.Collectors; import static org.apache.jena.riot.WebContent.contentTypeNQuads; @@ -53,8 +55,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest @ActiveProfiles({"test", "rest"}) @@ -63,17 +64,14 @@ EventStreamWriter.class, EventStreamReader.class, KafkaSourceReader.class, ViewSpecificationConverter.class, PrefixAdderImpl.class, ValidatorsConfig.class, AdminRestResponseEntityExceptionHandler.class, RetentionModelExtractor.class, CharsetEncodingConfig.class, - FragmentationConfigExtractor.class, HostNamePrefixConstructorConfig.class, RelativeUriPrefixConstructor.class, - RdfModelConverter.class, AdminVersionHeaderControllerAdvice.class}) -@Import(BuildProperties.class) + FragmentationConfigExtractor.class, HostNamePrefixConstructorConfig.class, RelativeUriPrefixConstructor.class, RdfModelConverter.class, VersionHeaderFilterConfig.class}) +@Import(AdminEventStreamsRestControllerTest.VersionConfig.class) class AdminEventStreamsRestControllerTest { private static final String COLLECTION = "name1"; public static final String TIMESTAMP_PATH = "http://purl.org/dc/terms/created"; public static final String VERSION_OF_PATH = "http://purl.org/dc/terms/isVersionOf"; @MockBean private EventStreamService eventStreamService; - @SpyBean - private AdminVersionHeaderControllerAdvice adminVersionHeaderControllerAdvice; @Autowired private MockMvc mockMvc; @@ -128,12 +126,12 @@ void when_StreamsPresent_then_StreamsAreReturned() throws Exception { mockMvc.perform(get("/admin/api/v1/eventstreams").accept(contentTypeNQuads)) .andExpect(status().isOk()) + .andExpect(header().exists("X-App-Version")) .andExpect(content().encoding(StandardCharsets.UTF_8)) .andExpect(content().contentTypeCompatibleWith(contentTypeNQuads)) .andExpect(IsIsomorphic.with(expectedEventStreamsModel)); verify(eventStreamService).retrieveAllEventStreams(); - verify(adminVersionHeaderControllerAdvice).addVersionHeader(any()); } } @@ -154,12 +152,12 @@ void when_StreamPresent_Then_StreamIsReturned() throws Exception { mockMvc.perform(get("/admin/api/v1/eventstreams/" + COLLECTION).accept(contentTypeNQuads)) .andExpect(status().isOk()) + .andExpect(header().exists("X-App-Version")) .andExpect(content().encoding(StandardCharsets.UTF_8)) .andExpect(content().contentTypeCompatibleWith(contentTypeNQuads)) .andExpect(IsIsomorphic.with(model)); verify(eventStreamService).retrieveEventStream(COLLECTION); - verify(adminVersionHeaderControllerAdvice).addVersionHeader(any()); } @Test @@ -348,4 +346,14 @@ private String readDataFromFile(String fileName) return Files.lines(path).collect(Collectors.joining()); } + @TestConfiguration + static class VersionConfig { + @Bean + public BuildProperties buildProperties() { + final Properties properties = new Properties(); + properties.setProperty("version", "1.0.0"); + return new BuildProperties(properties); + } + } + } diff --git a/ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilter.java b/ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilter.java new file mode 100644 index 000000000..5c2c55302 --- /dev/null +++ b/ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilter.java @@ -0,0 +1,23 @@ +package be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning; + +import jakarta.servlet.*; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; + +public class VersionHeaderFilter implements Filter { + private static final String APP_VERSION_HEADER_KEY = "X-App-Version"; + private final String appVersion; + + public VersionHeaderFilter(String appVersion) { + this.appVersion = appVersion; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + if (response instanceof HttpServletResponse httpResponse) { + httpResponse.setHeader(APP_VERSION_HEADER_KEY, appVersion); + } + chain.doFilter(request, response); + } +} diff --git a/ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilterConfig.java b/ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilterConfig.java new file mode 100644 index 000000000..0bc6c2282 --- /dev/null +++ b/ldes-server-domain/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/domain/versioning/VersionHeaderFilterConfig.java @@ -0,0 +1,18 @@ +package be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning; + +import org.springframework.boot.info.BuildProperties; +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class VersionHeaderFilterConfig { + + @Bean + public FilterRegistrationBean registerVersionHeaderFilter(BuildProperties buildProperties) { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>(); + filterRegistrationBean.setFilter(new VersionHeaderFilter(buildProperties.getVersion())); + filterRegistrationBean.addUrlPatterns("/*"); + return filterRegistrationBean; + } +} diff --git a/ldes-server-domain/src/main/java/module-info.java b/ldes-server-domain/src/main/java/module-info.java index 8101c51f5..53c87c5af 100644 --- a/ldes-server-domain/src/main/java/module-info.java +++ b/ldes-server-domain/src/main/java/module-info.java @@ -6,6 +6,7 @@ exports be.vlaanderen.informatievlaanderen.ldes.server.domain.model; exports be.vlaanderen.informatievlaanderen.ldes.server.domain.rest; exports be.vlaanderen.informatievlaanderen.ldes.server.domain.encodig; + exports be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning; // Events exports be.vlaanderen.informatievlaanderen.ldes.server.domain.events.retention; diff --git a/ldes-server-fetch/ldes-server-fetch-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/versioning/VersionHeaderControllerAdvice.java b/ldes-server-fetch/ldes-server-fetch-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/versioning/VersionHeaderControllerAdvice.java deleted file mode 100644 index 28aa26f15..000000000 --- a/ldes-server-fetch/ldes-server-fetch-rest/src/main/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/versioning/VersionHeaderControllerAdvice.java +++ /dev/null @@ -1,21 +0,0 @@ -package be.vlaanderen.informatievlaanderen.ldes.server.rest.versioning; - -import jakarta.servlet.http.HttpServletResponse; -import org.springframework.boot.info.BuildProperties; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.ModelAttribute; - -@ControllerAdvice -public class VersionHeaderControllerAdvice { - private static final String APP_VERSION_HEADER_KEY = "X-App-Version"; - private final String appVersion; - - public VersionHeaderControllerAdvice(BuildProperties buildProperties) { - this.appVersion = buildProperties.getVersion(); - } - - @ModelAttribute - public void addVersionHeader(HttpServletResponse response) { - response.setHeader(APP_VERSION_HEADER_KEY, appVersion); - } -} diff --git a/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/eventstream/EventStreamControllerTest.java b/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/eventstream/EventStreamControllerTest.java index 42c7a50d4..76fb10395 100644 --- a/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/eventstream/EventStreamControllerTest.java +++ b/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/eventstream/EventStreamControllerTest.java @@ -13,7 +13,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.rest.config.RestConfig; import be.vlaanderen.informatievlaanderen.ldes.server.rest.eventstream.converters.EventStreamResponseHttpConverter; import be.vlaanderen.informatievlaanderen.ldes.server.rest.exceptionhandling.RestResponseEntityExceptionHandler; -import be.vlaanderen.informatievlaanderen.ldes.server.rest.versioning.VersionHeaderControllerAdvice; +import be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning.VersionHeaderFilterConfig; import org.apache.jena.rdf.model.*; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFParser; @@ -67,8 +67,7 @@ RestResponseEntityExceptionHandler.class, EventStreamWriter.class, EventStreamReader.class, KafkaSourceReader.class, ViewSpecificationConverter.class, PrefixAdderImpl.class, EventStreamResponseHttpConverter.class, RetentionModelExtractor.class, HttpModelConverter.class, FragmentationConfigExtractor.class, - HostNamePrefixConstructorConfig.class, RelativeUriPrefixConstructor.class, - RdfModelConverter.class, VersionHeaderControllerAdvice.class + HostNamePrefixConstructorConfig.class, RelativeUriPrefixConstructor.class, RdfModelConverter.class, VersionHeaderFilterConfig.class }) class EventStreamControllerTest { private static final String COLLECTION = "mobility-hindrances"; diff --git a/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/TreeNodeControllerTest.java b/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/TreeNodeControllerTest.java index f0d0d989a..da79174b2 100644 --- a/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/TreeNodeControllerTest.java +++ b/ldes-server-fetch/ldes-server-fetch-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/rest/treenode/TreeNodeControllerTest.java @@ -24,7 +24,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.rest.exceptionhandling.RestResponseEntityExceptionHandler; import be.vlaanderen.informatievlaanderen.ldes.server.rest.treenode.config.TreeViewWebConfig; import be.vlaanderen.informatievlaanderen.ldes.server.rest.treenode.services.*; -import be.vlaanderen.informatievlaanderen.ldes.server.rest.versioning.VersionHeaderControllerAdvice; +import be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning.VersionHeaderFilterConfig; import org.apache.jena.rdf.model.*; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFParser; @@ -79,7 +79,7 @@ RestConfig.class, TreeViewWebConfig.class, RestResponseEntityExceptionHandler.class, HostNamePrefixConstructorConfig.class, RelativeUriPrefixConstructor.class, RdfModelConverter.class, TreeNodeStreamConverterImpl.class, PrefixAdderImpl.class, - TreeNodeStatementCreatorImpl.class, CharsetEncodingConfig.class, VersionHeaderControllerAdvice.class, + TreeNodeStatementCreatorImpl.class, CharsetEncodingConfig.class, VersionHeaderFilterConfig.class, }) class TreeNodeControllerTest { private static final String COLLECTION_NAME = "ldes-1"; diff --git a/ldes-server-infra-postgres/postgres-fragmentation-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/postgres/PostgresBucketisationIntegrationTest.java b/ldes-server-infra-postgres/postgres-fragmentation-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/postgres/PostgresBucketisationIntegrationTest.java index ef86a1855..08406c2ba 100644 --- a/ldes-server-infra-postgres/postgres-fragmentation-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/postgres/PostgresBucketisationIntegrationTest.java +++ b/ldes-server-infra-postgres/postgres-fragmentation-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/fragmentation/postgres/PostgresBucketisationIntegrationTest.java @@ -11,6 +11,7 @@ import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.info.BuildProperties; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; @@ -39,7 +40,7 @@ "be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.postgres" }) @ContextConfiguration(classes = {BucketisedMemberItemWriterConfig.class}) -@Import(PostgresBucketisationIntegrationTest.EventStreamControllerTestConfiguration.class) +@Import({PostgresBucketisationIntegrationTest.EventStreamControllerTestConfiguration.class, BuildProperties.class}) @SuppressWarnings("java:S2187") public class PostgresBucketisationIntegrationTest { @MockBean diff --git a/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java b/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java index 30d28f9f5..6af775cd3 100644 --- a/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java +++ b/ldes-server-infra-postgres/postgres-ingest-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/PostgresIngestIntegrationTest.java @@ -10,9 +10,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.info.BuildProperties; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; @@ -31,6 +33,7 @@ @EnableJpaRepositories(basePackageClasses = {MemberEntityRepository.class, EventStreamEntityRepository.class}) @Sql(value = {"init-collections.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @Sql(statements = "DELETE FROM collections;", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) +@Import(BuildProperties.class) @SuppressWarnings("java:S2187") public class PostgresIngestIntegrationTest { diff --git a/ldes-server-infra-postgres/postgres-pagination-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/pagination/postgres/PaginationSteps.java b/ldes-server-infra-postgres/postgres-pagination-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/pagination/postgres/PaginationSteps.java index ed78933c2..19a6f5fef 100644 --- a/ldes-server-infra-postgres/postgres-pagination-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/pagination/postgres/PaginationSteps.java +++ b/ldes-server-infra-postgres/postgres-pagination-repository/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/pagination/postgres/PaginationSteps.java @@ -95,8 +95,7 @@ private void saveMembers(List memberIds) { final List batchArgs = memberIds.stream() .map(member -> new Object[]{ "http://example.com/es/%d".formatted(member), collectionId, eventStream, - LocalDateTime.now(), 0, new byte[]{}, - "%s/%s/%d".formatted(eventStream, eventStream, member) + LocalDateTime.now(), 0, new byte[]{} }) .toList(); diff --git a/ldes-server-infra-postgres/postgres-pagination-repository/src/test/resources/application-postgres-test.yml b/ldes-server-infra-postgres/postgres-pagination-repository/src/test/resources/application-postgres-test.yml index b8c8bf725..44347ce39 100644 --- a/ldes-server-infra-postgres/postgres-pagination-repository/src/test/resources/application-postgres-test.yml +++ b/ldes-server-infra-postgres/postgres-pagination-repository/src/test/resources/application-postgres-test.yml @@ -1,3 +1,9 @@ spring: liquibase: - change-log: classpath:/db/changelog/master.xml \ No newline at end of file + change-log: classpath:/db/changelog/master.xml +zonky: + test: + database: + postgres: + docker: + image: postgres:14-alpine \ No newline at end of file diff --git a/ldes-server-ingest/ldes-server-ingest-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/rest/MemberIngestControllerTest.java b/ldes-server-ingest/ldes-server-ingest-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/rest/MemberIngestControllerTest.java index 11bdcbf75..a64531b0c 100644 --- a/ldes-server-ingest/ldes-server-ingest-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/rest/MemberIngestControllerTest.java +++ b/ldes-server-ingest/ldes-server-ingest-rest/src/test/java/be/vlaanderen/informatievlaanderen/ldes/server/ingest/rest/MemberIngestControllerTest.java @@ -6,6 +6,7 @@ import be.vlaanderen.informatievlaanderen.ldes.server.domain.exceptions.ShaclValidationException; import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.EventStream; import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.VersionCreationProperties; +import be.vlaanderen.informatievlaanderen.ldes.server.domain.versioning.VersionHeaderFilterConfig; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.MemberIngester; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.exceptions.MemberSubjectNotFoundException; import be.vlaanderen.informatievlaanderen.ldes.server.ingest.rest.converters.IngestedModelConverter; @@ -27,9 +28,12 @@ import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsSource; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.info.BuildProperties; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.annotation.Bean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -41,22 +45,23 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.util.Objects; +import java.util.Properties; import java.util.stream.Stream; import static org.hamcrest.core.StringContains.containsString; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @WebMvcTest @ActiveProfiles("test") @ContextConfiguration(classes = {IngestedModelConverter.class, MemberIngestController.class, IngestionRestResponseEntityExceptionHandler.class, RdfModelConverter.class, MemberIngestValidator.class, - BlankNodesValidator.class, PathsValidator.class}) + BlankNodesValidator.class, PathsValidator.class, VersionHeaderFilterConfig.class, + MemberIngestControllerTest.VersionConfig.class}) class MemberIngestControllerTest { - + private static final String VERSION = "4.0.4-SNAPSHOT"; @Autowired private MockMvc mockMvc; @@ -81,7 +86,8 @@ void when_POSTRequestIsPerformed_LDesMemberIsSaved(String contentType, Lang rdfF byte[] ldesMemberBytes = readLdesMemberDataFromFile("example-ldes-member.nq", rdfFormat); mockMvc.perform(post("/mobility-hindrances").contentType(contentType).content(ldesMemberBytes)) - .andExpect(status().isOk()); + .andExpect(status().isOk()) + .andExpect(header().exists("X-App-Version")); verify(memberIngester, times(1)).ingest(anyString(), any(Model.class)); } @@ -96,7 +102,8 @@ void when_POSTRequestIsPerformedWithMultipleNamedNodes_ThrowException() throws E .contentType("application/n-quads") .content(ldesMemberBytes)) .andExpect(status().isBadRequest()) - .andExpect(content().string(containsString("Only 1 member is allowed per request on collection with version creation disabled"))); + .andExpect(header().exists("X-App-Version")) + .andExpect(content().string(containsString("Only 1 member is allowed per request on collection with version creation disabled"))); } @Test @@ -106,6 +113,7 @@ void when_POSTRequestIsPerformedWithoutTimestampPath_ThrowException() throws Exc mockMvc.perform(post("/mobility-hindrances").contentType("application/n-quads").content(ldesMemberBytes)) .andExpect(status().isBadRequest()) + .andExpect(header().exists("X-App-Version")) .andExpect(content().string(containsString("Member must have exactly 1 statement with timestamp path: http://www.w3.org/ns/prov#generatedAtTime"))); verifyNoInteractions(memberIngester); } @@ -120,7 +128,8 @@ void when_POSTRequestIsPerformed_WithoutVersionOf_ThenTheRequestFails() throws E mockMvc.perform(post("/mobility-hindrances").contentType("application/n-quads").content(ldesMemberBytes)) .andExpect(status().isBadRequest()) - .andExpect(content().string(containsString("Member must have exactly 1 statement with versionOf path: http://purl.org/dc/terms/isVersionOf"))); + .andExpect(header().exists("X-App-Version")) + .andExpect(content().string(containsString("Member must have exactly 1 statement with versionOf path: http://purl.org/dc/terms/isVersionOf"))); } @Test @@ -135,7 +144,8 @@ void when_POSTRequestIsPerformedUsingAnotherCollectionName_ResponseIs404() mockMvc.perform(post("/another-collection-name") .contentType("application/n-quads") .content(ldesMemberBytes)) - .andExpect(status().isNotFound()); + .andExpect(status().isNotFound()) + .andExpect(header().exists("X-App-Version")); } @Test @@ -143,7 +153,8 @@ void when_memberConformToShapeIsIngested_then_status200IsReturned() throws Excep String modelString = readModelStringFromFile("menu-items/example-data-old.ttl"); mockMvc.perform(post("/restaurant").contentType("text/turtle").content(modelString)) - .andExpect(status().isOk()); + .andExpect(status().isOk()) + .andExpect(header().exists("X-App-Version")); verify(memberIngester).ingest(anyString(), any(Model.class)); } @@ -154,7 +165,8 @@ void whenIngestValidationExceptionIsThrown_thenStatus400IsReturned() throws Exce doThrow(new ShaclValidationException("", ModelFactory.createDefaultModel())).when(memberIngester).ingest(anyString(), any(Model.class)); mockMvc.perform(post("/restaurant").contentType("text/turtle").content(modelString)) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()) + .andExpect(header().exists("X-App-Version")); } private byte[] readLdesMemberDataFromFile(String fileName, Lang rdfFormat) { @@ -196,4 +208,13 @@ public Stream provideArguments(ExtensionContext context) { } } + @TestConfiguration + static class VersionConfig { + @Bean + public BuildProperties buildProperties() { + final Properties properties = new Properties(); + properties.setProperty("version", VERSION); + return new BuildProperties(properties); + } + } }