Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseEntity 추가 #69

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/play/pluv/PluvApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/play/pluv/base/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions src/main/java/play/pluv/base/LocalDateTimeProvider.java
Original file line number Diff line number Diff line change
@@ -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<TemporalAccessor> getNow() {
return Optional.of(LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS));
}

}
19 changes: 18 additions & 1 deletion src/test/java/play/pluv/support/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -54,4 +62,13 @@ void setUp(
.withResponseDefaults(prettyPrint()))
.build();
}

@Configuration
static class RestDocsResultConfig {

@Bean
public LocalDateTimeProvider localDateTimeProvider() {
return new LocalDateTimeProvider();
}
}
}
Loading