From 8b5c7a4d5865840313dd62cd165f4c5731c8692e Mon Sep 17 00:00:00 2001 From: Colin DAMON Date: Mon, 29 Aug 2022 11:17:55 +0200 Subject: [PATCH] Delete aop logging module --- .../AopLoggingApplicationService.java | 20 -- .../aop/domain/AopLoggingModuleFactory.java | 49 ----- .../AOPLoggingModuleConfiguration.java | 25 --- .../springboot/logging/aop/package-info.java | 2 - .../logging/aop/LoggingAspect.java.mustache | 107 --------- .../LoggingAspectConfiguration.java.mustache | 17 -- .../aop/LoggingAspectTest.java.mustache | 207 ------------------ .../domain/AopLoggingModuleFactoryTest.java | 54 ----- 8 files changed, 481 deletions(-) delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/application/AopLoggingApplicationService.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactory.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/infrastructure/primary/AOPLoggingModuleConfiguration.java delete mode 100644 src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/package-info.java delete mode 100644 src/main/resources/generator/server/springboot/logging/aop/LoggingAspect.java.mustache delete mode 100644 src/main/resources/generator/server/springboot/logging/aop/LoggingAspectConfiguration.java.mustache delete mode 100644 src/main/resources/generator/server/springboot/logging/aop/LoggingAspectTest.java.mustache delete mode 100644 src/test/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactoryTest.java diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/application/AopLoggingApplicationService.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/application/AopLoggingApplicationService.java deleted file mode 100644 index 2ed7de51dee..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/application/AopLoggingApplicationService.java +++ /dev/null @@ -1,20 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.logging.aop.application; - -import org.springframework.stereotype.Service; -import tech.jhipster.lite.generator.server.springboot.logging.aop.domain.AopLoggingModuleFactory; -import tech.jhipster.lite.module.domain.JHipsterModule; -import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; - -@Service -public class AopLoggingApplicationService { - - private final AopLoggingModuleFactory factory; - - public AopLoggingApplicationService() { - factory = new AopLoggingModuleFactory(); - } - - public JHipsterModule buildModule(JHipsterModuleProperties properties) { - return factory.buildModule(properties); - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactory.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactory.java deleted file mode 100644 index aa60baaa49a..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactory.java +++ /dev/null @@ -1,49 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.logging.aop.domain; - -import static tech.jhipster.lite.module.domain.JHipsterModule.*; - -import tech.jhipster.lite.error.domain.Assert; -import tech.jhipster.lite.module.domain.JHipsterModule; -import tech.jhipster.lite.module.domain.file.JHipsterSource; -import tech.jhipster.lite.module.domain.javaproperties.PropertyKey; -import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; - -public class AopLoggingModuleFactory { - - private static final JHipsterSource SOURCE = from("server/springboot/logging/aop"); - private static final String AOP_FOLDER = "technical/infrastructure/secondary/aop/logging"; - private static final PropertyKey AOP_LOG_PROPERTY = propertyKey("application.aop.logging"); - - public JHipsterModule buildModule(JHipsterModuleProperties properties) { - Assert.notNull("properties", properties); - - String packagePath = properties.packagePath(); - - //@formatter:off - return moduleBuilder(properties) - .javaDependencies() - .addDependency(groupId("org.springframework.boot"), artifactId("spring-boot-starter-aop")) - .and() - .files() - .batch(SOURCE, toSrcMainJava().append(packagePath).append(AOP_FOLDER)) - .addTemplate("LoggingAspectConfiguration.java") - .addTemplate("LoggingAspect.java") - .and() - .add( - SOURCE.template("LoggingAspectTest.java"), - toSrcTestJava().append(packagePath).append(AOP_FOLDER).append("LoggingAspectTest.java") - ) - .and() - .springMainProperties() - .set(AOP_LOG_PROPERTY, propertyValue("false")) - .and() - .springMainProperties(springProfile("local")) - .set(AOP_LOG_PROPERTY, propertyValue("true")) - .and() - .springTestProperties() - .set(AOP_LOG_PROPERTY, propertyValue("true")) - .and() - .build(); - //@formatter:on - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/infrastructure/primary/AOPLoggingModuleConfiguration.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/infrastructure/primary/AOPLoggingModuleConfiguration.java deleted file mode 100644 index a5411deeda2..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/infrastructure/primary/AOPLoggingModuleConfiguration.java +++ /dev/null @@ -1,25 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.logging.aop.infrastructure.primary; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import tech.jhipster.lite.generator.server.springboot.logging.aop.application.AopLoggingApplicationService; -import tech.jhipster.lite.module.domain.resource.JHipsterModuleApiDoc; -import tech.jhipster.lite.module.domain.resource.JHipsterModuleOrganization; -import tech.jhipster.lite.module.domain.resource.JHipsterModulePropertiesDefinition; -import tech.jhipster.lite.module.domain.resource.JHipsterModuleResource; - -@Configuration -class AOPLoggingModuleConfiguration { - - @Bean - JHipsterModuleResource aopLoggingModule(AopLoggingApplicationService aopLogging) { - return JHipsterModuleResource - .builder() - .slug("aop-logging") - .propertiesDefinition(JHipsterModulePropertiesDefinition.builder().addBasePackage().addProjectName().addIndentation().build()) - .apiDoc(new JHipsterModuleApiDoc("Spring Boot - Logging", "Add AOP logging")) - .organization(JHipsterModuleOrganization.SPRINGBOOT_DEPENDENCY) - .tags("server", "spring", "spring-boot", "log") - .factory(aopLogging::buildModule); - } -} diff --git a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/package-info.java b/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/package-info.java deleted file mode 100644 index ddd25230919..00000000000 --- a/src/main/java/tech/jhipster/lite/generator/server/springboot/logging/aop/package-info.java +++ /dev/null @@ -1,2 +0,0 @@ -@tech.jhipster.lite.BusinessContext -package tech.jhipster.lite.generator.server.springboot.logging.aop; diff --git a/src/main/resources/generator/server/springboot/logging/aop/LoggingAspect.java.mustache b/src/main/resources/generator/server/springboot/logging/aop/LoggingAspect.java.mustache deleted file mode 100644 index 837727c4868..00000000000 --- a/src/main/resources/generator/server/springboot/logging/aop/LoggingAspect.java.mustache +++ /dev/null @@ -1,107 +0,0 @@ -package {{packageName}}.technical.infrastructure.secondary.aop.logging; - -import java.util.Arrays; -import org.aspectj.lang.JoinPoint; -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.annotation.AfterThrowing; -import org.aspectj.lang.annotation.Around; -import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; - -/** - * Aspect for logging execution of service and repository Spring components. - * - * By default, it only runs with the "dev" profile. - */ -@Aspect -public class LoggingAspect { - - @Value("${application.aop.logging:false}") - private boolean logging; - - /** - * Pointcut that matches all repositories, services and Web REST endpoints. - */ - @Pointcut( - "within(@org.springframework.stereotype.Repository *)" + - " || within(@org.springframework.stereotype.Service *)" + - " || within(@org.springframework.web.bind.annotation.RestController *)" - ) - public void springBeanPointcut() { - // Method is empty as this is just a Pointcut, the implementations are in the advices. - } - - /** - * Pointcut that matches all Spring beans in the application's main packages. - */ - @Pointcut( - "within({{packageName}}..*.primary..*)" + - " || within({{packageName}}..*.application..*)" + - " || within({{packageName}}..*.infrastructure..*)" - ) - public void applicationPackagePointcut() { - // Method is empty as this is just a Pointcut, the implementations are in the advices. - } - - /** - * Retrieves the {@link Logger} associated to the given {@link JoinPoint}. - * - * @param joinPoint join point we want the logger for. - * @return {@link Logger} associated to the given {@link JoinPoint}. - */ - protected Logger logger(JoinPoint joinPoint) { - return LoggerFactory.getLogger(joinPoint.getSignature().getDeclaringTypeName()); - } - - /** - * Advice that logs methods throwing exceptions. - * - * @param joinPoint join point for advice. - * @param e exception. - */ - @AfterThrowing(pointcut = "applicationPackagePointcut() && springBeanPointcut()", throwing = "e") - public void logAfterThrowing(JoinPoint joinPoint, Throwable e) { - if (logging) { - logger(joinPoint) - .error( - "Exception in {}() with cause = '{}' and exception = '{}'", - joinPoint.getSignature().getName(), - e.getCause() != null ? e.getCause() : "NULL", - e.getMessage(), - e - ); - } else { - logger(joinPoint) - .error("Exception in {}() with cause = {}", joinPoint.getSignature().getName(), e.getCause() != null ? e.getCause() : "NULL"); - } - } - - /** - * Advice that logs when a method is entered and exited. - * - * @param joinPoint join point for advice. - * @return result. - * @throws Throwable throws {@link IllegalArgumentException}. - */ - @Around("applicationPackagePointcut() && springBeanPointcut()") - public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable { - Logger log = logger(joinPoint); - if (log.isDebugEnabled()) { - log.debug("Enter: {}() with argument[s] = {}", joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs())); - } - - try { - Object result = joinPoint.proceed(); - if (log.isDebugEnabled()) { - log.debug("Exit: {}() with result = {}", joinPoint.getSignature().getName(), result); - } - return result; - } catch (IllegalArgumentException e) { - log.error("Illegal argument: {} in {}()", Arrays.toString(joinPoint.getArgs()), joinPoint.getSignature().getName()); - throw e; - } - } -} diff --git a/src/main/resources/generator/server/springboot/logging/aop/LoggingAspectConfiguration.java.mustache b/src/main/resources/generator/server/springboot/logging/aop/LoggingAspectConfiguration.java.mustache deleted file mode 100644 index fd9c47d9c0c..00000000000 --- a/src/main/resources/generator/server/springboot/logging/aop/LoggingAspectConfiguration.java.mustache +++ /dev/null @@ -1,17 +0,0 @@ -package {{packageName}}.technical.infrastructure.secondary.aop.logging; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.EnableAspectJAutoProxy; - -@Configuration -@EnableAspectJAutoProxy -public class LoggingAspectConfiguration { - - @Bean - @ConditionalOnProperty("application.aop.logging") - public LoggingAspect loggingAspect() { - return new LoggingAspect(); - } -} diff --git a/src/main/resources/generator/server/springboot/logging/aop/LoggingAspectTest.java.mustache b/src/main/resources/generator/server/springboot/logging/aop/LoggingAspectTest.java.mustache deleted file mode 100644 index 39a802eb3a8..00000000000 --- a/src/main/resources/generator/server/springboot/logging/aop/LoggingAspectTest.java.mustache +++ /dev/null @@ -1,207 +0,0 @@ -package {{packageName}}.technical.infrastructure.secondary.aop.logging; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.aspectj.lang.ProceedingJoinPoint; -import org.aspectj.lang.Signature; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.Mock; -import org.mockito.MockedStatic; -import org.mockito.junit.jupiter.MockitoExtension; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.test.util.ReflectionTestUtils; -import {{packageName}}.UnitTest; - -@UnitTest -@ExtendWith(MockitoExtension.class) -class LoggingAspectTest { - - @Mock - private Logger logger; - - @Mock - private ProceedingJoinPoint joinPoint; - - @Mock - private Signature signature; - - private LoggingAspect loggingAspect; - - private static String signatureTypeName = "signature type name"; - private static String signatureName = "signature name"; - - private static String exceptionMessage = "exception message"; - private static String exceptionCause = "exception cause"; - private static String exceptionFormatComplete = "Exception in {}() with cause = '{}' and exception = '{}'"; - private static String exceptionFormat = "Exception in {}() with cause = {}"; - - private static String enterMessage = "Enter: {}() with argument[s] = {}"; - private static String exitMessage = "Exit: {}() with result = {}"; - private static String illegalMessage = "Illegal argument: {} in {}()"; - - private static String joinArgs = "join args"; - - @BeforeEach - void setup() { - loggingAspect = new LoggingAspect(); - } - - private void initMocks() { - when(joinPoint.getSignature()).thenReturn(signature); - when(signature.getDeclaringTypeName()).thenReturn(signatureTypeName); - } - - @Test - void shouldGetLogger() { - initMocks(); - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - - loggingAspect.logger(joinPoint); - - verify(joinPoint).getSignature(); - verify(signature).getDeclaringTypeName(); - loggerFactory.verify(() -> LoggerFactory.getLogger(signatureTypeName)); - } - } - - @Test - void shouldLogAfterWithLoggingTrueAndExceptionCause() { - initMocks(); - when(signature.getName()).thenReturn(signatureName); - ReflectionTestUtils.setField(loggingAspect, "logging", true); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - Exception exception = new Exception(exceptionMessage, new Exception(exceptionCause)); - - loggingAspect.logAfterThrowing(joinPoint, exception); - - verify(logger).error(exceptionFormatComplete, signatureName, exception.getCause(), exceptionMessage, exception); - } - } - - @Test - void shouldLogAfterWithLoggingTrueAndNoExceptionCause() { - initMocks(); - when(signature.getName()).thenReturn(signatureName); - ReflectionTestUtils.setField(loggingAspect, "logging", true); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - Exception exception = new Exception(exceptionMessage); - - loggingAspect.logAfterThrowing(joinPoint, exception); - - verify(logger).error(exceptionFormatComplete, signatureName, "NULL", exceptionMessage, exception); - } - } - - @Test - void shouldLogAfterWithLoggingFalseAndExceptionCause() { - initMocks(); - when(signature.getName()).thenReturn(signatureName); - ReflectionTestUtils.setField(loggingAspect, "logging", false); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - Exception exception = new Exception(exceptionMessage, new Exception(exceptionCause)); - - loggingAspect.logAfterThrowing(joinPoint, exception); - - verify(logger).error(exceptionFormat, signatureName, exception.getCause()); - } - } - - @Test - void shouldLogAfterWithLoggingFalseAndNoExceptionCause() { - initMocks(); - when(signature.getName()).thenReturn(signatureName); - ReflectionTestUtils.setField(loggingAspect, "logging", false); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - Exception exception = new Exception(exceptionMessage); - - loggingAspect.logAfterThrowing(joinPoint, exception); - - verify(logger).error(exceptionFormat, signatureName, "NULL"); - } - } - - @Test - void shouldLogAroundWithDebug() throws Throwable { - initMocks(); - when(logger.isDebugEnabled()).thenReturn(true); - when(signature.getName()).thenReturn(signatureName); - String proceedResult = "proceed result"; - when(joinPoint.proceed()).thenReturn(proceedResult); - when(joinPoint.getArgs()).thenReturn(new String[] { joinArgs }); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - - Object result = loggingAspect.logAround(joinPoint); - assertThat(result).isEqualTo(proceedResult); - - verify(logger).debug(enterMessage, signatureName, "[" + joinArgs + "]"); - verify(logger).debug(exitMessage, signatureName, proceedResult); - } - } - - @Test - void shouldLogAroundWithoutDebug() throws Throwable { - initMocks(); - when(logger.isDebugEnabled()).thenReturn(false); - String proceedResult = "proceed result"; - when(joinPoint.proceed()).thenReturn(proceedResult); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - - Object result = loggingAspect.logAround(joinPoint); - assertThat(result).isEqualTo(proceedResult); - - verify(logger, never()).debug(anyString(), any(Object.class), any(Object.class)); - } - } - - @Test - void shouldLogAroundWitProceedException() throws Throwable { - initMocks(); - when(logger.isDebugEnabled()).thenReturn(false); - when(signature.getName()).thenReturn(signatureName); - when(joinPoint.proceed()).thenThrow(IllegalArgumentException.class); - when(joinPoint.getArgs()).thenReturn(new String[] { joinArgs }); - - try (MockedStatic loggerFactory = mockStatic(LoggerFactory.class)) { - loggerFactory.when(() -> LoggerFactory.getLogger(signatureTypeName)).thenReturn(logger); - - assertThatThrownBy(() -> loggingAspect.logAround(joinPoint)).isExactlyInstanceOf(IllegalArgumentException.class); - - verify(logger, never()).debug(anyString(), any(Object.class), any(Object.class)); - verify(logger).error(illegalMessage, "[" + joinArgs + "]", signatureName); - } - } - - @Test - void shouldSpringBeanPoincut() { - assertThatCode(() -> loggingAspect.springBeanPointcut()).doesNotThrowAnyException(); - } - - @Test - void shouldApplicationPoincut() { - assertThatCode(() -> loggingAspect.applicationPackagePointcut()).doesNotThrowAnyException(); - } -} diff --git a/src/test/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactoryTest.java b/src/test/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactoryTest.java deleted file mode 100644 index 280ad2b8f45..00000000000 --- a/src/test/java/tech/jhipster/lite/generator/server/springboot/logging/aop/domain/AopLoggingModuleFactoryTest.java +++ /dev/null @@ -1,54 +0,0 @@ -package tech.jhipster.lite.generator.server.springboot.logging.aop.domain; - -import static tech.jhipster.lite.module.infrastructure.secondary.JHipsterModulesAssertions.*; - -import org.junit.jupiter.api.Test; -import tech.jhipster.lite.TestFileUtils; -import tech.jhipster.lite.UnitTest; -import tech.jhipster.lite.module.domain.JHipsterModule; -import tech.jhipster.lite.module.domain.JHipsterModulesFixture; -import tech.jhipster.lite.module.domain.properties.JHipsterModuleProperties; - -@UnitTest -class AopLoggingModuleFactoryTest { - - private static final AopLoggingModuleFactory factory = new AopLoggingModuleFactory(); - - @Test - void shouldBuildModule() { - JHipsterModuleProperties properties = JHipsterModulesFixture - .propertiesBuilder(TestFileUtils.tmpDirForTest()) - .basePackage("com.jhipster.test") - .projectBaseName("myapp") - .build(); - - JHipsterModule module = factory.buildModule(properties); - - assertThatModuleWithFiles(module, pomFile()) - .createFile("pom.xml") - .containing( - """ - - org.springframework.boot - spring-boot-starter-aop - - """ - ) - .and() - .createPrefixedFiles( - "src/main/java/com/jhipster/test/technical/infrastructure/secondary/aop/logging", - "LoggingAspectConfiguration.java", - "LoggingAspect.java" - ) - .createFiles("src/test/java/com/jhipster/test/technical/infrastructure/secondary/aop/logging/LoggingAspectTest.java") - .createFile("src/main/resources/config/application.properties") - .containing("application.aop.logging=false") - .and() - .createFile("src/main/resources/config/application-local.properties") - .containing("application.aop.logging=true") - .and() - .createFile("src/test/resources/config/application.properties") - .containing("application.aop.logging=true") - .and(); - } -}