Skip to content

Commit

Permalink
refactor: 테스트를 위한 설계 유연성 개선 (User테스트)
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed Nov 7, 2024
1 parent 95a5294 commit 2d21a80
Show file tree
Hide file tree
Showing 71 changed files with 1,140 additions and 454 deletions.
31 changes: 31 additions & 0 deletions src/main/java/com/tukorea/planding/common/JsonConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.tukorea.planding.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RequiredArgsConstructor
public class JsonConverter {

private final ObjectMapper objectMapper;

public String convertObjectToJson(Object obj) {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
return null;
}
}

public <T> T convertJsonToObject(String json, Class<T> clazz) {
try {
return objectMapper.readValue(json, clazz);
} catch (IOException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.tukorea.planding.common.infrastructure;

import com.tukorea.planding.common.service.UserCodeHolder;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
public class SystemUserCode implements UserCodeHolder {
@Override
public String userCode() {
return "#" + UUID.randomUUID().toString().substring(0, 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tukorea.planding.common.service;

public interface UserCodeHolder {
String userCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@

import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import com.tukorea.planding.domain.chat.config.RedisChatService;
import com.tukorea.planding.domain.chat.dto.MessageRequest;
import com.tukorea.planding.domain.chat.dto.MessageResponse;
import com.tukorea.planding.domain.chat.service.ChatService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.dto.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

Expand All @@ -28,15 +22,15 @@ public class ChatController {
private final ChatService chatService;

@PostMapping("/{groupCode}")
public CommonResponse<MessageResponse> sendMessage(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode, @RequestBody MessageRequest request) {
MessageResponse messageResponse = chatService.sendMessage(userInfo.getUserCode(), request, groupCode);
public CommonResponse<MessageResponse> sendMessage(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode, @RequestBody MessageRequest request) {
MessageResponse messageResponse = chatService.sendMessage(userResponse.getUserCode(), request, groupCode);
return CommonUtils.success(messageResponse);
}

@Operation(summary = "채팅방 내용 불러오기")
@GetMapping("/{groupCode}/message")
public CommonResponse<List<MessageResponse>> getMessages(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
List<MessageResponse> responses = chatService.getMessages(userInfo, groupCode);
public CommonResponse<List<MessageResponse>> getMessages(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode) {
List<MessageResponse> responses = chatService.getMessages(userResponse, groupCode);
return CommonUtils.success(responses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import com.tukorea.planding.domain.chat.entity.ChatMessage;
import com.tukorea.planding.domain.chat.repository.ChatMessageRepository;
import com.tukorea.planding.domain.group.service.query.UserGroupQueryService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.dto.UserResponse;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.entity.UserDomain;
import com.tukorea.planding.domain.user.service.UserQueryService;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
Expand All @@ -30,7 +31,7 @@ public class ChatService {
private final UserGroupQueryService userGroupQueryService;

public MessageResponse sendMessage(String sender, MessageRequest messageDTO, String groupCode) {
User user = userQueryService.getUserByUserCode(sender);
UserDomain user = userQueryService.getUserByUserCode(sender);

ChatMessage chatMessage = ChatMessage.builder()
.content(messageDTO.getContent())
Expand All @@ -53,8 +54,8 @@ public MessageResponse sendMessage(String sender, MessageRequest messageDTO, Str
return response;
}

public List<MessageResponse> getMessages(UserInfo userInfo, String groupCode) {
if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userInfo.getUserCode())) {
public List<MessageResponse> getMessages(UserResponse userResponse, String groupCode) {
if (!userGroupQueryService.checkUserAccessToGroupRoom(groupCode, userResponse.getUserCode())) {
throw new BusinessException(ErrorCode.ACCESS_DENIED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.tukorea.planding.domain.group.dto.response.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.dto.response.GroupResponse;
import com.tukorea.planding.domain.group.service.GroupFavoriteService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.dto.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,21 +24,21 @@ public class GroupFavoriteController {

@Operation(summary = "즐겨찾기 그룹 조회")
@GetMapping()
public CommonResponse<List<GroupResponse>> searchFavorite(@AuthenticationPrincipal UserInfo userInfo) {
return CommonUtils.success(groupFavoriteService.findFavoriteGroupsByUser(userInfo));
public CommonResponse<List<GroupResponse>> searchFavorite(@AuthenticationPrincipal UserResponse userResponse) {
return CommonUtils.success(groupFavoriteService.findFavoriteGroupsByUser(userResponse));
}

@Operation(summary = "그룹 즐겨찾기 추가")
@PostMapping("/{groupCode}")
public CommonResponse<GroupFavoriteResponse> addFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
GroupFavoriteResponse response = groupFavoriteService.addFavorite(userInfo, groupCode);
public CommonResponse<GroupFavoriteResponse> addFavorite(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode) {
GroupFavoriteResponse response = groupFavoriteService.addFavorite(userResponse, groupCode);
return CommonUtils.success(response);
}

@Operation(summary = "그룹 즐겨찾기 해제")
@DeleteMapping("/{groupCode}")
public CommonResponse<?> deleteFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
groupFavoriteService.deleteFavorite(userInfo, groupCode);
public CommonResponse<?> deleteFavorite(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode) {
groupFavoriteService.deleteFavorite(userResponse, groupCode);
return CommonUtils.success("즐겨찾기 해제 완료.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.tukorea.planding.domain.group.dto.response.GroupInviteAcceptResponse;
import com.tukorea.planding.domain.group.dto.response.GroupInviteMessageResponse;
import com.tukorea.planding.domain.group.service.GroupInviteService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.dto.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,28 +24,28 @@ public class GroupInviteController {

@Operation(summary = "유저에게 초대를 보낸다")
@PostMapping()
public CommonResponse<GroupInviteMessageResponse> invite(@AuthenticationPrincipal UserInfo userInfo, @RequestBody GroupInviteRequest groupInviteRequest) {
return CommonUtils.success(groupInviteService.inviteGroupRoom(userInfo, groupInviteRequest));
public CommonResponse<GroupInviteMessageResponse> invite(@AuthenticationPrincipal UserResponse userResponse, @RequestBody GroupInviteRequest groupInviteRequest) {
return CommonUtils.success(groupInviteService.inviteGroupRoom(userResponse, groupInviteRequest));
}

@Operation(summary = "초대를 수락한다")
@GetMapping("/accept/{groupCode}/{inviteCode}")
public CommonResponse<GroupInviteAcceptResponse> accept(@AuthenticationPrincipal UserInfo userInfo, @PathVariable(name = "groupCode") String groupCode, @PathVariable(name = "inviteCode") String code) {
GroupInviteAcceptResponse response = groupInviteService.acceptInvitation(userInfo, code, groupCode);
public CommonResponse<GroupInviteAcceptResponse> accept(@AuthenticationPrincipal UserResponse userResponse, @PathVariable(name = "groupCode") String groupCode, @PathVariable(name = "inviteCode") String code) {
GroupInviteAcceptResponse response = groupInviteService.acceptInvitation(userResponse, code, groupCode);
return CommonUtils.success(response);
}

@Operation(summary = "초대를 받은 목록", description = "아직 초대의 상태를 바꾸지 않은 경우만")
@GetMapping()
public CommonResponse<List<GroupInviteMessageResponse>> getInvitations(@AuthenticationPrincipal UserInfo userInfo) {
List<GroupInviteMessageResponse> groupInviteResponse = groupInviteService.getInvitations(userInfo);
public CommonResponse<List<GroupInviteMessageResponse>> getInvitations(@AuthenticationPrincipal UserResponse userResponse) {
List<GroupInviteMessageResponse> groupInviteResponse = groupInviteService.getInvitations(userResponse);
return CommonUtils.success(groupInviteResponse);
}

@Operation(summary = "초대를 거절한다.")
@DeleteMapping("/decline/{inviteCode}")
public CommonResponse<?> declineInvitation(@AuthenticationPrincipal UserInfo userInfo, @PathVariable(name = "inviteCode") String code) {
groupInviteService.declineInvitation(userInfo, code);
public CommonResponse<?> declineInvitation(@AuthenticationPrincipal UserResponse userResponse, @PathVariable(name = "inviteCode") String code) {
groupInviteService.declineInvitation(userResponse, code);
return CommonUtils.success("거절하였습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
import com.tukorea.planding.domain.group.dto.response.GroupInformationResponse;
import com.tukorea.planding.domain.group.dto.response.GroupResponse;
import com.tukorea.planding.domain.group.service.GroupRoomService;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.dto.UserResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand All @@ -30,49 +29,49 @@ public class GroupRoomController {

@Operation(summary = "메인페이지 API", description = "내 그룹 가져오기")
@GetMapping("/paging")
public CommonResponse<List<GroupResponse>> getAllGroupRoomByUser(@AuthenticationPrincipal UserInfo userInfo,
public CommonResponse<List<GroupResponse>> getAllGroupRoomByUser(@AuthenticationPrincipal UserResponse userResponse,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "11") int size) {

PageRequest pageRequest = PageRequest.of(page, size);
List<GroupResponse> groupResponses = groupRoomService.getAllGroupRoomByUser(userInfo, pageRequest);
List<GroupResponse> groupResponses = groupRoomService.getAllGroupRoomByUser(userResponse, pageRequest);
return CommonUtils.success(groupResponses);
}

@Operation(summary = "그룹 정보 조회")
@GetMapping("/{groupCode}")
public CommonResponse<GroupInformationResponse> getUserByGroup(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
GroupInformationResponse responses = groupRoomService.getGroupUsers(userInfo, groupCode);
public CommonResponse<GroupInformationResponse> getUserByGroup(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode) {
GroupInformationResponse responses = groupRoomService.getGroupUsers(userResponse, groupCode);
return CommonUtils.success(responses);
}

@Operation(summary = "그룹 생성")
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public CommonResponse<GroupResponse> createGroupRoom(@AuthenticationPrincipal UserInfo userInfo,
public CommonResponse<GroupResponse> createGroupRoom(@AuthenticationPrincipal UserResponse userResponse,
@RequestPart(value = "request") GroupCreateRequest createGroupRoom,
@RequestPart(value = "thumbnail", required = false) MultipartFile thumbnailFile) {
GroupResponse groupResponse = groupRoomService.createGroupRoom(userInfo, createGroupRoom, thumbnailFile);
GroupResponse groupResponse = groupRoomService.createGroupRoom(userResponse, createGroupRoom, thumbnailFile);
return CommonUtils.success(groupResponse);
}

@Operation(summary = "그룹 정보 수정")
@PatchMapping()
public CommonResponse<GroupResponse> updateGroupNameOrDescription(@AuthenticationPrincipal UserInfo userInfo, @RequestBody GroupUpdateRequest groupUpdateRequest) {
GroupResponse groupResponses = groupRoomService.updateGroupNameOrDescription(userInfo, groupUpdateRequest);
public CommonResponse<GroupResponse> updateGroupNameOrDescription(@AuthenticationPrincipal UserResponse userResponse, @RequestBody GroupUpdateRequest groupUpdateRequest) {
GroupResponse groupResponses = groupRoomService.updateGroupNameOrDescription(userResponse, groupUpdateRequest);
return CommonUtils.success(groupResponses);
}

@Operation(summary = "그룹 삭제")
@DeleteMapping("/{groupCode}")
public CommonResponse<?> deleteGroup(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
groupRoomService.deleteGroup(userInfo, groupCode);
public CommonResponse<?> deleteGroup(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode) {
groupRoomService.deleteGroup(userResponse, groupCode);
return CommonUtils.success("그룹삭제 완료.");
}

@Operation(summary = "그룹 나가기")
@DeleteMapping("/leave/{groupCode}")
public CommonResponse<?> leaveGroup(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
groupRoomService.leaveGroup(userInfo, groupCode);
public CommonResponse<?> leaveGroup(@AuthenticationPrincipal UserResponse userResponse, @PathVariable String groupCode) {
groupRoomService.leaveGroup(userResponse, groupCode);
return CommonUtils.success("그룹 나가기 완료.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tukorea.planding.domain.group.entity.domain;

import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.entity.UserDomain;

public class GroupFavoriteDomain {

private Long id;

private UserDomain user;

private GroupRoomDomain groupRoom;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tukorea.planding.domain.group.entity.domain;

import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.UserGroup;
import com.tukorea.planding.domain.schedule.entity.GroupSchedule;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.entity.UserDomain;
import jakarta.persistence.JoinColumn;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class GroupRoomDomain {
private Long id;

private String name;

private String description;

private UserDomain owner;

private String groupCode; // 그룹방 고유 식별값

private String thumbnail;

private boolean alarm = true;

private final Set<UserGroupDomain> userGroups = new HashSet<>();

private final List<GroupSchedule> groupSchedules = new ArrayList<>();

private final List<GroupFavoriteDomain> groupFavorites = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.tukorea.planding.domain.group.entity.domain;


import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.user.entity.UserDomain;

public class UserGroupDomain {
private Long id;
private UserDomain user;

private GroupRoomDomain groupRoom;

private boolean isConnected;

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import static com.tukorea.planding.domain.group.entity.QUserGroup.userGroup;
import static com.tukorea.planding.domain.user.entity.QUser.user;


@Repository
@RequiredArgsConstructor
public class UserGroupRepositoryCustomImpl implements UserGroupRepositoryCustom {
Expand Down
Loading

0 comments on commit 2d21a80

Please sign in to comment.