Skip to content

Commit

Permalink
chore: make version header to all endpoints accessible (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobulcke authored and Yalz committed Dec 4, 2024
1 parent 8ed5bd3 commit 8ebb0f2
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 72 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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"})
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<VersionHeaderFilter> registerVersionHeaderFilter(BuildProperties buildProperties) {
FilterRegistrationBean<VersionHeaderFilter> filterRegistrationBean = new FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new VersionHeaderFilter(buildProperties.getVersion()));
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
1 change: 1 addition & 0 deletions ldes-server-domain/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ private void saveMembers(List<Long> memberIds) {
final List<Object[]> 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();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
spring:
liquibase:
change-log: classpath:/db/changelog/master.xml
change-log: classpath:/db/changelog/master.xml
zonky:
test:
database:
postgres:
docker:
image: postgres:14-alpine
Loading

0 comments on commit 8ebb0f2

Please sign in to comment.