From 517a399cabe4dd7a40c07a81facd599df3cbf9cd Mon Sep 17 00:00:00 2001
From: Antoine Rey <antoine.rey@free.fr>
Date: Tue, 28 Dec 2021 16:59:23 +0100
Subject: [PATCH] Bump to Spring Boot 2.6.2 #63

---
 pom.xml                                       | 16 ++++---
 .../util/ApplicationSwaggerConfig.java        | 42 +++++++++++++++++++
 .../resources/application-hsqldb.properties   |  6 +--
 .../resources/application-mysql.properties    |  6 +--
 .../application-postgresql.properties         |  6 +--
 src/main/resources/application.properties     |  9 ++--
 .../rest/OwnerRestControllerTests.java        |  5 ++-
 .../rest/PetRestControllerTests.java          |  4 ++
 .../rest/VisitRestControllerTests.java        |  2 +
 .../clinicService/ApplicationTestConfig.java  |  2 +-
 .../userService/AbstractUserServiceTests.java |  2 +-
 src/test/resources/application.properties     |  8 ++--
 12 files changed, 81 insertions(+), 27 deletions(-)

diff --git a/pom.xml b/pom.xml
index 8aa89d9c..4b4a805d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
 
     <groupId>org.springframework.samples</groupId>
     <artifactId>spring-petclinic-rest</artifactId>
-    <version>2.4.2</version>
+    <version>2.6.2</version>
 
     <description>REST version of the Spring Petclinic sample application</description>
     <url>https://spring-petclinic.github.io/</url>
@@ -13,22 +13,26 @@
     <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
-        <version>2.4.2</version>
+        <version>2.6.2</version>
         <relativePath/> <!-- lookup parent from Maven repository -->
     </parent>
 
     <properties>
+        <!-- Third librairies -->
         <spring-data-jdbc.version>1.2.1.RELEASE</spring-data-jdbc.version>
         <springfox-swagger.version>3.0.0</springfox-swagger.version>
-        <jacoco.version>0.8.7</jacoco.version>
-        <docker.jib-maven-plugin.version>1.3.0</docker.jib-maven-plugin.version>
-        <docker.image.prefix>springcommunity</docker.image.prefix>
-        <jsr305.version>3.0.2</jsr305.version>
         <jackson-databind-nullable.version>0.2.1</jackson-databind-nullable.version>
         <mapstruct.version>1.4.1.Final</mapstruct.version>
         <jaxb-api.version>2.3.0</jaxb-api.version>
+
+        <!-- Maven plugins -->
+        <jacoco.version>0.8.7</jacoco.version>
         <openapi-generator-maven-plugin.version>5.2.1</openapi-generator-maven-plugin.version>
         <build-helper-maven-plugin.version>3.2.0</build-helper-maven-plugin.version>
+
+        <!-- Docker -->
+        <docker.jib-maven-plugin.version>1.3.0</docker.jib-maven-plugin.version>
+        <docker.image.prefix>springcommunity</docker.image.prefix>
     </properties>
 
     <dependencies>
diff --git a/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java b/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java
index 18df3bd7..3c406a55 100755
--- a/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java
+++ b/src/main/java/org/springframework/samples/petclinic/util/ApplicationSwaggerConfig.java
@@ -17,19 +17,28 @@
 package org.springframework.samples.petclinic.util;
 
 
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 
+import org.springframework.util.ReflectionUtils;
+import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
 import springfox.documentation.service.ApiInfo;
 import springfox.documentation.service.Contact;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
+import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
+import java.lang.reflect.Field;
 import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * Java config for Springfox swagger documentation plugin
@@ -67,5 +76,38 @@ public class ApplicationSwaggerConfig {
 		"http://www.apache.org/licenses/LICENSE-2.0", Collections.emptyList());
    }
 
+    /**
+     * Springfox workaround required by Spring Boot 2.6
+     * See https://github.com/springfox/springfox/issues/346
+     */
+    @Bean
+    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
+        return new BeanPostProcessor() {
+
+            @Override
+            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
+                if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
+                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
+                }
+                return bean;
+            }
+
+            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
+                mappings.removeIf(mapping -> mapping.getPatternParser() != null);
+            }
+
+            @SuppressWarnings("unchecked")
+            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
+                try {
+                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
+                    field.setAccessible(true);
+                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
+                } catch (IllegalArgumentException | IllegalAccessException e) {
+                    throw new IllegalStateException(e);
+                }
+            }
+        };
+    }
+
 
 }
diff --git a/src/main/resources/application-hsqldb.properties b/src/main/resources/application-hsqldb.properties
index 8af33d29..5a915662 100644
--- a/src/main/resources/application-hsqldb.properties
+++ b/src/main/resources/application-hsqldb.properties
@@ -1,8 +1,8 @@
 # HSQLDB config start
 #----------------------------------------------------------------
 
