Skip to content

Commit

Permalink
refactor: 리뷰 피드백 및 테스트 코드 추가 구현 (#194)
Browse files Browse the repository at this point in the history
* feat: 리뷰 작성을 위한 모임 참여자 조회 응답 DTO 변경 (#163)

* refactor: AlertHandler 기존 코드로 변경 (#163)

* refactor: MeetingController 모임 참여자 조회 응답 DTO 변경 (#163)

* refactor: MeetingController 모임 참여자 조회 응답 DTO 변경 (#163)

* Revert "refactor: MeetingController 모임 참여자 조회 응답 DTO 변경 (#163)"

This reverts commit 3dc30b3.

* refactor: MeetingController 모임 참여자 조회 응답 DTO 변경 (#163)

* Revert "refactor: MeetingController 모임 참여자 조회 응답 DTO 변경 (#163)"

This reverts commit 2b3c47c.

* refactor: MeetingService 모임 참여자 조회 응답 DTO 변경 (#163)

* refactor: UserService Assert.isTrue 사용으로 변경 (#163)

* test: UserControllerTest 컨트롤러 단위 테스트 고도화 (#163)

* test: UserServiceTest 서비스 단위 테스트 고도화 (#163)

* test: MeetingIntegrationTest 모임 참여자 조회 관련 추가 구현 (#163)
  • Loading branch information
choidongkuen authored Feb 11, 2024
1 parent 016c3c5 commit f3699f1
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/teumteum/alert/app/AlertHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private String toCommaString(List<Long> ids) {
for (int i = 0; i < ids.size() - 1; i++) {
stringBuilder.append(ids.get(i)).append(",");
}
stringBuilder.append(ids.get(ids.size() - 1));
stringBuilder.append(ids.getLast());
return stringBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.teumteum.meeting.domain.Topic;
import net.teumteum.meeting.domain.request.CreateMeetingRequest;
import net.teumteum.meeting.domain.request.UpdateMeetingRequest;
import net.teumteum.meeting.domain.response.MeetingParticipantsResponse;
import net.teumteum.meeting.domain.response.MeetingParticipantsForReviewResponse;
import net.teumteum.meeting.domain.response.MeetingResponse;
import net.teumteum.meeting.domain.response.MeetingsResponse;
import net.teumteum.meeting.model.PageDto;
Expand Down Expand Up @@ -40,8 +40,7 @@ public class MeetingController {

@PostMapping()
@ResponseStatus(HttpStatus.CREATED)
public MeetingResponse createMeeting(
@RequestPart @Valid CreateMeetingRequest meetingRequest,
public MeetingResponse createMeeting(@RequestPart @Valid CreateMeetingRequest meetingRequest,
@RequestPart List<MultipartFile> images) {
Long userId = securityService.getCurrentUserId();
return meetingService.createMeeting(images, meetingRequest, userId);
Expand All @@ -56,10 +55,8 @@ public MeetingResponse getMeetingById(@PathVariable("meetingId") Long meetingId)

@GetMapping
@ResponseStatus(HttpStatus.OK)
public PageDto<MeetingsResponse> getMeetingsByCondition(
Pageable pageable,
@RequestParam(value = "isOpen") boolean isOpen,
@RequestParam(value = "topic", required = false) Topic topic,
public PageDto<MeetingsResponse> getMeetingsByCondition(Pageable pageable,
@RequestParam(value = "isOpen") boolean isOpen, @RequestParam(value = "topic", required = false) Topic topic,
@RequestParam(value = "meetingAreaStreet", required = false) String meetingAreaStreet,
@RequestParam(value = "participantUserId", required = false) Long participantUserId,
@RequestParam(value = "searchWord", required = false) String searchWord) {
Expand All @@ -70,8 +67,7 @@ public PageDto<MeetingsResponse> getMeetingsByCondition(

@PutMapping("/{meetingId}")
@ResponseStatus(HttpStatus.OK)
public MeetingResponse updateMeeting(@PathVariable Long meetingId,
@RequestPart @Valid UpdateMeetingRequest request,
public MeetingResponse updateMeeting(@PathVariable Long meetingId, @RequestPart @Valid UpdateMeetingRequest request,
@RequestPart List<MultipartFile> images) {
Long userId = securityService.getCurrentUserId();
return meetingService.updateMeeting(meetingId, images, request, userId);
Expand Down Expand Up @@ -100,7 +96,7 @@ public void deleteParticipant(@PathVariable("meetingId") Long meetingId) {

@GetMapping("/{meetingId}/participants")
@ResponseStatus(HttpStatus.OK)
public List<MeetingParticipantsResponse> getParticipants(@PathVariable("meetingId") Long meetingId) {
public MeetingParticipantsForReviewResponse getParticipants(@PathVariable("meetingId") Long meetingId) {
return meetingService.getParticipants(meetingId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.teumteum.meeting.domain.response;

import java.util.List;
import net.teumteum.user.domain.User;

public record MeetingParticipantsForReviewResponse(
List<MeetingParticipantForReviewResponse> participants
) {

public static MeetingParticipantsForReviewResponse of(List<User> users) {
return new MeetingParticipantsForReviewResponse(
users.stream()
.map(user -> new MeetingParticipantForReviewResponse(
user.getId(),
user.getCharacterId(),
user.getName(),
user.getJob().getDetailJobClass()))
.toList()
);
}

public record MeetingParticipantForReviewResponse(
Long id,
Long characterId,
String name,
String job
) {

}
}

This file was deleted.

10 changes: 6 additions & 4 deletions src/main/java/net/teumteum/meeting/service/MeetingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
import net.teumteum.meeting.domain.Topic;
import net.teumteum.meeting.domain.request.CreateMeetingRequest;
import net.teumteum.meeting.domain.request.UpdateMeetingRequest;
import net.teumteum.meeting.domain.response.MeetingParticipantsResponse;
import net.teumteum.meeting.domain.response.MeetingParticipantsForReviewResponse;
import net.teumteum.meeting.domain.response.MeetingResponse;
import net.teumteum.meeting.domain.response.MeetingsResponse;
import net.teumteum.meeting.model.PageDto;
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserConnector;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -153,14 +154,15 @@ public void cancelParticipant(Long meetingId, Long userId) {
}

@Transactional(readOnly = true)
public List<MeetingParticipantsResponse> getParticipants(Long meetingId) {
public MeetingParticipantsForReviewResponse getParticipants(Long meetingId) {
var existMeeting = getMeeting(meetingId);

return existMeeting.getParticipantUserIds().stream()
List<User> participantUsers = existMeeting.getParticipantUserIds().stream()
.map(userConnector::findUserById)
.flatMap(Optional::stream)
.map(MeetingParticipantsResponse::of)
.toList();

return MeetingParticipantsForReviewResponse.of(participantUsers);
}

@Transactional
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/net/teumteum/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ public InterestQuestionResponse getInterestQuestionByUserIds(List<Long> userIds,
}

private void checkUserExistence(Authenticated authenticated, String oauthId) {
userRepository.findByAuthenticatedAndOAuthId(authenticated, oauthId)
.ifPresent(user -> {
boolean userExists = userRepository.findByAuthenticatedAndOAuthId(authenticated, oauthId).isPresent();
Assert.isTrue(!userExists, () -> {
throw new IllegalArgumentException("일치하는 user 가 이미 존재합니다.");
});
}
);
}

private void checkMeetingExistence(Long meetingId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package net.teumteum.integration;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;
import net.teumteum.core.error.ErrorResponse;
import net.teumteum.meeting.domain.Meeting;
import net.teumteum.meeting.domain.Topic;
import net.teumteum.meeting.domain.response.MeetingParticipantsForReviewResponse;
import net.teumteum.meeting.domain.response.MeetingParticipantsForReviewResponse.MeetingParticipantForReviewResponse;
import net.teumteum.meeting.domain.response.MeetingResponse;
import net.teumteum.meeting.domain.response.MeetingsResponse;
import net.teumteum.meeting.model.PageDto;
Expand Down Expand Up @@ -484,7 +490,21 @@ void Get_participants_if_exist_meeting_id_received() {
// when
var result = api.getMeetingParticipants(VALID_TOKEN, meeting.getId());
// then
result.expectStatus().isOk();
result.expectStatus().isOk()
.expectBody(MeetingParticipantsForReviewResponse.class)
.consumeWith(response -> {
MeetingParticipantsForReviewResponse responseBody = response.getResponseBody();
assertNotNull(responseBody);
List<MeetingParticipantForReviewResponse> participants = responseBody.participants();
assertNotNull(participants);

assertTrue(participants.stream().allMatch(participant ->
participant.id() != null &&
participant.characterId() != null &&
participant.name() != null &&
participant.job() != null
));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,26 @@ void Register_user_card_with_201_created() throws Exception {
.andExpect(jsonPath("$.refreshToken").value(VALID_REFRESH_TOKEN));
}

@Test
@DisplayName("이미 카드 등록한 사용자의 등록 요청값이 주어지면, 400 Bad Request을 반환한다.")
void Return_400_bad_request_if_user_already_exist() throws Exception {
// given
UserRegisterRequest request = RequestFixture.userRegisterRequest(user);

given(userService.register(any(UserRegisterRequest.class)))
.willThrow(new IllegalArgumentException("일치하는 user 가 이미 존재합니다."));

// when && then
mockMvc.perform(post("/users")
.content(objectMapper.writeValueAsString(request))
.contentType(APPLICATION_JSON)
.with(csrf())
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.message").value("일치하는 user 가 이미 존재합니다."));
}

@Test
@DisplayName("유효하지 않은 사용자의 등록 요청값이 주어지면, 400 Bad Request 상태값을 반환한다.")
void Register_user_card_with_400_bad_request() throws Exception {
Expand Down Expand Up @@ -144,6 +164,26 @@ void Withdraw_user_with_200_ok() throws Exception {
.andDo(print())
.andExpect(status().isOk());
}

@Test
@DisplayName("회원 탈퇴 하고자 하는 회원이 존재하지 않으면")
void Return_400_bad_request_if_user_is_not_exist() throws Exception {
// given
UserWithdrawRequest request
= RequestFixture.userWithdrawRequest(List.of("쓰지 않는 앱이에요", "오류가 생겨서 쓸 수 없어요"));

doThrow(new IllegalArgumentException("일치하는 user가 이미 존재합니다.")).when(userService).withdraw(any(
UserWithdrawRequest.class), anyLong());

// when && then
mockMvc.perform(post("/users/withdraws")
.content(objectMapper.writeValueAsString(request))
.contentType(APPLICATION_JSON)
.with(csrf())
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isBadRequest());
}
}

@Nested
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/net/teumteum/unit/user/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ void If_valid_user_withdraw_request_withdraw_user() {
verify(redisService, times(1)).deleteData(anyString());
verify(withdrawReasonRepository, times(1)).saveAll(any());
}

@Test
@DisplayName("유저 id에 해당하는 유저가 존재하지 않는 경우, 400 Bad Request 을 반환한다.")
void Return_400_bad_request_if_user_is_not_exist() {
// given
UserWithdrawRequest request
= RequestFixture.userWithdrawRequest(List.of("쓰지 않는 앱이에요", "오류가 생겨서 쓸 수 없어요"));

long userId = 1L;

given(userRepository.findById(anyLong()))
.willThrow(new IllegalArgumentException("userId 에 해당하는 user를 찾을 수 없습니다."));

// When & Then
assertThatThrownBy(() -> userService.withdraw(request, userId))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("userId 에 해당하는 user를 찾을 수 없습니다.");
}
}

@Nested
Expand Down

0 comments on commit f3699f1

Please sign in to comment.