Skip to content

Commit

Permalink
Merge pull request #235 from PLADI-ALM/test/PDS-201-rejectProductBooking
Browse files Browse the repository at this point in the history
[PDS-201/test] 관리자 장비 예약 반려 API 테스트 코드
  • Loading branch information
dangnak2 authored Nov 26, 2023
2 parents 5e7393a + b189ea1 commit 790fb19
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.example.pladialmserver.booking.service;

import com.example.pladialmserver.booking.dto.request.SendEmailReq;
import com.example.pladialmserver.booking.dto.response.ProductBookingDetailRes;
import com.example.pladialmserver.booking.entity.ResourceBooking;
import com.example.pladialmserver.booking.repository.resourceBooking.ResourceBookingRepository;
Expand All @@ -11,7 +10,6 @@
import com.example.pladialmserver.global.utils.DateTimeUtil;
import com.example.pladialmserver.global.utils.EmailUtil;
import com.example.pladialmserver.notification.service.PushNotificationService;
import com.example.pladialmserver.user.dto.TokenDto;
import com.example.pladialmserver.user.entity.Role;
import com.example.pladialmserver.user.entity.User;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -21,13 +19,8 @@
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -69,7 +62,7 @@ void getProductBookingDetail_SUCCESS() {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser, BookingStatus.WAITING);
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.WAITING);
// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
ProductBookingDetailRes res = resourceBookingService.getProductBookingDetail(basicUser, resourceBooking.getResourceBookingId());
Expand All @@ -91,7 +84,7 @@ void getProductBookingDetail_NO_AUTHENTICATION() {
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User fakeUser = setUpUser(3L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser, BookingStatus.WAITING);
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.WAITING);
// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
BaseException exception = assertThrows(BaseException.class, () -> {
Expand All @@ -108,7 +101,7 @@ void getProductBookingDetail_BOOKING_NOT_FOUND() {
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User fakeUser = setUpUser(3L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser, BookingStatus.WAITING);
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.WAITING);
// when
BaseException exception = assertThrows(BaseException.class, () -> {
resourceBookingService.getProductBookingDetail(fakeUser, resourceBooking.getResourceBookingId()+1);
Expand All @@ -123,7 +116,7 @@ void cancelBookingProduct_SUCCESS() throws IOException {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser, BookingStatus.USING);
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.USING);

// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
Expand All @@ -144,7 +137,7 @@ void cancelBookingProduct_ALREADY_CANCELED_BOOKING() {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser, BookingStatus.CANCELED);
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.CANCELED);

// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
Expand All @@ -162,7 +155,7 @@ void cancelBookingProduct_ALREADY_FINISHED_BOOKING() {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser, BookingStatus.FINISHED);
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.FINISHED);

// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
Expand All @@ -183,7 +176,65 @@ void getProductBookingDetailByAdmin() {
}

@Test
void rejectProductBooking() {
@DisplayName("[성공] 관리자 장비 예약 반려")
void rejectProductBooking_SUCCESS() throws IOException {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.WAITING);

// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
String title = COMPANY_NAME + RESOURCE + SPACE + BOOKING_TEXT + BOOKING_REJECT;
Map<String, String> bookingData = new HashMap<>();
doNothing().when(emailUtil).sendEmail(resourceBooking.getUser().getEmail(), title, bookingData, BOOKING_TEMPLATE);
doNothing().when(notificationService).sendNotification(eq(Constants.NotificationCategory.EQUIPMENT), eq(Constants.Notification.BODY_DENIED), any(User.class));

resourceBookingService.rejectProductBooking(adminUser, resourceBooking.getResourceBookingId());

// then
assertThat(resourceBooking.getStatus()).isEqualTo(BookingStatus.CANCELED);
}

@Test
@DisplayName("[실패] 관리자 장비 예약 반려 - 불가능한 예약 상태인 경우")
void rejectProductBooking_INVALID_BOOKING_STATUS() {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking_finished = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.FINISHED);
ResourceBooking resourceBooking_canceled = setUpResourceBooking(2L, basicUser, adminUser, BookingStatus.CANCELED);

// when
lenient().when(resourceBookingRepository.findById(anyLong()))
.thenReturn(Optional.of(resourceBooking_finished))
.thenReturn(Optional.of(resourceBooking_canceled));

BaseException exception = assertThrows(BaseException.class, () -> {
resourceBookingService.rejectProductBooking(adminUser, resourceBooking_finished.getResourceBookingId());
resourceBookingService.rejectProductBooking(adminUser, resourceBooking_canceled.getResourceBookingId());
});
// then
assertThat(exception.getBaseResponseCode()).isEqualTo(BaseResponseCode.INVALID_BOOKING_STATUS);
}

@Test
@DisplayName("[실패] 관리자 장비 예약 반려 - 접근 권한이 없는 경우")
void rejectProductBooking_NO_AUTHENTICATION() {
// given
User basicUser = setUpUser(1L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(2L, Role.ADMIN, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(1L, basicUser, adminUser, BookingStatus.WAITING);

// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());

BaseException exception = assertThrows(BaseException.class, () -> {
resourceBookingService.rejectProductBooking(basicUser, resourceBooking.getResourceBookingId());
});

// then
assertThat(exception.getBaseResponseCode()).isEqualTo(BaseResponseCode.NO_AUTHENTICATION);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import static com.example.pladialmserver.resource.service.model.TestResourceInfo.setUpResource;

public class TestResourceBookingInfo {
public static ResourceBooking setUpResourceBooking(User basicUser, User adminUser, BookingStatus status) {
public static ResourceBooking setUpResourceBooking(Long resourceBookingId, User basicUser, User adminUser, BookingStatus status) {
Resource resource = setUpResource(adminUser);
return ResourceBooking.builder()
.resourceBookingId(1L)
.resourceBookingId(resourceBookingId)
.user(basicUser)
.resource(resource)
.startDate(LocalDateTime.of(2024, 1, 1, 0, 0))
Expand Down

0 comments on commit 790fb19

Please sign in to comment.