From eb4c6029647ea0ed891f8b7650bc8104d5820d92 Mon Sep 17 00:00:00 2001 From: Ewann ROUX Date: Wed, 12 Aug 2026 17:01:56 +0200 Subject: [PATCH] chore: migrate to java 25 --- NOTICE | 51 +++++++++++------ etc/license-mappings.xml | 6 ++ pom.xml | 49 ++++++++++++++-- .../ApiMediacentreApplication.java | 2 + .../config/AppProperties.java | 6 +- .../config/SecurityConfiguration.java | 44 +++++++------- .../config/beans/CacheProperties.java | 1 - .../config/beans/SoffitProperties.java | 13 +++++ ...leParserServiceJacksonAnnotationsImpl.java | 12 ++-- .../ApiRessourcesDiffusablesController.java | 4 +- .../web/rest/HealthCheckController.java | 4 +- .../config/TestSecurityConfiguration.java | 57 +++++++++++++++++++ .../cache/CacheServiceJsonImplTest.java | 16 +++--- .../filter/RessourceDiffusableFilterTest.java | 15 +++-- .../test/TestUtil.java | 2 - ...piRessourcesDiffusablesControllerTest.java | 42 +++++++++++--- .../web/rest/HealthCheckTest.java | 18 +++--- src/test/resources/application-test.yml | 3 + 18 files changed, 251 insertions(+), 94 deletions(-) create mode 100644 src/test/java/fr/recia/ressourcesdiffusablesapi/config/TestSecurityConfiguration.java create mode 100644 src/test/resources/application-test.yml diff --git a/NOTICE b/NOTICE index 9f73a20..0df24ae 100644 --- a/NOTICE +++ b/NOTICE @@ -16,24 +16,22 @@ specific language governing permissions and limitations under the License. This project includes: - Apache Commons Lang under Apache License, Version 2.0 - Apache Log4j API under Apache License, Version 2.0 - Apache Log4j to SLF4J Adapter under Apache License, Version 2.0 + Apache Commons Lang under Apache-2.0 + Apache Commons Logging under Apache-2.0 + Apache Log4j API under Apache-2.0 asm under BSD-3-Clause ASM based accessors helper used by json-smart under The Apache Software License, Version 2.0 - AssertJ fluent assertions under Apache License, Version 2.0 + AssertJ Core under Apache-2.0 + Awaitility under Apache 2.0 Bouncy Castle Provider under Bouncy Castle Licence Byte Buddy (without dependencies) under Apache License, Version 2.0 Byte Buddy agent under Apache License, Version 2.0 - Hamcrest under BSD License 3 + Hamcrest under BSD-3-Clause hazelcast under The Apache Software License, Version 2.0 - Jackson datatype: jdk8 under The Apache Software License, Version 2.0 - Jackson datatype: JSR310 under The Apache Software License, Version 2.0 Jackson-annotations under The Apache Software License, Version 2.0 Jackson-core under The Apache Software License, Version 2.0 jackson-databind under The Apache Software License, Version 2.0 - Jackson-module-parameter-names under The Apache Software License, Version 2.0 - Jakarta Activation API jar under EDL 1.0 + Jakarta Activation API under EDL 1.0 Jakarta Annotations API under EPL 2.0 or GPL2 w/ CPE Jakarta XML Binding API under Eclipse Distribution License - v 1.0 JASYPT: Java Simplified Encryption under The Apache Software License, Version 2.0 @@ -43,31 +41,35 @@ This project includes: JJWT :: Impl under Apache License, Version 2.0 JSON library from Android SDK under Apache License 2.0 JSON Small and Fast Parser under The Apache Software License, Version 2.0 + json-path under The Apache Software License, Version 2.0 JSONassert under The Apache Software License, Version 2.0 + JSpecify annotations under The Apache License, Version 2.0 JSR107 API and SPI under Apache License, Version 2.0 - JUL to SLF4J bridge under MIT License + JUL to SLF4J bridge under MIT JUnit Jupiter (Aggregator) under Eclipse Public License v2.0 JUnit Jupiter API under Eclipse Public License v2.0 JUnit Jupiter Engine under Eclipse Public License v2.0 JUnit Jupiter Params under Eclipse Public License v2.0 JUnit Platform Commons under Eclipse Public License v2.0 JUnit Platform Engine API under Eclipse Public License v2.0 - Logback Classic Module under Eclipse Public License - v 1.0 or GNU Lesser General Public License - Logback Core Module under Eclipse Public License - v 1.0 or GNU Lesser General Public License - mockito-core under The MIT License - mockito-junit-jupiter under The MIT License + Log4j API to SLF4J Adapter under Apache-2.0 + Logback Classic Module under EPL-2.0 or LGPL-2.1-only + Logback Core Module under EPL-2.0 or LGPL-2.1-only + micrometer-commons under The Apache Software License, Version 2.0 + micrometer-observation under The Apache Software License, Version 2.0 + mockito-core under MIT + mockito-junit-jupiter under MIT Objenesis under Apache License, Version 2.0 org.apiguardian:apiguardian-api under The Apache License, Version 2.0 org.opentest4j:opentest4j under The Apache License, Version 2.0 org.xmlunit:xmlunit-core under The Apache Software License, Version 2.0 - project ':json-path' under The Apache Software License, Version 2.0 Project Lombok under The MIT License Ressources Diffusables API under Apache License, Version 2.0 - SLF4J API Module under MIT License + SLF4J API Module under MIT SnakeYAML under Apache License, Version 2.0 + soffit-java-client under Apache License, Version 2.0 Spring AOP under Apache License, Version 2.0 Spring Beans under Apache License, Version 2.0 - Spring Commons Logging Bridge under Apache License, Version 2.0 Spring Context under Apache License, Version 2.0 Spring Core under Apache License, Version 2.0 Spring Expression Language (SpEL) under Apache License, Version 2.0 @@ -77,15 +79,28 @@ This project includes: spring-boot under Apache License, Version 2.0 spring-boot-autoconfigure under Apache License, Version 2.0 spring-boot-configuration-processor under Apache License, Version 2.0 + spring-boot-http-converter under Apache License, Version 2.0 + spring-boot-jackson under Apache License, Version 2.0 + spring-boot-resttestclient under Apache License, Version 2.0 + spring-boot-security under Apache License, Version 2.0 + spring-boot-servlet under Apache License, Version 2.0 spring-boot-starter under Apache License, Version 2.0 - spring-boot-starter-json under Apache License, Version 2.0 + spring-boot-starter-jackson under Apache License, Version 2.0 + spring-boot-starter-jackson-test under Apache License, Version 2.0 spring-boot-starter-logging under Apache License, Version 2.0 spring-boot-starter-security under Apache License, Version 2.0 spring-boot-starter-test under Apache License, Version 2.0 spring-boot-starter-tomcat under Apache License, Version 2.0 + spring-boot-starter-tomcat-runtime under Apache License, Version 2.0 spring-boot-starter-web under Apache License, Version 2.0 + spring-boot-starter-webmvc under Apache License, Version 2.0 + spring-boot-starter-webmvc-test under Apache License, Version 2.0 spring-boot-test under Apache License, Version 2.0 spring-boot-test-autoconfigure under Apache License, Version 2.0 + spring-boot-tomcat under Apache License, Version 2.0 + spring-boot-web-server under Apache License, Version 2.0 + spring-boot-webmvc under Apache License, Version 2.0 + spring-boot-webmvc-test under Apache License, Version 2.0 spring-security-config under Apache License, Version 2.0 spring-security-core under Apache License, Version 2.0 spring-security-crypto under Apache License, Version 2.0 diff --git a/etc/license-mappings.xml b/etc/license-mappings.xml index 1380775..fbe3fe9 100644 --- a/etc/license-mappings.xml +++ b/etc/license-mappings.xml @@ -2,4 +2,10 @@ + + fr.recia.notifications + soffit-java-client + 0.0.2 + Apache License, Version 2.0 + \ No newline at end of file diff --git a/pom.xml b/pom.xml index f8ea239..533225a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.boot spring-boot-starter-parent - 2.7.18 + 4.1.0 @@ -18,8 +18,8 @@ API des ressources diffusables - 11 - 11 + 25 + 1.18.42 @@ -35,6 +35,7 @@ org.projectlombok lombok + ${lombok.version} true @@ -71,12 +72,19 @@ - jakarta.xml.bind - jakarta.xml.bind-api - 4.0.1 + fr.recia.notifications + soffit-java-client + 0.0.2 + + + org.springframework.boot + spring-boot-starter-webmvc-test + test + + org.springframework.boot @@ -86,6 +94,20 @@ + + + recia + https://nexus.recia.dev/repository/public/ + + true + always + + + false + + + + @@ -138,6 +160,21 @@ true + + org.apache.maven.plugins + maven-compiler-plugin + 3.14.0 + + 25 + + + org.projectlombok + lombok + ${lombok.version} + + + + diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/ApiMediacentreApplication.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/ApiMediacentreApplication.java index 7156058..1e242d4 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/ApiMediacentreApplication.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/ApiMediacentreApplication.java @@ -16,8 +16,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; @SpringBootApplication +@ConfigurationPropertiesScan public class ApiMediacentreApplication { public static void main(String[] args) { SpringApplication.run(ApiMediacentreApplication.class, args); diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/AppProperties.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/AppProperties.java index b8a6afa..609fe73 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/AppProperties.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/AppProperties.java @@ -14,7 +14,7 @@ */ package fr.recia.ressourcesdiffusablesapi.config; -import com.fasterxml.jackson.core.JsonProcessingException; +import com.hazelcast.shaded.com.fasterxml.jackson.core.JsonProcessingException; import fr.recia.ressourcesdiffusablesapi.config.beans.CacheProperties; import fr.recia.ressourcesdiffusablesapi.config.beans.CorsProperties; import fr.recia.ressourcesdiffusablesapi.config.beans.GARProperties; @@ -40,7 +40,7 @@ public class AppProperties { private CorsProperties cors = new CorsProperties(); private GARProperties gar = new GARProperties(); - private SoffitProperties soffit = new SoffitProperties(); +// private SoffitProperties soffit = new SoffitProperties(); private NoGARProperties noGar = new NoGARProperties(); private CacheProperties cache = new CacheProperties(); @@ -55,7 +55,7 @@ public String toString() { cors + ",\n" + gar + ",\n" + noGar + ",\n" + - soffit + ",\n" + +// soffit + ",\n" + cache + ",\n" + "\n}"; } diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/SecurityConfiguration.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/SecurityConfiguration.java index deeaf8c..6c69abe 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/SecurityConfiguration.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/SecurityConfiguration.java @@ -14,56 +14,56 @@ */ package fr.recia.ressourcesdiffusablesapi.config; +import fr.recia.notifications.soffit_java_client.SoffitJwtAuthenticationFilter; +import fr.recia.notifications.soffit_java_client.SoffitJwtValidator; +import fr.recia.ressourcesdiffusablesapi.config.beans.SoffitProperties; import lombok.extern.slf4j.Slf4j; import org.apereo.portal.soffit.security.SoffitApiAuthenticationManager; -import org.apereo.portal.soffit.security.SoffitApiPreAuthenticatedProcessingFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer; import org.springframework.security.web.SecurityFilterChain; -import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter; -import org.springframework.security.web.util.matcher.AntPathRequestMatcher; -import org.springframework.security.web.util.matcher.NegatedRequestMatcher; -import org.springframework.security.web.util.matcher.RequestMatcher; +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; + @Slf4j @Configuration +@Profile("!test") @EnableWebSecurity public class SecurityConfiguration { private final AppProperties appProperties; - public SecurityConfiguration(AppProperties appProperties) { + private final SoffitProperties jwtProperties; + + public SecurityConfiguration(AppProperties appProperties, SoffitProperties jwtProperties) { this.appProperties = appProperties; + this.jwtProperties = jwtProperties; } - @Bean - public WebSecurityCustomizer webSecurityCustomizer() { - final RequestMatcher pathMatcher = new AntPathRequestMatcher("/api/**"); - final RequestMatcher inverseMatcher = new NegatedRequestMatcher(pathMatcher); - return web -> web.ignoring().requestMatchers(inverseMatcher); + @Bean + SoffitJwtValidator soffitJwtValidator() { + return new SoffitJwtValidator(jwtProperties.getJwtSignatureKey()); } @Bean - public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { - final AbstractPreAuthenticatedProcessingFilter filter = new SoffitApiPreAuthenticatedProcessingFilter( - appProperties.getSoffit().getJwtSignatureKey() - ); - - filter.setAuthenticationManager(authenticationManager()); + SoffitJwtAuthenticationFilter soffitJwtAuthenticationFilter(SoffitJwtValidator validator) { + return new SoffitJwtAuthenticationFilter(validator); + } - http.addFilter(filter); + @Bean + SecurityFilterChain securityFilterChain(HttpSecurity http, SoffitJwtAuthenticationFilter filter) { http.authorizeHttpRequests(authz -> authz - .antMatchers("/health-check").permitAll() - .antMatchers(HttpMethod.GET, "/api/**").authenticated() + .requestMatchers("/health-check").permitAll() + .requestMatchers(HttpMethod.GET, "/api/**").authenticated() .anyRequest().denyAll() ); - http.sessionManagement().sessionFixation().newSession(); + http.addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class); return http.build(); } diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/CacheProperties.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/CacheProperties.java index a181cc9..f889d4d 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/CacheProperties.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/CacheProperties.java @@ -15,7 +15,6 @@ * limitations under the License. */ -import com.fasterxml.jackson.databind.util.ISO8601Utils; import fr.recia.ressourcesdiffusablesapi.utils.Utils; import lombok.Data; import lombok.extern.slf4j.Slf4j; diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/SoffitProperties.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/SoffitProperties.java index 23d17ce..7089025 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/SoffitProperties.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/config/beans/SoffitProperties.java @@ -15,12 +15,25 @@ package fr.recia.ressourcesdiffusablesapi.config.beans; import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import jakarta.annotation.PostConstruct; @Data +@Slf4j +@ConfigurationProperties(prefix = "app.soffit", ignoreUnknownFields = true) public class SoffitProperties { private String jwtSignatureKey; + + @PostConstruct + void init(){ + log.info(this.toString()); + System.out.println(this.toString()); + } + @Override public String toString() { return "\"SoffitProperties\": {" + diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/service/parser/impl/RessourceDiffusableParserServiceJacksonAnnotationsImpl.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/service/parser/impl/RessourceDiffusableParserServiceJacksonAnnotationsImpl.java index 048cd0f..aa83f7d 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/service/parser/impl/RessourceDiffusableParserServiceJacksonAnnotationsImpl.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/service/parser/impl/RessourceDiffusableParserServiceJacksonAnnotationsImpl.java @@ -14,17 +14,16 @@ */ package fr.recia.ressourcesdiffusablesapi.service.parser.impl; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.cfg.ConstructorDetector; -import com.fasterxml.jackson.databind.json.JsonMapper; + import fr.recia.ressourcesdiffusablesapi.model.RessourceDiffusable; import fr.recia.ressourcesdiffusablesapi.service.parser.IRessourceDiffusableParserService; import lombok.extern.slf4j.Slf4j; +import tools.jackson.databind.JsonNode; +import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.cfg.ConstructorDetector; +import tools.jackson.databind.json.JsonMapper; import java.io.IOException; -import java.io.UncheckedIOException; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -39,7 +38,6 @@ public List parseRawJsonStringIntoRessourceDiffusableList(S ObjectMapper mapper = JsonMapper.builder() .constructorDetector(ConstructorDetector.EXPLICIT_ONLY) .build(); - mapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, false); JsonNode rootNode = mapper.readTree(rawJsonString); JsonNode inside = rootNode.get(0); diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesController.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesController.java index c2cd990..4ec15c9 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesController.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesController.java @@ -20,6 +20,8 @@ import fr.recia.ressourcesdiffusablesapi.model.apiresponse.ApiResponseError; import fr.recia.ressourcesdiffusablesapi.service.filter.IRessourceDiffusableFilterService; import fr.recia.ressourcesdiffusablesapi.web.rest.exceptions.RequestArgumentNumericValueInvalidException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -32,8 +34,6 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; @Slf4j @RestController diff --git a/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckController.java b/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckController.java index 998b5d7..4d94ac0 100644 --- a/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckController.java +++ b/src/main/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckController.java @@ -14,14 +14,14 @@ */ package fr.recia.ressourcesdiffusablesapi.web.rest; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; @Slf4j @RestController diff --git a/src/test/java/fr/recia/ressourcesdiffusablesapi/config/TestSecurityConfiguration.java b/src/test/java/fr/recia/ressourcesdiffusablesapi/config/TestSecurityConfiguration.java new file mode 100644 index 0000000..9e9f8e2 --- /dev/null +++ b/src/test/java/fr/recia/ressourcesdiffusablesapi/config/TestSecurityConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2021 GIP-RECIA, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package fr.recia.ressourcesdiffusablesapi.config; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.http.HttpMethod; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.web.FilterChainProxy; +import org.springframework.security.web.SecurityFilterChain; + +@TestConfiguration +@Slf4j +public class TestSecurityConfiguration { + + @Bean + SecurityFilterChain testSecurityFilterChain(HttpSecurity http) throws Exception { + http + .csrf(csrf -> csrf.disable()) + .authorizeHttpRequests(authz -> authz + .requestMatchers("/health-check").permitAll() + .requestMatchers(HttpMethod.GET, "/api/**").permitAll() + .anyRequest().denyAll() + ); + + return http.build(); + } + + @Bean + ApplicationRunner securityChainsLogger(FilterChainProxy filterChainProxy) { + return args -> { + log.info("SECURITY FILTER CHAINS:"); + System.out.println("SECURITY FILTER CHAINS:"); + filterChainProxy.getFilterChains().forEach(chain -> { + log.info("{}", chain); + System.out.println(chain.toString()); + } + + ); + }; + } +} diff --git a/src/test/java/fr/recia/ressourcesdiffusablesapi/service/cache/CacheServiceJsonImplTest.java b/src/test/java/fr/recia/ressourcesdiffusablesapi/service/cache/CacheServiceJsonImplTest.java index 2f5c415..8efb6b0 100644 --- a/src/test/java/fr/recia/ressourcesdiffusablesapi/service/cache/CacheServiceJsonImplTest.java +++ b/src/test/java/fr/recia/ressourcesdiffusablesapi/service/cache/CacheServiceJsonImplTest.java @@ -29,11 +29,11 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.context.ApplicationContext; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -65,22 +65,22 @@ public class CacheServiceJsonImplTest { @Autowired private ApplicationContext ctx; - @MockBean + @MockitoBean private Clock clock; - @MockBean + @MockitoBean private IRessourceDiffusableDAO ressourceDiffusableDAO; - @MockBean + @MockitoBean private ICacheFileIO cacheFileIO; - @MockBean + @MockitoBean private IRessourceDiffusableParserService ressourceDiffusableParserService; - @SpyBean + @MockitoSpyBean private AppProperties appProperties; - @MockBean + @MockitoBean CacheProperties cacheProperties; diff --git a/src/test/java/fr/recia/ressourcesdiffusablesapi/service/filter/RessourceDiffusableFilterTest.java b/src/test/java/fr/recia/ressourcesdiffusablesapi/service/filter/RessourceDiffusableFilterTest.java index 6ed761d..59bb6bf 100644 --- a/src/test/java/fr/recia/ressourcesdiffusablesapi/service/filter/RessourceDiffusableFilterTest.java +++ b/src/test/java/fr/recia/ressourcesdiffusablesapi/service/filter/RessourceDiffusableFilterTest.java @@ -14,6 +14,7 @@ */ package fr.recia.ressourcesdiffusablesapi.service.filter; +import fr.recia.ressourcesdiffusablesapi.config.TestSecurityConfiguration; import fr.recia.ressourcesdiffusablesapi.model.PaginationRequest; import fr.recia.ressourcesdiffusablesapi.model.PaginationResponse; import fr.recia.ressourcesdiffusablesapi.model.RessourceDiffusable; @@ -22,15 +23,18 @@ import fr.recia.ressourcesdiffusablesapi.service.cache.ICacheService; import fr.recia.ressourcesdiffusablesapi.service.filter.impl.RessourceDiffusableFilterServiceDefaultImpl; import fr.recia.ressourcesdiffusablesapi.test.utils.RessourceDiffusableForMockFactory; +import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.annotation.DirtiesContext; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; +import org.springframework.context.annotation.Import; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @@ -42,15 +46,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.doReturn; +@ActiveProfiles("test") @TestMethodOrder(MethodOrderer.OrderAnnotation.class) @ExtendWith(SpringExtension.class) -@TestInstance(TestInstance.Lifecycle.PER_CLASS) @SpringJUnitConfig @SpringBootTest -@DirtiesContext(classMode= DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) public class RessourceDiffusableFilterTest { - @MockBean + @MockitoBean ICacheService cacheService; @Autowired diff --git a/src/test/java/fr/recia/ressourcesdiffusablesapi/test/TestUtil.java b/src/test/java/fr/recia/ressourcesdiffusablesapi/test/TestUtil.java index c405107..9747009 100644 --- a/src/test/java/fr/recia/ressourcesdiffusablesapi/test/TestUtil.java +++ b/src/test/java/fr/recia/ressourcesdiffusablesapi/test/TestUtil.java @@ -19,7 +19,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import org.springframework.http.MediaType; /** @@ -32,7 +31,6 @@ public class TestUtil { private static ObjectMapper createObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); - mapper.registerModule(new JavaTimeModule()); return mapper; } diff --git a/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesControllerTest.java b/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesControllerTest.java index 5f30995..ab8d6da 100644 --- a/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesControllerTest.java +++ b/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/ApiRessourcesDiffusablesControllerTest.java @@ -14,6 +14,8 @@ */ package fr.recia.ressourcesdiffusablesapi.web.rest; +import fr.recia.ressourcesdiffusablesapi.config.TestSecurityConfiguration; +import fr.recia.ressourcesdiffusablesapi.config.beans.SoffitProperties; import fr.recia.ressourcesdiffusablesapi.model.PaginationRequest; import fr.recia.ressourcesdiffusablesapi.model.PaginationResponse; import fr.recia.ressourcesdiffusablesapi.model.RessourceDiffusable; @@ -26,30 +28,43 @@ import fr.recia.ressourcesdiffusablesapi.web.rest.matchers.RessourceDiffusableFilterMatcher; import lombok.extern.slf4j.Slf4j; import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Profile; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; +import org.springframework.security.core.context.SecurityContextHolder; import java.util.List; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; @ExtendWith(SpringExtension.class) @Slf4j @WithMockUser -@WebMvcTest +@SpringBootTest +@ActiveProfiles("test") +@AutoConfigureMockMvc +@Import(TestSecurityConfiguration.class) class ApiRessourcesDiffusablesControllerTest { - @MockBean + @MockitoBean private IRessourceDiffusableFilterService filterService; @Autowired @@ -70,11 +85,19 @@ void testJsonApiRessourcesDiffusables() throws Exception { PaginationRequest paginationRequest = new PaginationRequest(requestPage, resourcesPerPage); RessourceDiffusableFilter ressourceDiffusableFilter = new RessourceDiffusableFilter(operator, null,null,null,null,null,null,null,null,null, null); - - doReturn(tuple2Values).when(filterService).getRessourcesDiffusablesFiltered(argThat(new RessourceDiffusableFilterMatcher(ressourceDiffusableFilter)), argThat(new PaginationRequestMatcher(paginationRequest))); - - String stringUrlTemplate = String.format("/api/ressources-diffusables?page=%1$d&ressourcesPerPage=%2$d&operator=%3$s", requestPage, resourcesPerPage, operator); - this.mockListRessourcesMvc.perform(get(stringUrlTemplate) + doReturn(tuple2Values) + .when(filterService) + .getRessourcesDiffusablesFiltered( + argThat(new RessourceDiffusableFilterMatcher(ressourceDiffusableFilter)), + argThat(new PaginationRequestMatcher(paginationRequest)) + ); + + mockListRessourcesMvc + .perform(get("/api/ressources-diffusables") + .with(user("testUser").roles("USER")) + .param("page", String.valueOf(requestPage)) + .param("ressourcesPerPage", String.valueOf(resourcesPerPage)) + .param("operator", operator) .contentType(TestUtil.APPLICATION_JSON_UTF8) .accept(TestUtil.APPLICATION_JSON_UTF8)) .andDo(MockMvcResultHandlers.print()) @@ -146,6 +169,7 @@ void testJsonApiRessourcesDiffusables() throws Exception { @Test void testJsonApiRessourcesDiffusablesBadRequest() throws Exception { this.mockListRessourcesMvc.perform(get("/api/ressources-diffusables?page=pokemon") + .with(user("testUser").roles("USER")) .contentType(TestUtil.APPLICATION_JSON_UTF8) .accept(TestUtil.APPLICATION_JSON_UTF8)) .andDo(MockMvcResultHandlers.print()) diff --git a/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckTest.java b/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckTest.java index 3289f41..1d72c51 100644 --- a/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckTest.java +++ b/src/test/java/fr/recia/ressourcesdiffusablesapi/web/rest/HealthCheckTest.java @@ -14,12 +14,18 @@ */ package fr.recia.ressourcesdiffusablesapi.web.rest; +import fr.recia.ressourcesdiffusablesapi.config.TestSecurityConfiguration; import fr.recia.ressourcesdiffusablesapi.test.TestUtil; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.MockitoAnnotations; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; +import org.springframework.context.annotation.Import; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.result.MockMvcResultHandlers; @@ -30,21 +36,17 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.head; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +@ActiveProfiles("test") +@AutoConfigureMockMvc +@Import(TestSecurityConfiguration.class) @ExtendWith(SpringExtension.class) @Slf4j @SpringBootTest class HealthCheckTest { + @Autowired private MockMvc mockHealthCheckMvc; - @PostConstruct - public void setup() { - MockitoAnnotations.openMocks(this); - - HealthCheckController healthCheckController = new HealthCheckController(); - - this.mockHealthCheckMvc = MockMvcBuilders.standaloneSetup(healthCheckController).build(); - } @Test void testHealthCheck() throws Exception { diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 0000000..d7f4e45 --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,3 @@ +app: + soffit: + jwt-signature-key: 'testsignaturekey' \ No newline at end of file