Skip to content

Commit

Permalink
Merge pull request #81 from HanaFun/testing
Browse files Browse the repository at this point in the history
fix: revenue, transaction api
  • Loading branch information
doSeung11 authored Jul 5, 2024
2 parents c0fc571 + 3b96239 commit 656916e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreateLessonDateReqDto {
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CreateLessonReqDto {
private Long categoryId;
private String title;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Optional;

public interface RevenueRepository extends JpaRepository<RevenueEntity, Long> {
Optional<RevenueEntity> 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<RevenueEntity> findByLessonEntityAndCreatedDateBetween(LessonEntity lessonEntity, LocalDateTime startDateTime, LocalDateTime endDateTime);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
}
}
78 changes: 78 additions & 0 deletions src/test/java/com/hanaro/hanafun/UserServiceTests.java
Original file line number Diff line number Diff line change
@@ -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() {
}
}

0 comments on commit 656916e

Please sign in to comment.