Skip to content

Commit

Permalink
Merge pull request #237 from PLADI-ALM/test/PDS-15-bookOffice
Browse files Browse the repository at this point in the history
[PDS-15/test] 회의실 예약 성공 & 취소 테스트 코드
  • Loading branch information
leeseunghakhello authored Nov 26, 2023
2 parents 6d1a3bc + 6f9f187 commit 118d57d
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static LocalDate stringToLocalDate(String stringDate) {
return LocalDate.parse(stringDate, DateTimeFormatter.ISO_DATE);
}

public static LocalTime stringToLocalTime(String stringDate) {
return LocalTime.parse(stringDate, DateTimeFormatter.ISO_TIME);
}

// string(YYYY-MM) -> localDate (해당 월의 첫 날)
public static LocalDate stringToFirstLocalDate(String stringDate) {
return stringToLocalDate(stringDate + "-01");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
Expand All @@ -15,6 +16,7 @@
import static com.example.pladialmserver.global.Constants.TIME_PATTERN;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class OfficeReq {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class Office extends BaseEntity {
@OneToMany(mappedBy = "office")
private List<OfficeFacility> facilityList = new ArrayList<>();
@Builder
public Office(String name, String location, String capacity, String description, String imgKey,Boolean isActive){
public Office(Long id, String name, String location, String capacity, String description, String imgKey,Boolean isActive){
this.officeId = id;
this.name=name;
this.location=location;
this.capacity=capacity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.example.pladialmserver.office.service;

import com.example.pladialmserver.booking.entity.OfficeBooking;
import com.example.pladialmserver.booking.repository.officeBooking.OfficeBookingRepository;
import com.example.pladialmserver.global.exception.BaseException;
import com.example.pladialmserver.global.exception.BaseResponseCode;
import com.example.pladialmserver.office.dto.request.OfficeReq;
import com.example.pladialmserver.office.entity.Office;
import com.example.pladialmserver.office.repository.OfficeRepository;
import com.example.pladialmserver.office.service.model.TestOfficeInfo;
import com.example.pladialmserver.user.entity.Role;
import com.example.pladialmserver.user.entity.User;
import com.example.pladialmserver.user.service.model.TestUserInfo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.AdditionalAnswers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Optional;

import static com.example.pladialmserver.global.exception.BaseResponseCode.ALREADY_BOOKED_TIME;
import static com.example.pladialmserver.global.exception.BaseResponseCode.OFFICE_NOT_FOUND;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.times;

@ExtendWith(MockitoExtension.class)
public class OfficeServiceBookingTest {
@InjectMocks
private OfficeService officeService;
@Mock
private OfficeRepository officeRepository;
@Mock
private OfficeBookingRepository officeBookingRepository;
@Spy
BCryptPasswordEncoder passwordEncoder;

@Test
@DisplayName("[성공] 회의실 예약")
void bookOffice(){
// given
OfficeReq officeReq = TestOfficeInfo.setUpOfficeReq();
User user = TestUserInfo.setUpUser(1L, Role.ADMIN, TestUserInfo.setUpDepartment(), TestUserInfo.setUpAffiliation(), passwordEncoder.encode("asdf1234!"));
Office office = TestOfficeInfo.setUpOffice();

doReturn(Optional.of(office)).when(officeRepository).findByOfficeIdAndIsEnableAndIsActive(office.getOfficeId(), true, true);
doReturn(false).when(officeBookingRepository).existsByDateAndTime(office, officeReq.getDate(), officeReq.getStartTime(), officeReq.getEndTime());
when(officeBookingRepository.save(any(OfficeBooking.class))).then(AdditionalAnswers.returnsFirstArg());

// when
officeService.bookOffice(user, 1L, officeReq);

// verify -> because of void method
verify(officeRepository, times(1)).findByOfficeIdAndIsEnableAndIsActive(any(Long.class), any(Boolean.class), any(Boolean.class));
verify(officeBookingRepository, times(1)).existsByDateAndTime(any(Office.class), any(LocalDate.class), any(LocalTime.class), any(LocalTime.class));
verify(officeBookingRepository, times(1)).save(any(OfficeBooking.class));
}

@Test
@DisplayName("[실패] 회의실 예약 - 회의실이 없는 경우")
void bookOfficeFail(){
// given
OfficeReq officeReq = TestOfficeInfo.setUpOfficeReq();
User user = TestUserInfo.setUpUser(1L, Role.ADMIN, TestUserInfo.setUpDepartment(), TestUserInfo.setUpAffiliation(), passwordEncoder.encode("asdf1234!"));
doThrow(new BaseException(OFFICE_NOT_FOUND)).when(officeRepository).findByOfficeIdAndIsEnableAndIsActive(2L, true, true);

// when
BaseException exception = assertThrows(BaseException.class, () -> {
officeService.bookOffice(user, 2L, officeReq);
});

// then
assertThat(exception.getBaseResponseCode()).isEqualTo(OFFICE_NOT_FOUND);
}

@Test
@DisplayName("[실패] 회의실 예약 - 이미 존재하는 예약인 경우")
void bookOfficeFail2(){
// given
OfficeReq officeReq = TestOfficeInfo.setUpOfficeReq();
User user = TestUserInfo.setUpUser(1L, Role.ADMIN, TestUserInfo.setUpDepartment(), TestUserInfo.setUpAffiliation(), passwordEncoder.encode("asdf1234!"));
Office office = TestOfficeInfo.setUpOffice();
doReturn(true).when(officeBookingRepository).existsByDateAndTime(office, officeReq.getDate(), officeReq.getStartTime(), officeReq.getEndTime());
doReturn(Optional.of(office)).when(officeRepository).findByOfficeIdAndIsEnableAndIsActive(office.getOfficeId(), true, true);

// when
BaseException exception = assertThrows(BaseException.class, () -> {
officeService.bookOffice(user, 1L, officeReq);
});
// then
assertThat(exception.getBaseResponseCode()).isEqualTo(ALREADY_BOOKED_TIME);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.pladialmserver.office.service.model;

import com.example.pladialmserver.global.utils.DateTimeUtil;
import com.example.pladialmserver.office.dto.request.OfficeReq;
import com.example.pladialmserver.office.entity.Office;

public class TestOfficeInfo {

public static OfficeReq setUpOfficeReq(){
return OfficeReq.builder()
.date(DateTimeUtil.stringToLocalDate("2023-12-01"))
.startTime(DateTimeUtil.stringToLocalTime("11:00"))
.endTime(DateTimeUtil.stringToLocalTime("12:00"))
.memo("프롬프터 사용 필요")
.build();
}

public static Office setUpOffice(){
return Office.builder()
.id(1L)
.name("회의실 1")
.location("2F")
.capacity("2")
.description("플래디 온리")
.imgKey("asdf.png")
.isActive(true)
.build();
}
}

0 comments on commit 118d57d

Please sign in to comment.