From 1637afeab8c1226ac7303845d3f2fd833aafe468 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 23 May 2024 16:11:39 +0200 Subject: [PATCH 1/8] feat: upgrade to spring boot 2.7.X --- pom.xml | 51 +++--- .../StatisticsServiceApplication.java | 4 - .../CustomSwaggerUIApiResourceController.java | 20 --- .../statisticsservice/config/AppConfig.java | 12 ++ .../CustomSwaggerUIPathWebMvcConfigurer.java | 22 --- .../config/SecurityConfig.java | 8 - .../config/SpringFoxConfig.java | 109 ------------- .../resources/application-testing.properties | 4 + src/main/resources/application.properties | 4 +- .../api/authorization/AuthorityTest.java | 2 +- .../RoleAuthorizationAuthorityMapperTest.java | 3 +- .../StatisticsControllerAuthorizationIT.java | 6 +- .../controller/StatisticsControllerIT.java | 27 ++-- .../api/helper/AuthenticatedUserTest.java | 32 ++-- .../api/service/LogServiceTest.java | 101 ------------ .../service/UserStatisticsServiceTest.java | 25 ++- .../TenantHeaderSupplierTest.java | 1 - .../ArchiveOrDeleteSessionListenerTest.java | 9 +- .../listener/AssignSessionListenerTest.java | 15 +- .../listener/CreateMessageListenerTest.java | 9 +- .../listener/RegistrationListenerTest.java | 9 +- .../listener/StartVideoCallListenerTest.java | 8 +- .../listener/StopVideoCallListenerTest.java | 45 +++--- .../model/StatisticsEventBuilderTest.java | 151 ++++++++++-------- .../StatisticsEventRepositoryIT.java | 14 +- ...tatisticsEventTenantAwareRepositoryIT.java | 9 +- .../RegistrationStatisticsServiceTest.java | 1 - .../service/StatisticsServiceTest.java | 16 +- .../CustomResponseErrorHandlerTest.java | 5 +- 29 files changed, 254 insertions(+), 468 deletions(-) delete mode 100644 src/main/java/de/caritas/cob/statisticsservice/api/controller/CustomSwaggerUIApiResourceController.java delete mode 100644 src/main/java/de/caritas/cob/statisticsservice/config/CustomSwaggerUIPathWebMvcConfigurer.java delete mode 100644 src/main/java/de/caritas/cob/statisticsservice/config/SpringFoxConfig.java delete mode 100644 src/test/java/de/caritas/cob/statisticsservice/api/service/LogServiceTest.java diff --git a/pom.xml b/pom.xml index 4663cb2..a01dd93 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,7 @@ org.springframework.boot spring-boot-starter-parent - 2.5.12 + 2.7.18 @@ -30,7 +30,7 @@ 6.2.1 3.0.0 3.3.5 - 5.7.5 + 5.7.12 2.10.9.2 @@ -64,6 +64,7 @@ org.springframework.boot spring-boot-starter-data-mongodb + 2.7.5 org.springframework.data @@ -73,7 +74,6 @@ org.hibernate.validator hibernate-validator - 6.1.6.Final @@ -104,13 +104,6 @@ 4.3.1 - - - io.springfox - springfox-boot-starter - ${springfox-boot-starter.version} - - org.keycloak @@ -150,7 +143,6 @@ org.apache.commons commons-lang3 - 3.11 @@ -168,7 +160,6 @@ org.springframework.boot spring-boot-autoconfigure - 2.3.5.RELEASE @@ -195,24 +186,18 @@ - - powermock-module-junit4 - org.powermock - test - 2.0.2 - - - powermock-api-mockito2 - org.powermock - test - 2.0.2 - de.flapdoodle.embed de.flapdoodle.embed.mongo + 4.13.0 test + + de.flapdoodle.embed + de.flapdoodle.embed.mongo.spring26x + 4.13.0 + @@ -237,6 +222,24 @@ spring-boot-maven-plugin + + org.openrewrite.maven + rewrite-maven-plugin + 5.30.0 + + + org.openrewrite.java.spring.boot2.UpgradeSpringBoot_2_7 + + + + + org.openrewrite.recipe + rewrite-spring + 5.9.0 + + + + org.openapitools diff --git a/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java b/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java index 184e804..ab78a20 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java +++ b/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java @@ -7,10 +7,6 @@ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class StatisticsServiceApplication { - static { - System.setProperty("os.arch", "i686_64"); - } - public static void main(String[] args) { SpringApplication.run(StatisticsServiceApplication.class, args); } diff --git a/src/main/java/de/caritas/cob/statisticsservice/api/controller/CustomSwaggerUIApiResourceController.java b/src/main/java/de/caritas/cob/statisticsservice/api/controller/CustomSwaggerUIApiResourceController.java deleted file mode 100644 index 144f217..0000000 --- a/src/main/java/de/caritas/cob/statisticsservice/api/controller/CustomSwaggerUIApiResourceController.java +++ /dev/null @@ -1,20 +0,0 @@ -package de.caritas.cob.statisticsservice.api.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import springfox.documentation.annotations.ApiIgnore; -import springfox.documentation.swagger.web.ApiResourceController; -import springfox.documentation.swagger.web.SwaggerResourcesProvider; - -@Controller -@ApiIgnore -@RequestMapping(value = "${springfox.docuPath}" + "/swagger-resources") -public class CustomSwaggerUIApiResourceController extends ApiResourceController { - - public static final String SWAGGER_UI_BASE_URL = "/statistics/docs"; - - public CustomSwaggerUIApiResourceController(SwaggerResourcesProvider swaggerResources) { - super(swaggerResources, SWAGGER_UI_BASE_URL); - } - -} diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/AppConfig.java b/src/main/java/de/caritas/cob/statisticsservice/config/AppConfig.java index 58f0389..c0a8f9a 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/config/AppConfig.java +++ b/src/main/java/de/caritas/cob/statisticsservice/config/AppConfig.java @@ -1,5 +1,7 @@ package de.caritas.cob.statisticsservice.config; +import org.keycloak.adapters.KeycloakConfigResolver; +import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; @@ -25,4 +27,14 @@ public LocalValidatorFactoryBean validator(MessageSource messageSource) { return validatorFactoryBean; } + /** + * Use the KeycloakSpringBootConfigResolver to be able to save the Keycloak settings in the spring + * application properties. + */ + @Bean + public KeycloakConfigResolver keyCloakConfigResolver() { + return new KeycloakSpringBootConfigResolver(); + } + + } diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/CustomSwaggerUIPathWebMvcConfigurer.java b/src/main/java/de/caritas/cob/statisticsservice/config/CustomSwaggerUIPathWebMvcConfigurer.java deleted file mode 100644 index 49a4399..0000000 --- a/src/main/java/de/caritas/cob/statisticsservice/config/CustomSwaggerUIPathWebMvcConfigurer.java +++ /dev/null @@ -1,22 +0,0 @@ -package de.caritas.cob.statisticsservice.config; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -@Component -public class CustomSwaggerUIPathWebMvcConfigurer implements WebMvcConfigurer { - - @Value("${springfox.docuPath}") - private String docuPath; - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler(docuPath + "/swagger-ui/**") - .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/"); - registry.addResourceHandler(docuPath + "/**") - .addResourceLocations("classpath:/META-INF/resources/"); - } - -} diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/SecurityConfig.java b/src/main/java/de/caritas/cob/statisticsservice/config/SecurityConfig.java index 7886f37..f60ce96 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/config/SecurityConfig.java +++ b/src/main/java/de/caritas/cob/statisticsservice/config/SecurityConfig.java @@ -93,14 +93,6 @@ protected void configure(HttpSecurity http) throws Exception { .anyRequest().denyAll(); } - /** - * Use the KeycloakSpringBootConfigResolver to be able to save the Keycloak settings in the spring - * application properties. - */ - @Bean - public KeycloakConfigResolver keyCloakConfigResolver() { - return new KeycloakSpringBootConfigResolver(); - } /** * Change springs authentication strategy to be stateless (no session is being created). diff --git a/src/main/java/de/caritas/cob/statisticsservice/config/SpringFoxConfig.java b/src/main/java/de/caritas/cob/statisticsservice/config/SpringFoxConfig.java deleted file mode 100644 index cad6032..0000000 --- a/src/main/java/de/caritas/cob/statisticsservice/config/SpringFoxConfig.java +++ /dev/null @@ -1,109 +0,0 @@ -package de.caritas.cob.statisticsservice.config; - -import static java.util.Collections.singletonList; - -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.ApiKey; -import springfox.documentation.service.AuthorizationScope; -import springfox.documentation.service.Contact; -import springfox.documentation.service.SecurityReference; -import springfox.documentation.service.SecurityScheme; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spi.service.contexts.SecurityContext; -import springfox.documentation.spring.web.plugins.Docket; - -/** - * Provides the SpringFox (API documentation generation) configuration. - * - */ -@Configuration -@Import(BeanValidatorPluginsConfiguration.class) -public class SpringFoxConfig { - - @Value("${springfox.docuTitle}") - private String docuTitle; - @Value("${springfox.docuDescription}") - private String docuDescription; - @Value("${springfox.docuVersion}") - private String docuVersion; - @Value("${springfox.docuTermsUrl}") - private String docuTermsUrl; - @Value("${springfox.docuContactName}") - private String docuContactName; - @Value("${springfox.docuContactUrl}") - private String docuContactUrl; - @Value("${springfox.docuContactEmail}") - private String docuContactEmail; - @Value("${springfox.docuLicense}") - private String docuLicense; - @Value("${springfox.docuLicenseUrl}") - private String docuLicenseUrl; - - @Bean - public Docket apiDocket() { - return new Docket(DocumentationType.SWAGGER_2).select() - .apis(RequestHandlerSelectors.basePackage("de.caritas.cob.statisticsservice.api")).build() - .consumes(getContentTypes()).produces(getContentTypes()).apiInfo(getApiInfo()) - .useDefaultResponseMessages(false).protocols(protocols()).securitySchemes(securitySchemes()) - .securityContexts(securityContexts()); - } - - private List securityContexts() { - return singletonList(SecurityContext.builder() - .forPaths(PathSelectors.any()).securityReferences(securityReferences()).build()); - } - - private List securityReferences() { - return singletonList( - SecurityReference.builder().reference("token").scopes(new AuthorizationScope[0]).build()); - } - - private List securitySchemes() { - return singletonList(new ApiKey("Bearer", "Authorization", "header")); - } - - /** - * Returns the API protocols (for documentation). - * - * @return the supported protocols - */ - private Set protocols() { - Set protocols = new HashSet<>(); - protocols.add("https"); - return protocols; - } - - /** - * Returns all content types which should be consumed/produced. - * - * @return the supported content types - */ - private Set getContentTypes() { - Set contentTypes = new HashSet<>(); - contentTypes.add("application/json"); - return contentTypes; - } - - /** - * Returns the API information (defined in application.properties). - * - * @return api information - */ - private ApiInfo getApiInfo() { - return new ApiInfo(docuTitle, docuDescription, docuVersion, docuTermsUrl, - new Contact(docuContactName, docuContactUrl, docuContactEmail), docuLicense, docuLicenseUrl, - Collections.emptyList()); - } - -} diff --git a/src/main/resources/application-testing.properties b/src/main/resources/application-testing.properties index c3d440b..e444b9f 100644 --- a/src/main/resources/application-testing.properties +++ b/src/main/resources/application-testing.properties @@ -22,4 +22,8 @@ csrf.header.property=X-CSRF-TOKEN csrf.cookie.property=CSRF-TOKEN consulting.type.service.api.url= +spring.mongodb.embedded.version=5.0.6 +spring.data.mongodb.uri=mongodb://mongodb:27017/consulting_types?retryWrites=false +de.flapdoodle.mongodb.embedded.version=5.0.6 + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 04a1950..4d07972 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -11,7 +11,7 @@ spring.data.jpa.repositories.bootstrap-mode=default # General app settings server.port=8080 -spring.mvc.locale=de_DE +spring.web.locale=de_DE spring.jackson.time-zone=Europe/Berlin # Logging: SLF4J (via Lombok) @@ -75,6 +75,6 @@ tenant.service.api.url=http://tenantservice:8080 management.endpoint.health.enabled=true management.endpoint.health.show-details=never management.endpoints.web.exposure.include=health -management.health.probes.enabled=true +management.endpoint.health.probes.enabled=true management.metrics.mongo.command.enabled=false management.metrics.mongo.connectionpool.enabled=false \ No newline at end of file diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/authorization/AuthorityTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/authorization/AuthorityTest.java index 0ffe01b..970e98e 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/authorization/AuthorityTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/authorization/AuthorityTest.java @@ -7,7 +7,7 @@ import static org.hamcrest.Matchers.nullValue; import de.caritas.cob.statisticsservice.api.authorization.Authority.AuthorityValue; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class AuthorityTest { diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/authorization/RoleAuthorizationAuthorityMapperTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/authorization/RoleAuthorizationAuthorityMapperTest.java index 644bf58..385e7e6 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/authorization/RoleAuthorizationAuthorityMapperTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/authorization/RoleAuthorizationAuthorityMapperTest.java @@ -10,7 +10,8 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerAuthorizationIT.java b/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerAuthorizationIT.java index ca55e9b..ca17be0 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerAuthorizationIT.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerAuthorizationIT.java @@ -9,9 +9,9 @@ import de.caritas.cob.statisticsservice.api.authorization.Authority.AuthorityValue; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventRepository; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventTenantAwareRepository; +import org.junit.jupiter.api.Test; + import javax.servlet.http.Cookie; -import org.junit.Test; -import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration; @@ -24,10 +24,8 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; -@RunWith(SpringRunner.class) @TestPropertySource(properties = "spring.profiles.active=testing") @SpringBootTest(classes = {StatisticsServiceApplication.class}) @EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerIT.java b/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerIT.java index 711aa61..ed1847d 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerIT.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/controller/StatisticsControllerIT.java @@ -9,7 +9,6 @@ import static de.caritas.cob.statisticsservice.api.testhelper.TestConstants.DATE_TO; import static de.caritas.cob.statisticsservice.api.testhelper.TestConstants.DATE_TO_FORMATTED; import static org.mockito.Mockito.when; -import static org.powermock.reflect.Whitebox.setInternalState; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -18,15 +17,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import de.caritas.cob.statisticsservice.StatisticsServiceApplication; import de.caritas.cob.statisticsservice.api.authorization.RoleAuthorizationAuthorityMapper; import de.caritas.cob.statisticsservice.api.authorization.StatisticsFeatureAuthorisationService; import de.caritas.cob.statisticsservice.api.service.LogService; import de.caritas.cob.statisticsservice.api.statistics.service.RegistrationStatisticsService; import de.caritas.cob.statisticsservice.api.statistics.service.StatisticsService; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.keycloak.adapters.KeycloakConfigResolver; import org.mockito.Mock; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -35,15 +32,13 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.http.MediaType; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.servlet.MockMvc; -@RunWith(SpringRunner.class) @WebMvcTest(StatisticsController.class) @AutoConfigureMockMvc(addFilters = false) -@ContextConfiguration(classes = StatisticsServiceApplication.class) -public class StatisticsControllerIT { +@TestPropertySource(properties = "spring.profiles.active=testing") +class StatisticsControllerIT { @Autowired private MockMvc mvc; @@ -60,16 +55,14 @@ public class StatisticsControllerIT { @MockBean StatisticsFeatureAuthorisationService statisticsFeatureAuthorisationService; + @MockBean + KeycloakConfigResolver keycloakConfigResolver; + @Mock private Logger logger; - @Before - public void setup() { - setInternalState(LogService.class, "LOGGER", logger); - } - @Test - public void getConsultantStatistics_Should_ReturnStatisticsDataAndOk() throws Exception { + void getConsultantStatistics_Should_ReturnStatisticsDataAndOk() throws Exception { when(statisticsService.fetchStatisticsData(DATE_FROM, DATE_TO)).thenReturn(CONSULTANT_STATISTICS_RESPONSE_DTO); @@ -81,7 +74,7 @@ public void getConsultantStatistics_Should_ReturnStatisticsDataAndOk() throws Ex } @Test - public void getRegistrationStatistics_Should_ReturnStatisticsDataAndOk() throws Exception { + void getRegistrationStatistics_Should_ReturnStatisticsDataAndOk() throws Exception { when(registrationStatisticsService.fetchRegistrationStatisticsData()).thenReturn(REGISTRATION_STATISTICS_LIST_RESPONSE_DTO); diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/helper/AuthenticatedUserTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/helper/AuthenticatedUserTest.java index 0e5afbd..103f770 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/helper/AuthenticatedUserTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/helper/AuthenticatedUserTest.java @@ -1,26 +1,34 @@ package de.caritas.cob.statisticsservice.api.helper; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; +import static org.junit.jupiter.api.Assertions.assertThrows; -@RunWith(MockitoJUnitRunner.class) +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) public class AuthenticatedUserTest { - @Test(expected = NullPointerException.class) + @Test public void AuthenticatedUser_Should_ThrowNullPointerExceptionWhenArgumentsAreNull() { - new AuthenticatedUser(null, null, null); + assertThrows(NullPointerException.class, () -> { + new AuthenticatedUser(null, null, null); + }); } - @Test(expected = NullPointerException.class) + @Test public void AuthenticatedUser_Should_ThrowNullPointerExceptionWhenUserIdIsNull() { - AuthenticatedUser authenticatedUser = new AuthenticatedUser(); - authenticatedUser.setUserId(null); + assertThrows(NullPointerException.class, () -> { + AuthenticatedUser authenticatedUser = new AuthenticatedUser(); + authenticatedUser.setUserId(null); + }); } - @Test(expected = NullPointerException.class) + @Test public void AuthenticatedUser_Should_ThrowNullPointerExceptionWhenUsernameIsNull() { - AuthenticatedUser authenticatedUser = new AuthenticatedUser(); - authenticatedUser.setUsername(null); + assertThrows(NullPointerException.class, () -> { + AuthenticatedUser authenticatedUser = new AuthenticatedUser(); + authenticatedUser.setUsername(null); + }); } } diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/service/LogServiceTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/service/LogServiceTest.java deleted file mode 100644 index a7b4a64..0000000 --- a/src/test/java/de/caritas/cob/statisticsservice/api/service/LogServiceTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package de.caritas.cob.statisticsservice.api.service; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.atLeastOnce; -import static org.mockito.Mockito.verify; -import static org.powermock.reflect.Whitebox.setInternalState; - -import java.io.PrintWriter; -import javax.ws.rs.BadRequestException; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.slf4j.Logger; -import org.springframework.http.HttpStatus; - -@RunWith(MockitoJUnitRunner.class) -public class LogServiceTest { - - @Mock - Exception exception; - - @Mock - BadRequestException badRequestException; - - @Mock - NumberFormatException numberFormatException; - - @Mock - private Logger logger; - - @Before - public void setup() { - setInternalState(LogService.class, "LOGGER", logger); - } - - @Test - public void logDatabaseError_Should_LogExceptionStackTrace() { - - LogService.logDatabaseError(exception); - verify(exception, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - - @Test - public void logBadRequestException_Should_LogExceptionStackTrace() { - - LogService.logBadRequestException(badRequestException); - verify(badRequestException, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - - @Test - public void logNumberFormatException_Should_LogExceptionStackTrace() { - - LogService.logNumberFormatException(numberFormatException); - verify(numberFormatException, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - - @Test - public void logInfo_Should_LogInfoMessage() { - - LogService.logInfo("info message"); - verify(logger, atLeastOnce()).info("info message"); - } - - @Test - public void logWarning_Should_LogWarnMessage_When_onlyExceptionIsProvided() { - - LogService.logWarning(exception); - verify(logger, atLeastOnce()).warn(eq("StatisticsService API: {}"), anyString()); - verify(exception, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - - @Test - public void logWarning_Should_LogWarnMessage_When_onlyExceptionAndStatusProvided() { - - LogService.logWarning(HttpStatus.MULTI_STATUS, exception); - verify(logger, atLeastOnce()).warn(eq("StatisticsService API: {}: {}"), - eq("Multi-Status"), anyString()); - verify(exception, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - - @Test - public void logInternalServerError_Should_LogError() { - - LogService.logInternalServerError(exception); - verify(logger, atLeastOnce()).error(eq("StatisticsService API: 500 Internal Server Error: {}"), - anyString()); - verify(exception, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - - @Test - public void logError_Should_LogError() { - LogService.logError(exception); - verify(logger, atLeastOnce()).error(eq("StatisticsService API: {}"), anyString()); - verify(exception, atLeastOnce()).printStackTrace(any(PrintWriter.class)); - } - -} diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsServiceTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsServiceTest.java index a71cabc..e07b4c1 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsServiceTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/service/UserStatisticsServiceTest.java @@ -9,16 +9,19 @@ import de.caritas.cob.statisticsservice.api.service.securityheader.SecurityHeaderSupplier; import de.caritas.cob.statisticsservice.api.service.securityheader.TenantHeaderSupplier; +import de.caritas.cob.statisticsservice.config.apiclient.UserStatisticsApiControllerFactory; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.UserStatisticsControllerApi; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpHeaders; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) -public class UserStatisticsServiceTest { +@ExtendWith(MockitoExtension.class) +class UserStatisticsServiceTest { @InjectMocks UserStatisticsService userStatisticsService; @@ -29,8 +32,16 @@ public class UserStatisticsServiceTest { @Mock TenantHeaderSupplier tenantHeaderSupplier; + @Mock + UserStatisticsApiControllerFactory userStatisticsApiControllerFactory; + + @BeforeEach + void setup() { + when(userStatisticsApiControllerFactory.createControllerApi()).thenReturn(userStatisticsControllerApi); + } + @Test - public void retrieveSessionViaRcGroupId_Should_RetrieveSessionViaUserStatisticsControllerApi() { + void retrieveSessionViaRcGroupId_Should_RetrieveSessionViaUserStatisticsControllerApi() { var headers = new HttpHeaders(); when(securityHeaderSupplier.getCsrfHttpHeaders()).thenReturn(headers); @@ -40,7 +51,7 @@ public void retrieveSessionViaRcGroupId_Should_RetrieveSessionViaUserStatisticsC } @Test - public void retrieveSessionViaSessionId_Should_RetrieveSessionViaUserStatisticsControllerApi() { + void retrieveSessionViaSessionId_Should_RetrieveSessionViaUserStatisticsControllerApi() { var headers = new HttpHeaders(); when(securityHeaderSupplier.getCsrfHttpHeaders()).thenReturn(headers); diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/service/securityheader/TenantHeaderSupplierTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/service/securityheader/TenantHeaderSupplierTest.java index 6ad62d7..cd7373e 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/service/securityheader/TenantHeaderSupplierTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/service/securityheader/TenantHeaderSupplierTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; -import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpHeaders; import org.springframework.test.util.ReflectionTestUtils; diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java index 7e57a23..af89bbe 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/ArchiveOrDeleteSessionListenerTest.java @@ -18,17 +18,18 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.ArchiveMetaData; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + import java.time.OffsetDateTime; -import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) public class ArchiveOrDeleteSessionListenerTest { @InjectMocks diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/AssignSessionListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/AssignSessionListenerTest.java index 8aeb246..58cfcc8 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/AssignSessionListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/AssignSessionListenerTest.java @@ -7,14 +7,15 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; import org.apache.commons.lang3.RandomStringUtils; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.time.OffsetDateTime; import java.util.Map; @@ -32,8 +33,8 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@RunWith(SpringRunner.class) -public class AssignSessionListenerTest { +@ExtendWith(SpringExtension.class) +class AssignSessionListenerTest { @InjectMocks AssignSessionListener assignSessionListener; @@ -42,7 +43,7 @@ public class AssignSessionListenerTest { @Captor ArgumentCaptor statisticsEventCaptor; @Test - public void receiveMessage_Should_saveFullEventToMongoDb() { + void receiveMessage_Should_saveFullEventToMongoDb() { // given SessionStatisticsResultDTO sessionStatisticsResultDTO = buildResultDto(); when(userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)) @@ -77,7 +78,7 @@ public void receiveMessage_Should_saveFullEventToMongoDb() { } @Test - public void receiveMessage_Should_savePartialEventToMongoDb() { + void receiveMessage_Should_savePartialEventToMongoDb() { var sessionStatisticsResultDTO = buildResultDto(); when(userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)) .thenReturn(sessionStatisticsResultDTO); diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListenerTest.java index 70c2802..5bef18d 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/CreateMessageListenerTest.java @@ -17,17 +17,18 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.CreateMessageMetaData; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + import java.time.OffsetDateTime; -import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) public class CreateMessageListenerTest { @InjectMocks diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java index 25273ed..8d44a6c 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/RegistrationListenerTest.java @@ -18,18 +18,19 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.RegistrationMetaData; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + import java.time.OffsetDateTime; import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) public class RegistrationListenerTest { @InjectMocks diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListenerTest.java index 33fbd25..3c86644 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StartVideoCallListenerTest.java @@ -8,14 +8,14 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.StartVideoCallMetaData; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.VideoCallStatus; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; @@ -32,7 +32,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) public class StartVideoCallListenerTest { @InjectMocks diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StopVideoCallListenerTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StopVideoCallListenerTest.java index 9a46404..7ccfbf5 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StopVideoCallListenerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/listener/StopVideoCallListenerTest.java @@ -8,9 +8,9 @@ import static de.caritas.cob.statisticsservice.api.testhelper.TestConstants.VIDEO_CALL_UUID; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; -import static org.powermock.api.mockito.PowerMockito.when; import de.caritas.cob.statisticsservice.api.model.EventType; import de.caritas.cob.statisticsservice.api.model.StopVideoCallStatisticsEventMessage; @@ -21,14 +21,15 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.User; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.StartVideoCallMetaData; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.VideoCallStatus; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + import java.time.Duration; import java.time.OffsetDateTime; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.junit.Test; -import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.InjectMocks; @@ -37,9 +38,9 @@ import org.springframework.amqp.AmqpException; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; -import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) public class StopVideoCallListenerTest { @InjectMocks @@ -52,7 +53,7 @@ public class StopVideoCallListenerTest { @Test public void receiveMessage_Should_saveEventToMongoDb() { // given - when(mongoTemplate.find(Mockito.any(Query.class), eq(StatisticsEvent.class))) + Mockito.when(mongoTemplate.find(Mockito.any(Query.class), Mockito.eq(StatisticsEvent.class))) .thenReturn(buildMongoResultWithOneStatisticsEvent()); StopVideoCallStatisticsEventMessage stopVideoCallStatisticsEventMessage = buildEventMessage(); @@ -91,28 +92,32 @@ public void receiveMessage_Should_saveEventToMongoDb() { assertThat(startVideoCallMetaData.getDuration(), is(duration)); } - @Test(expected = AmqpException.class) + @Test public void receiveMessage_Should_ThrowAmqpException_WhenMoreThanOneStartVideoCallEventWasFound() { - // given - when(mongoTemplate.find(Mockito.any(Query.class), eq(StatisticsEvent.class))) - .thenReturn(buildMongoResultWithTwoStatisticsEvent()); + assertThrows(AmqpException.class, () -> { + // given + Mockito.when(mongoTemplate.find(Mockito.any(Query.class), eq(StatisticsEvent.class))) + .thenReturn(buildMongoResultWithTwoStatisticsEvent()); - StopVideoCallStatisticsEventMessage stopVideoCallStatisticsEventMessage = buildEventMessage(); + StopVideoCallStatisticsEventMessage stopVideoCallStatisticsEventMessage = buildEventMessage(); - // when, then - stopVideoCallListener.receiveMessage(stopVideoCallStatisticsEventMessage); + // when, then + stopVideoCallListener.receiveMessage(stopVideoCallStatisticsEventMessage); + }); } - @Test(expected = AmqpException.class) + @Test public void receiveMessage_Should_ThrowAmqpException_WhenNoStartVideoCallEventWasFound() { - // given - when(mongoTemplate.find(Mockito.any(Query.class), eq(StatisticsEvent.class))) - .thenReturn(Collections.emptyList()); + assertThrows(AmqpException.class, () -> { + // given + Mockito.when(mongoTemplate.find(Mockito.any(Query.class), eq(StatisticsEvent.class))) + .thenReturn(Collections.emptyList()); - StopVideoCallStatisticsEventMessage stopVideoCallStatisticsEventMessage = buildEventMessage(); + StopVideoCallStatisticsEventMessage stopVideoCallStatisticsEventMessage = buildEventMessage(); - // when, then - stopVideoCallListener.receiveMessage(stopVideoCallStatisticsEventMessage); + // when, then + stopVideoCallListener.receiveMessage(stopVideoCallStatisticsEventMessage); + }); } public List buildMongoResultWithOneStatisticsEvent() { diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/model/StatisticsEventBuilderTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/model/StatisticsEventBuilderTest.java index 9736ce8..4b041c4 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/model/StatisticsEventBuilderTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/model/StatisticsEventBuilderTest.java @@ -7,10 +7,10 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEventBuilder; import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.meta.CreateMessageMetaData; import de.caritas.cob.statisticsservice.userstatisticsservice.generated.web.model.SessionStatisticsResultDTO; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; import java.time.Instant; @@ -23,85 +23,96 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class StatisticsEventBuilderTest { @Mock UserStatisticsService userStatisticsService; - @Test(expected = NullPointerException.class) + @Test public void build_Should_ThrowNullPointerException_WhenEventTypeIsNull() { - - StatisticsEventBuilder builder = - StatisticsEventBuilder.getInstance( - () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); - builder - .withTimestamp(Instant.now()) - .withUserId(CONSULTANT_ID) - .withUserRole(UserRole.CONSULTANT) - .build(); + assertThrows(NullPointerException.class, () -> { + + StatisticsEventBuilder builder = + StatisticsEventBuilder.getInstance( + () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); + builder + .withTimestamp(Instant.now()) + .withUserId(CONSULTANT_ID) + .withUserRole(UserRole.CONSULTANT) + .build(); + }); } - @Test(expected = NullPointerException.class) + @Test public void build_Should_ThrowNullPointerException_WhenTimestampIsNull() { - - StatisticsEventBuilder builder = - StatisticsEventBuilder.getInstance( - () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); - builder - .withEventType(EventType.ASSIGN_SESSION) - .withUserId(CONSULTANT_ID) - .withUserRole(UserRole.CONSULTANT) - .build(); + assertThrows(NullPointerException.class, () -> { + + StatisticsEventBuilder builder = + StatisticsEventBuilder.getInstance( + () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); + builder + .withEventType(EventType.ASSIGN_SESSION) + .withUserId(CONSULTANT_ID) + .withUserRole(UserRole.CONSULTANT) + .build(); + }); } - @Test(expected = NullPointerException.class) + @Test public void build_Should_ThrowNullPointerException_WhenUserIdIsNull() { - - StatisticsEventBuilder builder = - StatisticsEventBuilder.getInstance( - () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); - builder - .withEventType(EventType.ASSIGN_SESSION) - .withTimestamp(Instant.now()) - .withUserRole(UserRole.CONSULTANT) - .build(); + assertThrows(NullPointerException.class, () -> { + + StatisticsEventBuilder builder = + StatisticsEventBuilder.getInstance( + () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); + builder + .withEventType(EventType.ASSIGN_SESSION) + .withTimestamp(Instant.now()) + .withUserRole(UserRole.CONSULTANT) + .build(); + }); } - @Test(expected = NullPointerException.class) + @Test public void build_Should_ThrowNullPointerException_WhenUserRoleIsNull() { - - StatisticsEventBuilder builder = - StatisticsEventBuilder.getInstance( - () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); - builder - .withEventType(EventType.ASSIGN_SESSION) - .withTimestamp(Instant.now()) - .withUserId(CONSULTANT_ID) - .build(); + assertThrows(NullPointerException.class, () -> { + + StatisticsEventBuilder builder = + StatisticsEventBuilder.getInstance( + () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); + builder + .withEventType(EventType.ASSIGN_SESSION) + .withTimestamp(Instant.now()) + .withUserId(CONSULTANT_ID) + .build(); + }); } - @Test(expected = NullPointerException.class) + @Test public void build_Should_ThrowNullPointerException_WhenRetrievedSessionHasNoId() { - - SessionStatisticsResultDTO session = buildSessionStatisticsResultDto(); - session.id(null); - - when(userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)) - .thenReturn(session); - - StatisticsEventBuilder builder = - StatisticsEventBuilder.getInstance( - () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); - builder - .withEventType(EventType.ASSIGN_SESSION) - .withTimestamp(Instant.now()) - .withUserId(CONSULTANT_ID) - .withUserRole(UserRole.CONSULTANT) - .build(); + assertThrows(NullPointerException.class, () -> { + + SessionStatisticsResultDTO session = buildSessionStatisticsResultDto(); + session.id(null); + + when(userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)) + .thenReturn(session); + + StatisticsEventBuilder builder = + StatisticsEventBuilder.getInstance( + () -> userStatisticsService.retrieveSessionViaSessionId(SESSION_ID)); + builder + .withEventType(EventType.ASSIGN_SESSION) + .withTimestamp(Instant.now()) + .withUserId(CONSULTANT_ID) + .withUserRole(UserRole.CONSULTANT) + .build(); + }); } @Test @@ -185,15 +196,17 @@ public void buildShouldNotRequestSessionFromUserServiceOnStartVideoCallEvent() { verifyNoInteractions(userStatisticsService); } - @Test(expected = IllegalArgumentException.class) + @Test public void buildShouldIllegalArgExceptionOnMissingSessionAndNotStartVideoCallEvent() { - StatisticsEventBuilder.getInstance() - .withEventType(EventType.ASSIGN_SESSION) - .withTimestamp(Instant.now()) - .withUserId(CONSULTANT_ID) - .withUserRole(UserRole.CONSULTANT) - .withMetaData(new Object()) - .build(); + assertThrows(IllegalArgumentException.class, () -> { + StatisticsEventBuilder.getInstance() + .withEventType(EventType.ASSIGN_SESSION) + .withTimestamp(Instant.now()) + .withUserId(CONSULTANT_ID) + .withUserRole(UserRole.CONSULTANT) + .withMetaData(new Object()) + .build(); + }); } private SessionStatisticsResultDTO buildSessionStatisticsResultDto() { diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepositoryIT.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepositoryIT.java index 12d1695..733a7e1 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepositoryIT.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventRepositoryIT.java @@ -22,24 +22,22 @@ import java.time.ZoneOffset; import java.util.List; import org.bson.Document; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; @DataMongoTest() @ContextConfiguration(classes = StatisticsServiceApplication.class) -@RunWith(SpringRunner.class) @TestPropertySource(properties = "spring.profiles.active=testing") public class StatisticsEventRepositoryIT { + public static final String MONGODB_STATISTICS_EVENTS_JSON_FILENAME = "mongodb/StatisticsEvents.json"; private final Instant dateFromConverted = @@ -52,7 +50,7 @@ public class StatisticsEventRepositoryIT { @Autowired MongoTemplate mongoTemplate; - @Before + @BeforeEach public void preFillMongoDb() throws IOException { mongoTemplate.dropCollection(MONGO_COLLECTION_NAME); ObjectMapper objectMapper = new ObjectMapper(); @@ -139,7 +137,7 @@ public void getAllArchiveSessionEvents_Should_ReturnArchiveSessionEvents() { } @Test - @Ignore("For some reason this test is failing in this test scenario caused by the event.0.startTime and event.0.endTime filters.") + @Disabled("For some reason this test is failing in this test scenario caused by the event.0.startTime and event.0.endTime filters.") public void calculateNumberOfDoneAppointmentsForConsultant_Should_ReturnCorrectNumberOfAppointments() { Count count = statisticsEventRepository.calculateNumbersOfDoneAppointments(CONSULTANT_ID, dateFromConverted, dateToConverted, dateToConverted); diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepositoryIT.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepositoryIT.java index 3b47815..dfb49c7 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepositoryIT.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/repository/StatisticsEventTenantAwareRepositoryIT.java @@ -10,20 +10,17 @@ import de.caritas.cob.statisticsservice.api.statistics.model.statisticsevent.StatisticsEvent; import java.io.IOException; import java.util.List; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; import org.springframework.core.io.ClassPathResource; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringRunner; @DataMongoTest() @ContextConfiguration(classes = StatisticsServiceApplication.class) -@RunWith(SpringRunner.class) @TestPropertySource(properties = "spring.profiles.active=testing") @TestPropertySource(properties = "multitenancy.enabled=true") public class StatisticsEventTenantAwareRepositoryIT { @@ -40,7 +37,7 @@ public class StatisticsEventTenantAwareRepositoryIT { @Autowired MongoTemplate mongoTemplate; - @Before + @BeforeEach public void preFillMongoDb() throws IOException { mongoTemplate.dropCollection(MONGO_COLLECTION_NAME); ObjectMapper objectMapper = new ObjectMapper(); diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java index 5129040..5641b17 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/RegistrationStatisticsServiceTest.java @@ -7,7 +7,6 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyList; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; diff --git a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/StatisticsServiceTest.java b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/StatisticsServiceTest.java index 329af89..e56e4c4 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/StatisticsServiceTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/api/statistics/service/StatisticsServiceTest.java @@ -5,6 +5,7 @@ import static de.caritas.cob.statisticsservice.api.testhelper.TestConstants.DATE_TO; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; @@ -17,18 +18,19 @@ import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventRepository; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventRepository.Count; import de.caritas.cob.statisticsservice.api.statistics.repository.StatisticsEventRepository.Duration; +import org.junit.jupiter.api.Test; + import java.time.Instant; import java.time.LocalDate; import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; -@RunWith(MockitoJUnitRunner.class) +@ExtendWith(MockitoExtension.class) public class StatisticsServiceTest { @InjectMocks @@ -38,9 +40,11 @@ public class StatisticsServiceTest { @Mock AuthenticatedUser authenticatedUser; - @Test(expected = BadRequestException.class) + @Test public void fetchStatisticsData_Should_ThrowBadRequestException_WhenDateFromIsAfterDateTo() { - statisticsService.fetchStatisticsData(DATE_TO, DATE_FROM); + assertThrows(BadRequestException.class, () -> { + statisticsService.fetchStatisticsData(DATE_TO, DATE_FROM); + }); } @Test diff --git a/src/test/java/de/caritas/cob/statisticsservice/config/resttemplate/CustomResponseErrorHandlerTest.java b/src/test/java/de/caritas/cob/statisticsservice/config/resttemplate/CustomResponseErrorHandlerTest.java index 60432bc..0cbf7a8 100644 --- a/src/test/java/de/caritas/cob/statisticsservice/config/resttemplate/CustomResponseErrorHandlerTest.java +++ b/src/test/java/de/caritas/cob/statisticsservice/config/resttemplate/CustomResponseErrorHandlerTest.java @@ -2,13 +2,14 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.net.URI; -import org.junit.Test; + +import org.junit.jupiter.api.Test; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; From e40c740e82cab079e7985dc2d798214369189263 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 23 May 2024 16:56:30 +0200 Subject: [PATCH 2/8] feat: upgrade to spring boot 2.7.X --- .../cob/statisticsservice/StatisticsServiceApplication.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java b/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java index ab78a20..184e804 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java +++ b/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java @@ -7,6 +7,10 @@ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class StatisticsServiceApplication { + static { + System.setProperty("os.arch", "i686_64"); + } + public static void main(String[] args) { SpringApplication.run(StatisticsServiceApplication.class, args); } From ba0fcebe22142100b8f3a4d3ff1c2726a82148c6 Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Thu, 23 May 2024 17:01:16 +0200 Subject: [PATCH 3/8] feat: upgrade to spring boot 2.7.X --- .github/workflows/dockerImage.yml | 2 +- .../cob/statisticsservice/StatisticsServiceApplication.java | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/.github/workflows/dockerImage.yml b/.github/workflows/dockerImage.yml index c14a636..2b1adfc 100644 --- a/.github/workflows/dockerImage.yml +++ b/.github/workflows/dockerImage.yml @@ -64,7 +64,7 @@ jobs: push_to_registry: strategy: matrix: - registry: ["docker.pkg.github.com", "ghcr.io"] + registry: ["ghcr.io"] needs: [test] name: Push Docker image to GitHub Packages runs-on: ubuntu-latest diff --git a/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java b/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java index 184e804..39c5405 100644 --- a/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java +++ b/src/main/java/de/caritas/cob/statisticsservice/StatisticsServiceApplication.java @@ -6,11 +6,6 @@ @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class StatisticsServiceApplication { - - static { - System.setProperty("os.arch", "i686_64"); - } - public static void main(String[] args) { SpringApplication.run(StatisticsServiceApplication.class, args); } From 85a9a49bb903df6d3a9093bfcb6961f1331e9a4c Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Fri, 24 May 2024 10:15:47 +0200 Subject: [PATCH 4/8] feat: upgrade to spring boot 2.7.X --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index a01dd93..1a14054 100644 --- a/pom.xml +++ b/pom.xml @@ -199,6 +199,12 @@ 4.13.0 + + commons-io + commons-io + 2.16.1 + + org.jeasy From 2b69b3ee2e9251f6107c5f444d449966d410462c Mon Sep 17 00:00:00 2001 From: tkuzynow Date: Fri, 26 Jul 2024 16:47:26 +0200 Subject: [PATCH 5/8] feat: integrate with spring cloud sleuth and zipkin --- pom.xml | 23 +++++++++++++++++++++++ src/main/resources/application.properties | 7 ++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1a14054..d5e6e40 100644 --- a/pom.xml +++ b/pom.xml @@ -20,6 +20,18 @@ + + + + org.springframework.cloud + spring-cloud-dependencies + 2021.0.8 + pom + import + + + + UTF-8 UTF-8 @@ -76,6 +88,17 @@ hibernate-validator + + org.springframework.cloud + spring-cloud-starter-sleuth + 3.1.11 + + + org.springframework.cloud + spring-cloud-starter-zipkin + 2.2.8.RELEASE + + org.openapitools diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4d07972..f0c93e2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,6 +7,7 @@ spring.main.allow-bean-definition-overriding=true spring.jpa.open-in-view=true spring.jpa.hibernate.ddl-auto=none spring.data.jpa.repositories.bootstrap-mode=default +spring.application.name=statistics-service # General app settings server.port=8080 @@ -77,4 +78,8 @@ management.endpoint.health.show-details=never management.endpoints.web.exposure.include=health management.endpoint.health.probes.enabled=true management.metrics.mongo.command.enabled=false -management.metrics.mongo.connectionpool.enabled=false \ No newline at end of file +management.metrics.mongo.connectionpool.enabled=false + +spring.zipkin.baseUrl= +spring.sleuth.sampler.percentage=1.0 +spring.zipkin.sender.type=web \ No newline at end of file From 3cd923f6c63231a8fcd2bcdc565c97c7009c854e Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Fri, 16 Aug 2024 14:22:36 +0200 Subject: [PATCH 6/8] fix: CARITAS-243 - remove initializeFeedbackChat and isPeerChat from consultingtypeservice.yaml --- services/consultingtypeservice.yaml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/services/consultingtypeservice.yaml b/services/consultingtypeservice.yaml index 00d1cd7..11878f7 100644 --- a/services/consultingtypeservice.yaml +++ b/services/consultingtypeservice.yaml @@ -310,12 +310,6 @@ components: sessionDataInitializing: allOf: - $ref: '#/components/schemas/SessionDataInitializingDTO' - initializeFeedbackChat: - type: boolean - example: true - isPeerChat: - type: boolean - example: true roles: $ref: '#/components/schemas/RolesDTO' notifications: @@ -377,12 +371,6 @@ components: sendFurtherStepsMessage: type: boolean example: true - initializeFeedbackChat: - type: boolean - example: true - isPeerChat: - type: boolean - example: true furtherInformation: allOf: - $ref: '#/components/schemas/FurtherInformationDTO' From 352b5cb19aebe9be9c7edf7bc519bafbe5c18469 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Wed, 11 Sep 2024 16:58:36 +0200 Subject: [PATCH 7/8] fix: CARITAS-243 upgrade deprecated actions/upload-artifact --- .github/workflows/dockerImage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dockerImage.yml b/.github/workflows/dockerImage.yml index ec4013b..0e1c345 100644 --- a/.github/workflows/dockerImage.yml +++ b/.github/workflows/dockerImage.yml @@ -44,7 +44,7 @@ jobs: run: mvn -B -Pprod clean package -DskipTests - name: Maven Verify run: mvn -B -Pprod clean verify - - uses: actions/upload-artifact@v2 + - uses: actions/upload-artifact@v4 with: name: targetfiles path: target/*.jar From 9d7143bc74d04701e4bb7b452ec9b2088f2432d1 Mon Sep 17 00:00:00 2001 From: LeandroSilva Date: Wed, 11 Sep 2024 17:16:41 +0200 Subject: [PATCH 8/8] fix: CARITAS-243 * upgrade deprecated actions/download-artifact --- .github/workflows/dockerImage.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dockerImage.yml b/.github/workflows/dockerImage.yml index 0e1c345..0c51b31 100644 --- a/.github/workflows/dockerImage.yml +++ b/.github/workflows/dockerImage.yml @@ -75,7 +75,7 @@ jobs: steps: - uses: actions/checkout@v2 - name: Download buildfiles artifact - uses: actions/download-artifact@v2 + uses: actions/download-artifact@v4 with: name: targetfiles - name: Get current time