diff --git a/build.gradle b/build.gradle index e215d70..9e6e520 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.mockito:mockito-core' + testImplementation 'org.mockito:mockito-junit-jupiter' // Spring Security implementation 'org.springframework.boot:spring-boot-starter-security' diff --git a/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonDateReqDto.java b/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonDateReqDto.java index ec5766c..c9dc6ef 100644 --- a/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonDateReqDto.java +++ b/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonDateReqDto.java @@ -12,7 +12,6 @@ @Getter @Builder @AllArgsConstructor -@NoArgsConstructor public class CreateLessonDateReqDto { @JsonFormat(pattern = "yyyy-MM-dd") private LocalDate date; diff --git a/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonReqDto.java b/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonReqDto.java index afa695c..a8517c5 100644 --- a/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonReqDto.java +++ b/src/main/java/com/hanaro/hanafun/lesson/dto/request/CreateLessonReqDto.java @@ -10,7 +10,6 @@ @Getter @Builder @AllArgsConstructor -@NoArgsConstructor public class CreateLessonReqDto { private Long categoryId; private String title; diff --git a/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java b/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java index 31902bc..4838197 100644 --- a/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java +++ b/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java @@ -10,7 +10,6 @@ import java.util.Optional; public interface RevenueRepository extends JpaRepository { - Optional findByCreatedDateBetween(LocalDateTime startDateTime, LocalDateTime endDateTime); @Query(value = "SELECT SUM(R.revenue) FROM RevenueEntity R WHERE R.lessonEntity = :lessonEntity") Long totalRevenueByLessonId(@Param("lessonEntity") LessonEntity lessonEntity); Optional findByLessonEntityAndCreatedDateBetween(LessonEntity lessonEntity, LocalDateTime startDateTime, LocalDateTime endDateTime); diff --git a/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java b/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java index 5d8ede0..ea09f08 100644 --- a/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java +++ b/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java @@ -21,6 +21,7 @@ import java.time.YearMonth; import java.util.Date; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -43,7 +44,7 @@ public TotalRevenueResDto totalRevenue(Long userId) { return new TotalRevenueResDto().builder() .totalRevenue(lessonEntityList.stream() - .mapToLong(lessonEntity -> revenueRepository.totalRevenueByLessonId(lessonEntity)) + .mapToLong(lessonEntity -> Optional.ofNullable(revenueRepository.totalRevenueByLessonId(lessonEntity)).orElse(0L)) .sum()) .build(); } diff --git a/src/main/java/com/hanaro/hanafun/transaction/service/impl/TransactionServiceImpl.java b/src/main/java/com/hanaro/hanafun/transaction/service/impl/TransactionServiceImpl.java index 7081d6a..c55eca7 100644 --- a/src/main/java/com/hanaro/hanafun/transaction/service/impl/TransactionServiceImpl.java +++ b/src/main/java/com/hanaro/hanafun/transaction/service/impl/TransactionServiceImpl.java @@ -7,6 +7,7 @@ import com.hanaro.hanafun.hanastorage.domain.HanastorageEntity; import com.hanaro.hanafun.hanastorage.domain.HanastorageRepository; import com.hanaro.hanafun.hanastorage.exception.HanastorageNotFoundException; +import com.hanaro.hanafun.lesson.domain.LessonEntity; import com.hanaro.hanafun.lesson.domain.LessonRepository; import com.hanaro.hanafun.lesson.exception.LessonNotFoundException; import com.hanaro.hanafun.lessondate.domain.LessonDateEntity; @@ -217,11 +218,14 @@ private void calcAccount(AccountEntity accountEntity, int payment, String type){ @Transactional private void calcRevenue(Long lessonId, int payment){ + LessonEntity lessonEntity = lessonRepository.findById(lessonId).orElseThrow(() -> new LessonNotFoundException()); + + //달의 시작과 끝을 설정. LocalDateTime startOfMonth = YearMonth.now().atDay(1).atStartOfDay(); LocalDateTime endOfMonth = YearMonth.now().atEndOfMonth().atTime(23, 59, 59); //없으면 그냥 이후의 로직을 처리함. 예외 처리 X. - RevenueEntity revenueEntity = revenueRepository.findByCreatedDateBetween(startOfMonth, endOfMonth) + RevenueEntity revenueEntity = revenueRepository.findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth) .orElse(null); if (revenueEntity != null) { @@ -234,7 +238,8 @@ private void calcRevenue(Long lessonId, int payment){ .rentalPrice(0) .etcPrice(0) .build(); - revenueRepository.save(newRevenue); + RevenueEntity testR = revenueRepository.save(newRevenue); + System.out.println("isCOME? " + testR.getRevenue()); } } } \ No newline at end of file diff --git a/src/test/java/com/hanaro/hanafun/UserServiceTests.java b/src/test/java/com/hanaro/hanafun/UserServiceTests.java new file mode 100644 index 0000000..f580c24 --- /dev/null +++ b/src/test/java/com/hanaro/hanafun/UserServiceTests.java @@ -0,0 +1,78 @@ +package com.hanaro.hanafun; + +import com.hanaro.hanafun.common.authentication.JwtUtil; +import com.hanaro.hanafun.user.domain.UserEntity; +import com.hanaro.hanafun.user.domain.UserRepository; +import com.hanaro.hanafun.user.dto.LoginReqDto; +import com.hanaro.hanafun.user.service.impl.UserServiceImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.Optional; + +import static org.mockito.Mockito.when; + +public class UserServiceTests { + + //@Mock을 사용하여 UserRepository를 Mock 객체로 생성한다. + @Mock + private UserRepository userRepository; + + //@Mock을 사용하여 JwtUtil을 Mock 객체로 생성한다. + @Mock + private JwtUtil jwtUtil; + + //@InjectMocks를 사용하여 Mock 객체들을 UserServiceImpl에 주입한다. + //스프링 컨텍스트와 무관하게 동작한다. + //참고로 @Autowired는 스프링 프레임워크의 일부로서 컨텍스트에서 관리하는 빈 객체만 주입한다. + @InjectMocks + private UserServiceImpl userService; + + //각 테스트 실행 전에 Mock 객체들을 초기화한다. + @BeforeEach + void setUp(){ + MockitoAnnotations.openMocks(this); + } + + @Test + void testLoginSuccess(){ + //Arrange: 테스트를 위한 준비 단계 + + //사용자 정보 설정 + long userId = 1L; + String username = "이민지"; + String password = "1111"; + String generatedJwt = ""; + + //UserEntity 생성 + UserEntity userEntity = UserEntity.builder() + .userId(userId) + .username(username) + .password(password) + .build(); + + //LoginReqDto 생성 + LoginReqDto loginReqDto = LoginReqDto.builder() + .password(password) + .build(); + + //userRepository.findByPassword(password) 호출 시 userEntity를 반환하도록 설정 + when(userRepository.findByPassword(password)).thenReturn(Optional.of(userEntity)); + + } + + @Test + void login() { + } + + @Test + void readPoint() { + } + + @Test + void readIsHost() { + } +}