-spring.datasource.schema=classpath*:db/hsqldb/initDB.sql
-spring.datasource.data=classpath*:db/hsqldb/populateDB.sql
+spring.sql.init.schema-locations=classpath*:db/hsqldb/initDB.sql
+spring.sql.init.data-locations=classpath*:db/hsqldb/populateDB.sql
 
 spring.datasource.url=jdbc:hsqldb:mem:petclinic
 spring.datasource.username=sa 
@@ -11,4 +11,4 @@ spring.jpa.database=HSQL
 spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect
 spring.jpa.hibernate.ddl-auto=none
 #----------------------------------------------------------------
-# HSQLDB config end
\ No newline at end of file
+# HSQLDB config end
diff --git a/src/main/resources/application-mysql.properties b/src/main/resources/application-mysql.properties
index d429b5ac..daffa4ab 100644
--- a/src/main/resources/application-mysql.properties
+++ b/src/main/resources/application-mysql.properties
@@ -1,7 +1,7 @@
 # uncomment for init database (first start)
-#spring.datasource.initialization-mode=always
-#spring.datasource.schema=classpath*:db/mysql/initDB.sql
-#spring.datasource.data=classpath*:db/mysql/populateDB.sql
+#spring.sql.init.mode=always
+#spring.sql.init.schema-locations=classpath*:db/mysql/initDB.sql
+#spring.sql.init.data-locations=classpath*:db/mysql/populateDB.sql
 
 # MySQL config start
 #----------------------------------------------------------------
diff --git a/src/main/resources/application-postgresql.properties b/src/main/resources/application-postgresql.properties
index ba933e5d..456b590e 100644
--- a/src/main/resources/application-postgresql.properties
+++ b/src/main/resources/application-postgresql.properties
@@ -1,7 +1,7 @@
 # uncomment for init database (first start)
-#spring.datasource.initialization-mode=always
-#spring.datasource.schema=classpath*:db/postgresql/initDB.sql
-#spring.datasource.data=classpath*:db/postgresql/populateDB.sql
+#spring.sql.init.mode=always
+#spring.sql.init.schema-locations=classpath*:db/postgresql/initDB.sql
+#spring.sql.init.data-locations=classpath*:db/postgresql/populateDB.sql
 
 # PostgreSQL config start
 #----------------------------------------------------------------
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index e3669c9b..c558a190 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -23,7 +23,12 @@ spring.profiles.active=hsqldb,spring-data-jpa
 server.port=9966
 server.servlet.context-path=/petclinic/
 
+# Springfox workaround required by Spring Boot 2.6
+# See https://github.com/springfox/springfox/issues/3462
+spring.mvc.pathmatch.matching-strategy=ant_path_matcher
+
 spring.messages.basename=messages/messages
+spring.jpa.open-in-view=false
 
 logging.level.org.springframework=INFO
 #logging.level.org.springframework=DEBUG
@@ -35,7 +40,3 @@ logging.level.org.springframework=INFO
 # by default the authentication is disabled
 petclinic.security.enable=false
 
