Skip to content

Commit

Permalink
Merge pull request #226 from PLADI-ALM/test/PDS-196-getResourceBookin…
Browse files Browse the repository at this point in the history
…gDetail

[PDS-196/test] 장비 예약 개별 조회 API 성공&실패 테스트 구현
  • Loading branch information
leeseunghakhello authored Nov 24, 2023
2 parents 1d80725 + 8e9c84e commit bf91579
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ public ResourceBooking(User user, Resource resource, LocalDateTime startDate, Lo
this.memo = memo;
}

public ResourceBooking(Long resourceBookingId, User user, Resource resource, LocalDateTime startDate, LocalDateTime endDate, String memo) {
this.resourceBookingId = resourceBookingId;
this.user = user;
this.resource = resource;
this.startDate = startDate;
this.endDate = endDate;
this.memo = memo;
}

public static ResourceBooking toDto(User user, Resource resource, ProductReq productReq) {
return ResourceBooking.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ public Resource(String name, String manufacturer, String description, String img
this.user=user;
}

public Resource(Long resourceId, String name, String manufacturer, String location, String description, String imgKey, User user) {
this.resourceId = resourceId;
this.name = name;
this.manufacturer = manufacturer;
this.location = location;
this.description = description;
this.imgKey = imgKey;
this.user = user;
}

public static Resource toDto(CreateProductReq request, User responsibility) {
return Resource.builder()
.name(request.getName())
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/example/pladialmserver/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ public User(Long userId, String name, String email, String password, Department
this.affiliation = affiliation;
}

public User(Long userId, String name, String email, String password, String phone, String assets, Department department, Role role, Affiliation affiliation) {
this.userId = userId;
this.name = name;
this.email = email;
this.password = password;
this.phone = phone;
this.assets = assets;
this.department = department;
this.role = role;
this.affiliation = affiliation;
}

public static User toEntity(CreateUserReq req, Department department, Affiliation affiliation){
return User.builder()
.name(req.getName())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.example.pladialmserver.booking.service;

import com.example.pladialmserver.booking.dto.response.ProductBookingDetailRes;
import com.example.pladialmserver.booking.entity.ResourceBooking;
import com.example.pladialmserver.booking.repository.resourceBooking.ResourceBookingRepository;
import com.example.pladialmserver.global.exception.BaseException;
import com.example.pladialmserver.global.exception.BaseResponseCode;
import com.example.pladialmserver.global.utils.DateTimeUtil;
import com.example.pladialmserver.user.entity.Role;
import com.example.pladialmserver.user.entity.User;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.util.Optional;

import static com.example.pladialmserver.booking.service.model.TestResourceBookingInfo.setUpResourceBooking;
import static com.example.pladialmserver.user.service.model.TestUserInfo.*;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doReturn;

@ExtendWith(MockitoExtension.class)
class ResourceBookingServiceTest {

@InjectMocks
private ResourceBookingService resourceBookingService;

@Mock
private ResourceBookingRepository resourceBookingRepository;

@Spy
BCryptPasswordEncoder passwordEncoder;

@Test
void getProductBookings() {
}

@Test
@DisplayName("[성공] 장비 예약 개별 조회")
void getProductBookingDetail_SUCCESS() {
// given
User basicUser = setUpBasicUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
User adminUser = setUpUser(setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser);
// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
ProductBookingDetailRes res = resourceBookingService.getProductBookingDetail(basicUser, resourceBooking.getResourceBookingId());
// then
assertThat(res.getId()).isEqualTo(resourceBooking.getResource().getResourceId());
assertThat(res.getStatus()).isEqualTo(resourceBooking.getStatus().getValue());
assertThat(res.getReservatorName()).isEqualTo(resourceBooking.getUser().getName());
assertThat(res.getReservatorPhone()).isEqualTo(resourceBooking.getUser().getPhone());
assertThat(res.getStartDate()).isEqualTo(DateTimeUtil.dateTimeToString(resourceBooking.getStartDate()));
assertThat(res.getEndDate()).isEqualTo(DateTimeUtil.dateTimeToString(resourceBooking.getEndDate()));
assertThat(res.getReturnDateTime()).isEqualTo(DateTimeUtil.dateTimeToStringNullable(resourceBooking.getReturnDate()));
assertThat(res.getMemo()).isEqualTo(resourceBooking.getMemo());
}

@Test
@DisplayName("[실패] 장비 예약 개별 조회 - 본인 장비 예약이 아닌 경우")
void getProductBookingDetail_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));
User fakeUser = setUpUser(3L, Role.BASIC, setUpDepartment(), setUpAffiliation(), passwordEncoder.encode(PASSWORD));
ResourceBooking resourceBooking = setUpResourceBooking(basicUser, adminUser);
// when
doReturn(Optional.of(resourceBooking)).when(resourceBookingRepository).findById(resourceBooking.getResourceBookingId());
BaseException exception = assertThrows(BaseException.class, () -> {
resourceBookingService.getProductBookingDetail(fakeUser, resourceBooking.getResourceBookingId());
});
// then
assertThat(exception.getBaseResponseCode()).isEqualTo(BaseResponseCode.NO_AUTHENTICATION);
}

@Test
@DisplayName("[실패] 장비 예약 개별 조회 - 존재하지 않는 예약인 경우")
void getProductBookingDetail_BOOKING_NOT_FOUND() {
// given
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);
// when
BaseException exception = assertThrows(BaseException.class, () -> {
resourceBookingService.getProductBookingDetail(fakeUser, resourceBooking.getResourceBookingId()+1);
});
// then
assertThat(exception.getBaseResponseCode()).isEqualTo(BaseResponseCode.BOOKING_NOT_FOUND);
}

