Skip to content

Commit

Permalink
merge: BaseEntity 추가
Browse files Browse the repository at this point in the history
BaseEntity 추가
  • Loading branch information
hong-sile authored Aug 8, 2024
2 parents 3987c3e + c467d86 commit 63cf8b3
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
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();
}
}
}

0 comments on commit 63cf8b3

Please sign in to comment.