-# ------------------------------------------------
-# Spring doc configuration
-springdoc.api-docs.enabled=true
-springdoc.writer-with-default-pretty-printer= true
diff --git a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java
index b329ebe0..033d4e56 100644
--- a/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/rest/OwnerRestControllerTests.java
@@ -76,7 +76,7 @@ class OwnerRestControllerTests {
         this.mockMvc = MockMvcBuilders.standaloneSetup(ownerRestController)
             .setControllerAdvice(new ExceptionControllerAdvice())
             .build();
-        owners = new ArrayList<OwnerDto>();
+        owners = new ArrayList<>();
 
         OwnerDto ownerWithPet = new OwnerDto();
         owners.add(ownerWithPet.id(1).firstName("George").lastName("Franklin").address("110 W. Liberty St.").city("Madison").telephone("6085551023").addPetsItem(getTestPetWithIdAndName(ownerWithPet, 1, "Rosy")));
@@ -304,6 +304,7 @@ class OwnerRestControllerTests {
         OwnerDto newOwnerDto = owners.get(0);
         newOwnerDto.setFirstName("");
         ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto);
         this.mockMvc.perform(put("/api/owners/1")
                 .content(newOwnerAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
@@ -315,6 +316,7 @@ class OwnerRestControllerTests {
     void testDeleteOwnerSuccess() throws Exception {
         OwnerDto newOwnerDto = owners.get(0);
         ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto);
         final Owner owner = ownerMapper.toOwner(owners.get(0));
         given(this.clinicService.findOwnerById(1)).willReturn(owner);
@@ -328,6 +330,7 @@ class OwnerRestControllerTests {
     void testDeleteOwnerError() throws Exception {
         OwnerDto newOwnerDto = owners.get(0);
         ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newOwnerAsJSON = mapper.writeValueAsString(newOwnerDto);
         given(this.clinicService.findOwnerById(-1)).willReturn(null);
         this.mockMvc.perform(delete("/api/owners/-1")
diff --git a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java
index 2297d112..7778f271 100644
--- a/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/rest/PetRestControllerTests.java
@@ -176,6 +176,7 @@ class PetRestControllerTests {
         ObjectMapper mapper = new ObjectMapper();
         mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
         mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
+        mapper.registerModule(new JavaTimeModule());
         String newPetAsJSON = mapper.writeValueAsString(newPet);
         this.mockMvc.perform(post("/api/pets/")
             .content(newPetAsJSON).accept(MediaType.APPLICATION_JSON_VALUE).contentType(MediaType.APPLICATION_JSON_VALUE))
@@ -214,6 +215,7 @@ class PetRestControllerTests {
         PetDto newPet = pets.get(0);
         newPet.setName(null);
         ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
         mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
         String newPetAsJSON = mapper.writeValueAsString(newPet);
@@ -228,6 +230,7 @@ class PetRestControllerTests {
     void testDeletePetSuccess() throws Exception {
         PetDto newPet = pets.get(0);
         ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newPetAsJSON = mapper.writeValueAsString(newPet);
         given(this.clinicService.findPetById(3)).willReturn(petMapper.toPet(pets.get(0)));
         this.mockMvc.perform(delete("/api/pets/3")
@@ -240,6 +243,7 @@ class PetRestControllerTests {
     void testDeletePetError() throws Exception {
         PetDto newPet = pets.get(0);
         ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newPetAsJSON = mapper.writeValueAsString(newPet);
         given(this.clinicService.findPetById(-1)).willReturn(null);
         this.mockMvc.perform(delete("/api/pets/-1")
diff --git a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java
index 33cad056..7a9afdc9 100644
--- a/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/rest/VisitRestControllerTests.java
@@ -225,6 +225,7 @@ class VisitRestControllerTests {
     void testDeleteVisitSuccess() throws Exception {
     	Visit newVisit = visits.get(0);
     	ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit));
     	given(this.clinicService.findVisitById(2)).willReturn(visits.get(0));
     	this.mockMvc.perform(delete("/api/visits/2")
@@ -237,6 +238,7 @@ class VisitRestControllerTests {
     void testDeleteVisitError() throws Exception {
     	Visit newVisit = visits.get(0);
     	ObjectMapper mapper = new ObjectMapper();
+        mapper.registerModule(new JavaTimeModule());
         String newVisitAsJSON = mapper.writeValueAsString(visitMapper.toVisitDto(newVisit));
     	given(this.clinicService.findVisitById(-1)).willReturn(null);
     	this.mockMvc.perform(delete("/api/visits/-1")
diff --git a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java
index a2014552..79398dc1 100644
--- a/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java
+++ b/src/test/java/org/springframework/samples/petclinic/service/clinicService/ApplicationTestConfig.java
@@ -7,7 +7,7 @@ import org.springframework.boot.test.context.TestConfiguration;
 public class ApplicationTestConfig {
 
 	public ApplicationTestConfig(){
-		MockitoAnnotations.initMocks(this);
+		MockitoAnnotations.openMocks(this);
 	}
 
 }
diff --git a/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java b/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java
index 488ce61a..54ecdded 100644
--- a/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java
+++ b/src/test/java/org/springframework/samples/petclinic/service/userService/AbstractUserServiceTests.java
@@ -17,7 +17,7 @@ public abstract class AbstractUserServiceTests {
 
     @BeforeEach
     public void init() {
-        MockitoAnnotations.initMocks(this);
+        MockitoAnnotations.openMocks(this);
     }
 
     @Test
diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties
index 09ce9a4d..31f3b808 100644
--- a/src/test/resources/application.properties
+++ b/src/test/resources/application.properties
@@ -21,7 +21,8 @@ spring.profiles.active=hsqldb,spring-data-jpa
 # ------------------------------------------------
 
 server.port=9966
-server.context-path=/petclinic/
+server.servlet.context-path=/petclinic/
+spring.jpa.open-in-view=false
 
 spring.messages.basename=messages/messages
 logging.level.org.springframework=INFO
@@ -35,7 +36,4 @@ logging.level.org.springframework=INFO
 security.ignored=/**
 basic.authentication.enabled=true
 petclinic.security.enable=true
-# ------------------------------------------------
-# Spring doc configuration
-springdoc.api-docs.enabled=true
-springdoc.writer-with-default-pretty-printer=true
+
-- 
GitLab