@Test
void cancelBookingProduct() {
}

@Test
void checkProductBookingTime() {
}

@Test
void getProductBookingDetailByAdmin() {
}

@Test
void rejectProductBooking() {
}

@Test
void allowProductBooking() {
}

@Test
void returnBookingProductByAdmin() {
}

@Test
void getBookingProducts() {
}

@Test
void returnBookingProduct() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.pladialmserver.booking.service.model;

import com.example.pladialmserver.booking.entity.ResourceBooking;
import com.example.pladialmserver.product.resource.entity.Resource;
import com.example.pladialmserver.user.entity.User;

import java.time.LocalDateTime;

import static com.example.pladialmserver.resource.service.model.TestResourceInfo.setUpResource;

public class TestResourceBookingInfo {
public static ResourceBooking setUpResourceBooking(User basicUser, User adminUser){
Resource resource = setUpResource(adminUser);
return new ResourceBooking(
1L,
basicUser,
resource,
LocalDateTime.of(2024,1,1,0,0),
LocalDateTime.of(2024,1,2,0,0),
"예약합니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.pladialmserver.resource.service.model;

import com.example.pladialmserver.product.resource.entity.Resource;
import com.example.pladialmserver.user.entity.User;

public class TestResourceInfo {

public static Resource setUpResource(User adminUser) {
return new Resource(
1L,
"ENG 카메라",
"소니",
"401호",
"파손 주의",
"sample.jpg",
adminUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,35 @@ public static User setUpUser(Department department, Affiliation affiliation, Str
.build();
}

public static User setUpUser(Long userId, Role role, Department department, Affiliation affiliation, String password){
return new User(
userId,
"홍길동",
"[email protected]",
password,
department,
"010-0000-0000",
role,
"1234545",
"A12345",
affiliation);
}

public static User setUpBasicUser(Department department, Affiliation affiliation, String password){
return User.builder()
.userId(1L)
.name("홍길동")
.email("[email protected]")
.password(password)
.fcmToken("1234545")
.phone("010-0000-0000")
.department(department)
.role(Role.BASIC)
.assets("A12345")
.affiliation(affiliation)
.build();
}

public static Department setUpDepartment(){
return Department.builder()
.name("마케팅")
Expand Down

0 comments on commit bf91579

Please sign in to comment.