From 07b2a4654573b57c9f3bfe3815011be9a9bc2792 Mon Sep 17 00:00:00 2001 From: greennarae Date: Tue, 10 Oct 2023 00:36:32 +0900 Subject: [PATCH 1/2] =?UTF-8?q?config:=20test=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EC=97=90=EC=84=9C=20lombok=20=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EA=B8=B0=EC=9C=84=ED=95=9C=20=EC=9D=98=EC=A1=B4=EC=84=B1=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animory/build.gradle | 2 ++ .../com/daggle/animory/common/config/JpaAuditConfiguration.java | 2 ++ .../main/java/com/daggle/animory/common/entity/BaseEntity.java | 2 ++ 3 files changed, 6 insertions(+) create mode 100644 animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java create mode 100644 animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java diff --git a/animory/build.gradle b/animory/build.gradle index 436b794a..142e3cb1 100644 --- a/animory/build.gradle +++ b/animory/build.gradle @@ -46,6 +46,8 @@ dependencies { // lombok compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' + testCompileOnly 'org.projectlombok:lombok' + testAnnotationProcessor 'org.projectlombok:lombok' // test testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java b/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java new file mode 100644 index 00000000..da85affc --- /dev/null +++ b/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java @@ -0,0 +1,2 @@ +package com.daggle.animory.common.config;public class JpaAuditConfiguration { +} diff --git a/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java b/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java new file mode 100644 index 00000000..8cb02b87 --- /dev/null +++ b/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java @@ -0,0 +1,2 @@ +package com.daggle.animory.common.entity;public class BaseEntity { +} From 1b30e4d528ca5bae7c6bc0061c29d5e184b83a81 Mon Sep 17 00:00:00 2001 From: greennarae Date: Tue, 10 Oct 2023 02:22:42 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20Base=20Entity(createdAt,=20updatedA?= =?UTF-8?q?t)=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/config/JpaAuditConfiguration.java | 9 ++++++- .../animory/common/entity/BaseEntity.java | 26 ++++++++++++++++++- .../domain/account/entity/Account.java | 3 ++- .../dto/request/PetRegisterRequestDto.java | 2 -- .../daggle/animory/domain/pet/entity/Pet.java | 9 +++---- .../domain/pet/entity/PetPolygonProfile.java | 2 +- .../domain/pet/service/PetWriteService.java | 1 + .../domain/shelter/entity/Shelter.java | 3 ++- .../domain/account/AccountControllerTest.java | 5 ---- .../animory/domain/pet/PetRepositoryTest.java | 2 +- .../domain/pet/fixture/PetFixture.java | 3 --- .../datajpatest/DataJpaTestWithDummyData.java | 2 ++ .../WithTimeSupportObjectMapper.java | 6 +++-- 13 files changed, 49 insertions(+), 24 deletions(-) diff --git a/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java b/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java index da85affc..87c6a64a 100644 --- a/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java +++ b/animory/src/main/java/com/daggle/animory/common/config/JpaAuditConfiguration.java @@ -1,2 +1,9 @@ -package com.daggle.animory.common.config;public class JpaAuditConfiguration { +package com.daggle.animory.common.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@Configuration +@EnableJpaAuditing +public class JpaAuditConfiguration { } diff --git a/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java b/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java index 8cb02b87..056637de 100644 --- a/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java +++ b/animory/src/main/java/com/daggle/animory/common/entity/BaseEntity.java @@ -1,2 +1,26 @@ -package com.daggle.animory.common.entity;public class BaseEntity { +package com.daggle.animory.common.entity; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.Column; +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@Getter +@EntityListeners(AuditingEntityListener.class) +@MappedSuperclass +public class BaseEntity { + @CreatedDate + @Column(updatable = false) + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd-HH-mm-ss") + private LocalDateTime createdAt; + + @LastModifiedDate + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd-HH-mm-ss") + private LocalDateTime updatedAt; } diff --git a/animory/src/main/java/com/daggle/animory/domain/account/entity/Account.java b/animory/src/main/java/com/daggle/animory/domain/account/entity/Account.java index c250c74b..cea42d57 100644 --- a/animory/src/main/java/com/daggle/animory/domain/account/entity/Account.java +++ b/animory/src/main/java/com/daggle/animory/domain/account/entity/Account.java @@ -1,5 +1,6 @@ package com.daggle.animory.domain.account.entity; +import com.daggle.animory.common.entity.BaseEntity; import lombok.*; import javax.persistence.*; @@ -11,7 +12,7 @@ @AllArgsConstructor @Table(name = "account") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Account { +public class Account extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; diff --git a/animory/src/main/java/com/daggle/animory/domain/pet/dto/request/PetRegisterRequestDto.java b/animory/src/main/java/com/daggle/animory/domain/pet/dto/request/PetRegisterRequestDto.java index 2c455ee6..b2c5873b 100644 --- a/animory/src/main/java/com/daggle/animory/domain/pet/dto/request/PetRegisterRequestDto.java +++ b/animory/src/main/java/com/daggle/animory/domain/pet/dto/request/PetRegisterRequestDto.java @@ -8,7 +8,6 @@ import javax.validation.constraints.NotNull; import java.time.LocalDate; -import java.time.LocalDateTime; @Builder public record PetRegisterRequestDto( @@ -41,7 +40,6 @@ public Pet toEntity(final Shelter shelter, final String imageUrl, final String v .profileImageUrl(imageUrl) .profileShortFormUrl(videoUrl) .size(size) - .createdAt(LocalDateTime.now()) .shelter(shelter) .build(); } diff --git a/animory/src/main/java/com/daggle/animory/domain/pet/entity/Pet.java b/animory/src/main/java/com/daggle/animory/domain/pet/entity/Pet.java index 95fe4f60..0a2d8f1f 100644 --- a/animory/src/main/java/com/daggle/animory/domain/pet/entity/Pet.java +++ b/animory/src/main/java/com/daggle/animory/domain/pet/entity/Pet.java @@ -1,5 +1,6 @@ package com.daggle.animory.domain.pet.entity; +import com.daggle.animory.common.entity.BaseEntity; import com.daggle.animory.domain.pet.dto.request.PetUpdateRequestDto; import com.daggle.animory.domain.pet.util.PetAgeToBirthDateConverter; import com.daggle.animory.domain.shelter.entity.Shelter; @@ -9,7 +10,6 @@ import javax.persistence.*; import javax.validation.constraints.NotNull; import java.time.LocalDate; -import java.time.LocalDateTime; @Getter @Entity @@ -17,7 +17,7 @@ @AllArgsConstructor @Table(name = "pet") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Pet { +public class Pet extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -43,7 +43,7 @@ public class Pet { @Column(length = 1000) private String description; - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd") private LocalDate protectionExpirationDate; private String vaccinationStatus; @@ -63,9 +63,6 @@ public class Pet { private String size; - @Column(nullable = false, columnDefinition = "datetime") - private LocalDateTime createdAt; - @ManyToOne @JoinColumn(name = "shelter_id") private Shelter shelter; diff --git a/animory/src/main/java/com/daggle/animory/domain/pet/entity/PetPolygonProfile.java b/animory/src/main/java/com/daggle/animory/domain/pet/entity/PetPolygonProfile.java index c9f205aa..cdd22a5b 100644 --- a/animory/src/main/java/com/daggle/animory/domain/pet/entity/PetPolygonProfile.java +++ b/animory/src/main/java/com/daggle/animory/domain/pet/entity/PetPolygonProfile.java @@ -33,7 +33,7 @@ public class PetPolygonProfile { private int activeness; - public void update(PetPolygonProfileDto petPolygonProfileDto) { + public void update(final PetPolygonProfileDto petPolygonProfileDto) { this.intelligence = petPolygonProfileDto.intelligence(); this.affinity = petPolygonProfileDto.affinity(); this.athletic = petPolygonProfileDto.athletic(); diff --git a/animory/src/main/java/com/daggle/animory/domain/pet/service/PetWriteService.java b/animory/src/main/java/com/daggle/animory/domain/pet/service/PetWriteService.java index b1cc3942..37945323 100644 --- a/animory/src/main/java/com/daggle/animory/domain/pet/service/PetWriteService.java +++ b/animory/src/main/java/com/daggle/animory/domain/pet/service/PetWriteService.java @@ -29,6 +29,7 @@ public class PetWriteService { private final ShelterRepository shelterRepository; private final PetRepository petRepository; private final PetPolygonRepository petPolygonRepository; + private final PetValidator petValidator; public RegisterPetSuccessDto registerPet(final Account account, diff --git a/animory/src/main/java/com/daggle/animory/domain/shelter/entity/Shelter.java b/animory/src/main/java/com/daggle/animory/domain/shelter/entity/Shelter.java index 338dd718..359849b7 100644 --- a/animory/src/main/java/com/daggle/animory/domain/shelter/entity/Shelter.java +++ b/animory/src/main/java/com/daggle/animory/domain/shelter/entity/Shelter.java @@ -1,5 +1,6 @@ package com.daggle.animory.domain.shelter.entity; +import com.daggle.animory.common.entity.BaseEntity; import com.daggle.animory.domain.account.entity.Account; import lombok.*; @@ -12,7 +13,7 @@ @AllArgsConstructor @Table(name = "shelter") @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class Shelter { +public class Shelter extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; diff --git a/animory/src/test/java/com/daggle/animory/domain/account/AccountControllerTest.java b/animory/src/test/java/com/daggle/animory/domain/account/AccountControllerTest.java index 6efc74ad..8c7ef9a9 100644 --- a/animory/src/test/java/com/daggle/animory/domain/account/AccountControllerTest.java +++ b/animory/src/test/java/com/daggle/animory/domain/account/AccountControllerTest.java @@ -6,10 +6,8 @@ import com.daggle.animory.domain.account.dto.request.ShelterSignUpDto; import com.daggle.animory.domain.shelter.entity.Province; import com.daggle.animory.testutil.webmvctest.BaseWebMvcTest; -import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.http.MediaType; @@ -21,9 +19,6 @@ @Import(AccountController.class) public class AccountControllerTest extends BaseWebMvcTest { - @Autowired - ObjectMapper om; - @MockBean AccountService accountService; diff --git a/animory/src/test/java/com/daggle/animory/domain/pet/PetRepositoryTest.java b/animory/src/test/java/com/daggle/animory/domain/pet/PetRepositoryTest.java index 4328b88b..3fd24c11 100644 --- a/animory/src/test/java/com/daggle/animory/domain/pet/PetRepositoryTest.java +++ b/animory/src/test/java/com/daggle/animory/domain/pet/PetRepositoryTest.java @@ -35,7 +35,7 @@ void findByShelterId() { final Integer shelterId = 1; final Page page = petRepository.findByShelterId(shelterId, PageRequest.of(0, 10)); - List pets = page.getContent(); + final List pets = page.getContent(); print(pets); pets.forEach( diff --git a/animory/src/test/java/com/daggle/animory/domain/pet/fixture/PetFixture.java b/animory/src/test/java/com/daggle/animory/domain/pet/fixture/PetFixture.java index 77cf66cb..ef1a429b 100644 --- a/animory/src/test/java/com/daggle/animory/domain/pet/fixture/PetFixture.java +++ b/animory/src/test/java/com/daggle/animory/domain/pet/fixture/PetFixture.java @@ -4,7 +4,6 @@ import com.daggle.animory.domain.shelter.entity.Shelter; import java.time.LocalDate; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -25,7 +24,6 @@ public static Pet getOne(final Shelter shelter) { .profileImageUrl("http://amazon.server/api/petImage/20231001104521_test1.jpg") .profileShortFormUrl("http://amazon.server/api/petVideo/20231001104521_test1.mp4") .size("작아요 소형견입니다.") - .createdAt(LocalDateTime.now()) .shelter(shelter) .build(); } @@ -50,7 +48,6 @@ public static List get(final int n, .profileImageUrl("http://amazon.server/api/petImage/20231001104521_test" + i + ".jpg") .profileShortFormUrl("http://amazon.server/api/petVideo/20231001104521_test" + i + ".mp4") .size("작아요 소형견입니다." + i) - .createdAt(LocalDateTime.now()) .shelter(shelter) .build() ); diff --git a/animory/src/test/java/com/daggle/animory/testutil/datajpatest/DataJpaTestWithDummyData.java b/animory/src/test/java/com/daggle/animory/testutil/datajpatest/DataJpaTestWithDummyData.java index 7b63c1cc..515f3f92 100644 --- a/animory/src/test/java/com/daggle/animory/testutil/datajpatest/DataJpaTestWithDummyData.java +++ b/animory/src/test/java/com/daggle/animory/testutil/datajpatest/DataJpaTestWithDummyData.java @@ -12,10 +12,12 @@ import org.junit.jupiter.api.BeforeEach; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import java.util.List; @DataJpaTest +@EnableJpaAuditing public abstract class DataJpaTestWithDummyData extends WithTimeSupportObjectMapper { @Autowired diff --git a/animory/src/test/java/com/daggle/animory/testutil/datajpatest/WithTimeSupportObjectMapper.java b/animory/src/test/java/com/daggle/animory/testutil/datajpatest/WithTimeSupportObjectMapper.java index c0763d89..d74086fd 100644 --- a/animory/src/test/java/com/daggle/animory/testutil/datajpatest/WithTimeSupportObjectMapper.java +++ b/animory/src/test/java/com/daggle/animory/testutil/datajpatest/WithTimeSupportObjectMapper.java @@ -3,11 +3,13 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import lombok.extern.slf4j.Slf4j; /** * time type serialization 설정이 추가된 object mapper 가 지원됩니다. * DataJpaTest 등에서 결과를 콘솔에 찍어보고자 할때 상속해서 사용하면 좋을 듯 합니다. */ +@Slf4j public abstract class WithTimeSupportObjectMapper { protected ObjectMapper om = new ObjectMapper() @@ -15,9 +17,9 @@ public abstract class WithTimeSupportObjectMapper { protected void print(final Object o) { try{ - System.out.println(om.writerWithDefaultPrettyPrinter().writeValueAsString(o)); + log.debug(om.writerWithDefaultPrettyPrinter().writeValueAsString(o)); } catch (final JsonProcessingException e){ - System.out.println("json parsing error: " + e.getMessage()); + log.debug("json parsing error: " + e.getMessage()); } }