Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/#67 grpc book rental #77

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.ssu.commerce.book.model.Book;
import com.ssu.commerce.book.persistence.BookRepository;
import com.ssu.commerce.core.error.NotFoundException;
import com.ssu.commerce.grpc.RentalBookRequest;
import com.ssu.commerce.grpc.RentalBookResponse;
import com.ssu.commerce.grpc.UpdateBookStateGrpc;
import com.ssu.commerce.grpc.UpdateBookStateRequest;
import io.grpc.stub.StreamObserver;
Expand All @@ -27,6 +29,37 @@
public class GrpcUpdateBookStateService extends UpdateBookStateGrpc.UpdateBookStateImplBase {
private final BookRepository bookRepository;

@Override
public void rentalBook(RentalBookRequest request, StreamObserver<RentalBookResponse> responseObserver) {
List<RentalBookRequestDto> rentalBookRequestDto = request.getIdList()
.stream().map(id -> RentalBookRequestDto.builder().bookId(UUID.fromString(id)).build())
.collect(Collectors.toList());

try {
if (processUpdateBookState(rentalBookRequestDto, BookState.SHARING)) {
RentalBookResponse response = RentalBookResponse.newBuilder()
.setMessage("Books rented successfully.")
.build();
responseObserver.onNext(response);
}
responseObserver.onCompleted();
} catch (NotFoundException e) {
RentalBookResponse response = RentalBookResponse.newBuilder()
.setMessage("Books not found.")
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
throw e;
} catch (BookStateConflictException e) {
RentalBookResponse response = RentalBookResponse.newBuilder()
.setMessage("Books could not be rented due to state conflicts.")
.build();
responseObserver.onNext(response);
responseObserver.onCompleted();
throw e;
}
}

@Override
@Transactional
public void updateBookState(
Expand All @@ -42,14 +75,16 @@ public void updateBookState(
List<RentalBookRequestDto> rentalBookRequestDto = request.getIdList()
.stream().map(id -> RentalBookRequestDto.builder().bookId(UUID.fromString(id)).build())
.collect(Collectors.toList());
updateBookState(rentalBookRequestDto, updateState, responseObserver);

com.ssu.commerce.book.constant.code.BookState state = GrpcBookStateMapper.INSTANCE.map(updateState);

processUpdateBookState(rentalBookRequestDto, state);
responseObserver.onCompleted();
}

private void updateBookState(
private boolean processUpdateBookState(
@DistributedLock List<RentalBookRequestDto> requestDto,
com.ssu.commerce.grpc.BookState grpcUpdateState,
StreamObserver<Empty> responseObserver
com.ssu.commerce.book.constant.code.BookState updateState
) {
List<UUID> bookIds = requestDto.stream().map(RentalBookRequestDto::getBookId).collect(Collectors.toList());
List<Book> booksToCheck = bookRepository.findAllById(bookIds);
Expand All @@ -61,8 +96,6 @@ private void updateBookState(
);
}

com.ssu.commerce.book.constant.code.BookState updateState = GrpcBookStateMapper.INSTANCE.map(grpcUpdateState);

boolean allUpdatePossible = booksToCheck.stream()
.allMatch(book -> checkUpdateBookState(book, updateState));

Expand All @@ -75,7 +108,7 @@ private void updateBookState(
bookRepository.save(book); // 변경된 상태를 저장
});

responseObserver.onCompleted();
return true;
}

private boolean checkUpdateBookState(Book book, BookState bookState) {
Expand Down
6 changes: 1 addition & 5 deletions api/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,4 @@ springdoc:

logging:
level:
org:
hibernate:
SQL: DEBUG # Hibernate SQL 로그를 DEBUG 레벨로 설정
com:
p6spy: DEBUG # p6spy 로그를 DEBUG 레벨로 설정하여 SQL 로그를 더욱 상세하게 추적
p6spy: info
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.ssu.commerce.book.persistence.BookRepository;
import com.ssu.commerce.book.supplier.BookTestDataSupplier;
import com.ssu.commerce.core.error.NotFoundException;
import com.ssu.commerce.grpc.RentalBookRequest;
import com.ssu.commerce.grpc.RentalBookResponse;
import com.ssu.commerce.grpc.UpdateBookStateRequest;
import io.grpc.stub.StreamObserver;
import org.junit.jupiter.api.Test;
Expand All @@ -30,6 +32,9 @@ public class GrpcUpdateBookStateServiceTest implements BookTestDataSupplier {
@Mock
private StreamObserver<Empty> response;

@Mock
private StreamObserver<RentalBookResponse> rentalResponseObserver;

@InjectMocks
private GrpcUpdateBookStateService grpcUpdateBookStateService;

Expand Down Expand Up @@ -73,4 +78,61 @@ void updateBookTest_fail_conflict() {

assertEquals(expectedException, exception);
}

@Test
void rentalBookTest_success() {
when(bookRepository.findAllById(List.of(TEST_VAL_BOOK_ID, TEST_VAL_ANOTHER_BOOK_ID)))
.thenReturn(BookTestDataSupplier.getBookListForGrpc());
RentalBookRequest rentalBookRequest = BookTestDataSupplier.getRentalBookRequest();

grpcUpdateBookStateService.rentalBook(rentalBookRequest, rentalResponseObserver);

verify(bookRepository).findAllById(List.of(TEST_VAL_BOOK_ID, TEST_VAL_ANOTHER_BOOK_ID));
verify(bookRepository).save(Book.builder().bookId(TEST_VAL_BOOK_ID).bookState(BookState.SHARING).build());
verify(bookRepository).save(Book.builder().bookId(TEST_VAL_ANOTHER_BOOK_ID).bookState(BookState.SHARING).build());
}

@Test
void rentalBookTest_fail_not_found() {
RentalBookRequest rentalBookRequest = BookTestDataSupplier.getRentalBookRequest();
String expectedMessage = new NotFoundException(
String.format("book not found; book list size=%d", 2),
"BOOK_003"
).getMessage();

when(bookRepository.findAllById(List.of(TEST_VAL_BOOK_ID, TEST_VAL_ANOTHER_BOOK_ID))).thenReturn(List.of());

NotFoundException exception = assertThrows(NotFoundException.class, () -> grpcUpdateBookStateService.rentalBook(rentalBookRequest, rentalResponseObserver));

RentalBookResponse expectedResponse = RentalBookResponse.newBuilder()
.setMessage("Books not found.")
.build();

verify(rentalResponseObserver).onNext(expectedResponse);
verify(rentalResponseObserver).onCompleted();

assertEquals(expectedMessage, exception.getMessage());
}

@Test
void rentalBookTest_fail_conflict() {
when(bookRepository.findAllById(List.of(TEST_VAL_BOOK_ID, TEST_VAL_ANOTHER_BOOK_ID)))
.thenReturn(BookTestDataSupplier.getBookListForGrpcConflict());

RentalBookRequest rentalBookRequest = BookTestDataSupplier.getRentalBookRequest();

BookStateConflictException expectedException = new BookStateConflictException("BOOK_004", "One or more books cannot be updated due to state conflicts.");
BookStateConflictException exception = assertThrows(BookStateConflictException.class, () -> {
grpcUpdateBookStateService.rentalBook(rentalBookRequest, rentalResponseObserver);
});

RentalBookResponse expectedResponse = RentalBookResponse.newBuilder()
.setMessage("Books could not be rented due to state conflicts.")
.build();

verify(rentalResponseObserver).onNext(expectedResponse);
verify(rentalResponseObserver).onCompleted();

assertEquals(expectedException.getMessage(), exception.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.ssu.commerce.book.dto.param.RegisterBookParamDto;
import com.ssu.commerce.book.model.Book;
import com.ssu.commerce.book.model.Category;
import com.ssu.commerce.grpc.RentalBookRequest;
import com.ssu.commerce.grpc.UpdateBookStateRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand Down Expand Up @@ -345,6 +346,14 @@ static UpdateBookStateRequest getUpdateBookStateRequest() {
.build();
}

static RentalBookRequest getRentalBookRequest() {
return RentalBookRequest.newBuilder()
.setToken(TEST_VAL_ACCESS_TOKE)
.addId(TEST_VAL_BOOK_ID.toString())
.addId(TEST_VAL_ANOTHER_BOOK_ID.toString())
.build();
}

static List<Book> getBookListForGrpc() {
return List.of(
Book.builder()
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ maven-repository=https://maven.pkg.github.com/ssu-commerce/book-server
docker-path=ghcr.io/ssu-commerce/book-server
mapstructVersion=1.5.5.Final
queryDSLVersion=5.0.0
grpcVersion=0.0.29
grpcVersion=0.0.30
protobufVersion=3.15.0
protobufPluginVersion=0.8.14
buildGrpcVersion=1.53.0
buildGrpcVersion=1.53.0
21 changes: 14 additions & 7 deletions grpc/src/main/proto/UpdatebookState.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import "google/protobuf/empty.proto";
option java_multiple_files = true;
option java_package = "com.ssu.commerce.grpc";


service UpdateBookState {
rpc UpdateBookState (UpdateBookStateRequest) returns (google.protobuf.Empty) {

}
rpc updateBookState (UpdateBookStateRequest) returns (google.protobuf.Empty);
rpc rentalBook (RentalBookRequest) returns (RentalBookResponse);
}

message UpdateBookStateRequest {
Expand All @@ -18,8 +16,17 @@ message UpdateBookStateRequest {
repeated string id = 3;
}

message RentalBookRequest {
string token = 1;
repeated string id = 2;
}

message RentalBookResponse {
string message = 1;
}

enum BookState {
DISSHAREABLE = 0;
SHARABLE = 1;
SHARING = 2;
DISSHAREABLE = 0;
SHARABLE = 1;
SHARING = 2;
}
Loading