Skip to content

Commit

Permalink
Merge pull request #100 from f-lab-edu/feature-16-user-api-test
Browse files Browse the repository at this point in the history
test:UserServiceTest.java 추가
  • Loading branch information
byunyourim authored Sep 5, 2024
2 parents fe3f60c + 7583197 commit 526f05f
Show file tree
Hide file tree
Showing 4 changed files with 266 additions and 115 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/kboticket/dto/user/UserPasswordDto.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.kboticket.dto.user;

import com.kboticket.controller.user.dto.ChangePasswordRequest;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.*;

@Builder
@Getter
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
public class UserPasswordDto {

private String email;
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/kboticket/service/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ public void updatePassword(UserPasswordDto userPasswordDto) {
userRepository.save(user);
}

// email 이 존재하는지
// email 존재 여부
public boolean isExistEmail(String email) {
return userRepository.existsByEmail(email);
}

// phone 이 존재하는지
// phone 존재 여부
public boolean isExistPhone(String phone) {
return userRepository.existsByPhone(phone);
}
Expand All @@ -158,12 +158,9 @@ public String findbyPhone(String phone) {

// 기존 비밀 번호 확인
public boolean checkPassword(String email, String inputPassword) {
log.info("storedpassword : " + inputPassword);

Optional<User> optionalUser = userRepository.findByEmail(email);
String storedPassword = optionalUser.get().getPassword();

log.info("storedpassword : " + storedPassword);
return bCryptPasswordEncoder.matches(inputPassword, storedPassword);
}
}
212 changes: 106 additions & 106 deletions src/test/java/com/kboticket/service/ReservationServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,106 +1,106 @@
package com.kboticket.service;

import com.kboticket.enums.ErrorCode;
import com.kboticket.exception.KboTicketException;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class ReservationServiceTest {

@InjectMocks
private ReservationService reservationService;

@Mock
private RedissonClient redissonClient;

@Mock
private RLock rLock;


@BeforeEach
void setUp() {
when(redissonClient.getLock(anyString())).thenReturn(rLock);
}

@Test
void shouldReserveSeatsSuccessfully() throws InterruptedException {
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L, 3L));
Long gameId = 1L;
String email = "[email protected]";

when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(true);

reservationService.reserve(seatIds, gameId, email);

verify(rLock, times(seatIds.size())).tryLock(anyLong(), anyLong(), any(TimeUnit.class));
verify(rLock, times(0)).unlock();
}

@Test
void shouldThrowExceptionWhenSeatIsAlreadyHeld() throws Exception {
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L));
Long gameId = 1L;
String email = "[email protected]";

when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(false);

KboTicketException exception = assertThrows(KboTicketException.class, () -> {
reservationService.reserve(seatIds, gameId, email);
});

assertEquals("테스트가 실패하면 메시지 표", ErrorCode.FAILED_TRY_ROCK.message, exception.getMessage());
verify(rLock, times(1)).unlock();
}

@Test
void shouldReleaseLocksOnException() throws InterruptedException {
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L));
Long gameId = 1L;
String email = "[email protected]";

when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class)))
.thenReturn(true)
.thenThrow(new KboTicketException(ErrorCode.FAILED_TRY_ROCK));

try {
reservationService.reserve(seatIds, gameId, email);
} catch (KboTicketException e) {
// Exception is expected
}

verify(rLock, times(1)).unlock();
}

@Test
void shouldHandleInterruptedException() throws InterruptedException {
Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L));
Long gameId = 1L;
String email = "[email protected]";

when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException());

assertDoesNotThrow(() -> reservationService.reserve(seatIds, gameId, email));

verify(rLock, times(1)).unlock();
}
}
//package com.kboticket.service;
//
//import com.kboticket.enums.ErrorCode;
//import com.kboticket.exception.KboTicketException;
//import org.junit.Test;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.runner.RunWith;
//import org.mockito.InjectMocks;
//import org.mockito.Mock;
//import org.redisson.api.RLock;
//import org.redisson.api.RedissonClient;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.util.Arrays;
//import java.util.HashSet;
//import java.util.Set;
//import java.util.concurrent.TimeUnit;
//
//import static org.junit.jupiter.api.Assertions.*;
//import static org.mockito.ArgumentMatchers.*;
//import static org.mockito.Mockito.*;
//
//@RunWith(SpringRunner.class)
//@SpringBootTest
//@Transactional
//public class ReservationServiceTest {
//
// @InjectMocks
// private ReservationService reservationService;
//
// @Mock
// private RedissonClient redissonClient;
//
// @Mock
// private RLock rLock;
//
//
// @BeforeEach
// void setUp() {
// when(redissonClient.getLock(anyString())).thenReturn(rLock);
// }
//
// @Test
// void shouldReserveSeatsSuccessfully() throws InterruptedException {
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L, 3L));
// Long gameId = 1L;
// String email = "[email protected]";
//
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(true);
//
// reservationService.reserve(seatIds, gameId, email);
//
// verify(rLock, times(seatIds.size())).tryLock(anyLong(), anyLong(), any(TimeUnit.class));
// verify(rLock, times(0)).unlock();
// }
//
// @Test
// void shouldThrowExceptionWhenSeatIsAlreadyHeld() throws Exception {
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L));
// Long gameId = 1L;
// String email = "[email protected]";
//
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenReturn(false);
//
// KboTicketException exception = assertThrows(KboTicketException.class, () -> {
// reservationService.reserve(seatIds, gameId, email);
// });
//
// assertEquals("테스트가 실패하면 메시지 표", ErrorCode.FAILED_TRY_ROCK.message, exception.getMessage());
// verify(rLock, times(1)).unlock();
// }
//
// @Test
// void shouldReleaseLocksOnException() throws InterruptedException {
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L));
// Long gameId = 1L;
// String email = "[email protected]";
//
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class)))
// .thenReturn(true)
// .thenThrow(new KboTicketException(ErrorCode.FAILED_TRY_ROCK));
//
// try {
// reservationService.reserve(seatIds, gameId, email);
// } catch (KboTicketException e) {
// // Exception is expected
// }
//
// verify(rLock, times(1)).unlock();
// }
//
// @Test
// void shouldHandleInterruptedException() throws InterruptedException {
// Set<Long> seatIds = new HashSet<>(Arrays.asList(1L, 2L));
// Long gameId = 1L;
// String email = "[email protected]";
//
// when(rLock.tryLock(anyLong(), anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException());
//
// assertDoesNotThrow(() -> reservationService.reserve(seatIds, gameId, email));
//
// verify(rLock, times(1)).unlock();
// }
//}
Loading

0 comments on commit 526f05f

Please sign in to comment.