diff --git a/src/main/java/play/pluv/PluvApplication.java b/src/main/java/play/pluv/PluvApplication.java index 0fb5347..239920a 100644 --- a/src/main/java/play/pluv/PluvApplication.java +++ b/src/main/java/play/pluv/PluvApplication.java @@ -3,8 +3,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication +@EnableJpaAuditing(dateTimeProviderRef = "localDateTimeProvider") @ConfigurationPropertiesScan public class PluvApplication { diff --git a/src/main/java/play/pluv/base/BaseEntity.java b/src/main/java/play/pluv/base/BaseEntity.java new file mode 100644 index 0000000..1192e51 --- /dev/null +++ b/src/main/java/play/pluv/base/BaseEntity.java @@ -0,0 +1,24 @@ +package play.pluv.base; + +import jakarta.persistence.Column; +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import java.time.LocalDateTime; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseEntity { + + @Column(updatable = false, nullable = false) + @CreatedDate + private LocalDateTime createdAt; + + @Column(nullable = false) + @LastModifiedDate + private LocalDateTime updatedAt; +} diff --git a/src/main/java/play/pluv/base/LocalDateTimeProvider.java b/src/main/java/play/pluv/base/LocalDateTimeProvider.java new file mode 100644 index 0000000..1dbfa55 --- /dev/null +++ b/src/main/java/play/pluv/base/LocalDateTimeProvider.java @@ -0,0 +1,18 @@ +package play.pluv.base; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.time.temporal.TemporalAccessor; +import java.util.Optional; +import org.springframework.data.auditing.DateTimeProvider; +import org.springframework.stereotype.Component; + +@Component +public class LocalDateTimeProvider implements DateTimeProvider { + + @Override + public Optional getNow() { + return Optional.of(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS)); + } + +} diff --git a/src/test/java/play/pluv/support/ApiTest.java b/src/test/java/play/pluv/support/ApiTest.java index 9fe2f1b..efc2485 100644 --- a/src/test/java/play/pluv/support/ApiTest.java +++ b/src/test/java/play/pluv/support/ApiTest.java @@ -4,30 +4,38 @@ import static org.springframework.restdocs.operation.preprocess.Preprocessors.prettyPrint; import com.fasterxml.jackson.databind.ObjectMapper; +import io.swagger.v3.oas.annotations.responses.ApiResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.RestDocumentationExtension; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; +import play.pluv.base.LocalDateTimeProvider; import play.pluv.login.application.JwtProvider; import play.pluv.login.application.LoginService; import play.pluv.music.application.MusicService; import play.pluv.oauth.application.OAuthService; import play.pluv.playlist.application.PlayListService; +import play.pluv.support.ApiTest.RestDocsResultConfig; @WebMvcTest -@Import(JwtProvider.class) +@Import({JwtProvider.class, RestDocsResultConfig.class}) @ExtendWith({RestDocumentationExtension.class, SpringExtension.class}) @DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) +@MockBean(JpaMetamodelMappingContext.class) public abstract class ApiTest { @Autowired @@ -54,4 +62,13 @@ void setUp( .withResponseDefaults(prettyPrint())) .build(); } + + @Configuration + static class RestDocsResultConfig { + + @Bean + public LocalDateTimeProvider localDateTimeProvider() { + return new LocalDateTimeProvider(); + } + } }