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: 방 만들기 기능 구현 완료 #5

Merged
merged 72 commits into from
Mar 20, 2024
Merged

Conversation

yeonise
Copy link
Contributor

@yeonise yeonise commented Mar 18, 2024

1. 구현 내용 관련

getRemoteAddr()

  • getRemoteAddr() 을 통해 사용자 IP를 가져옵니다.
server:
  forward-headers-strategy: framework

블로그 글을 참고하여 위 설정을 추가했습니다.

Session Cookie

세션 쿠키와 관련된 로직은 컨트롤러 단에서 처리하고자 했습니다.

  • @CookieValue 를 사용하여 세션 쿠키가 존재하는지 확인하고, 존재한다면 이미 다른 방에 참가하고 있다고 판단했습니다.
  • 방을 성공적으로 생성한 경우에만 response에 생성했던 쿠키를 담아서 보내도록 했습니다. (방 생성 도중 오류가 발생한 경우 쿠키가 전송되지 않습니다.)

2. 테스트 및 API 문서 관련

RestDocsSupport : Controller test + REST Docs 문서 생성

  • @Import(PropertiesConfig.class) 를 추가하여 application-test.yml 에서 설정한 cookieProperties를 가져오도록 했습니다.
  • andExpect() 를 적극적으로 사용하여 보다 꼼꼼하게 검증하고자 했습니다.

IntegrationTestSupport : 통합 환경에서의 테스트

  • 중요하다고 생각하는 부분만 테스트 케이스로 남기고자 했습니다.

Asciidoc 문서 형식 커스터마이징

  • 사이드바 목록에서 현재 보고 있는 위치가 표시되도록 했습니다.
  • 가독성이 높은 한글 폰트로 변경했습니다.
  • 테이블에 Optional 컬럼을 추가했습니다.
  • 문서와 문서 사이에 공백을 추가했습니다.

test-report.yml workflow 변경

  • 테스트 수행을 위한 임시 redis 컨테이너를 생성하는 단계를 추가했습니다.
  • Actions Secret에 application.yml 파일들을 저장했고 이를 프로젝트에 복사하는 단계를 추가했습니다.

yeonise added 30 commits March 8, 2024 13:36
Body의 방 설정 정보(RoomSettings)를 참고하여 새로운 방을 생성합니다.
각 IP는 최대 1개의 방을 hosting 할 수 있습니다.
새로운 방 생성에 성공하면 room code를 반환합니다.
yeonise added 9 commits March 18, 2024 16:55
요청 데이터에서 오류가 발생한 경우에 대한 예제를 추가합니다. (BindException.class)
요청 데이터에서 오류가 발생한 경우에 대한 예제를 추가합니다. (BindException.class)
세션 쿠키를 갖고 있다는 것은 이미 방에 참가 중임을 암시합니다. 따라서 새로운 방을 생성할 수 없습니다.
세션 쿠키를 갖고 있다는 것은 이미 방에 참가 중임을 암시합니다. 따라서 새로운 방을 생성할 수 없습니다.

This comment was marked as resolved.

This comment was marked as resolved.

Copy link

github-actions bot commented Mar 18, 2024

Test Results

4 tests  +3   4 ✅ +3   3s ⏱️ +3s
2 suites +1   0 💤 ±0 
2 files   +1   0 ❌ ±0 

Results for commit a8b230d. ± Comparison against base commit 16c677c.

♻️ This comment has been updated with latest results.

@yeonise
Copy link
Contributor Author

yeonise commented Mar 18, 2024

Test Coverage Report

Overall Project 94.7% -4.71% 🍏
Files changed 95.24% 🍏

File Coverage
CustomException.java 100% 🍏
ErrorType.java 100% 🍏
GlobalExceptionHandler.java 100% 🍏
RandomUtil.java 100% 🍏
ResponseResult.java 100% 🍏
SingleRoomParticipationViolationException.java 100% 🍏
Role.java 100% 🍏
RoomController.java 100% 🍏
RoomService.java 100% 🍏
CookieProperties.java 100% 🍏
WebConfig.java 100% 🍏
PropertiesConfig.java 100% 🍏
RedisConfig.java 100% 🍏
RoomCode.java 100% 🍏
Room.java 90.74% -9.26% 🍏
RoomSettings.java 87.5% -12.5% 🍏
User.java 86.84% -13.16% 🍏
ApiResponse.java 86.27% -13.73% 🍏
AddressArgumentResolver.java 86.11% -13.89% 🍏

@yeonise yeonise requested a review from ghkdgus29 March 19, 2024 03:19
WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);

assert request != null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert 를 사용하면 얻는 효과가 뭔가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

초기 단계에서 객체를 확인을 하여 나중에 더 모호한 NullPointerException으로 실패하는 것을 방지합니다.
위 코드는 IDE에서 제안하는 사항 중 하나입니다.

좀 더 개선된 응답을 제공하고자 한다면, 다음과 같이 작성할 수 있습니다.

Suggested change
assert request != null;
if (request == null) {
throw new IllegalArgumentException("HttpServletRequest는 null일 수 없습니다");
}

@Range(min = 2, max = 10)
private int capacity;

@Pattern(regexp = "^[0-9a-zA-Z]{5,10}$")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 입력 형식이 잘못되었을때, 어떤 에러 메시지를 응답하나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

기본 설정값으로 응답합니다.
더 자세한 응답 메시지를 원하신다면 message=" " 를 추가해도 좋습니다.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 응답 예시입니다.

{
    "code": 400,
    "status": "Bad Request",
    "result": "예외가 발생했습니다",
    "data": [
        {
            "type": "password",
            "message": "must match \"^[0-9a-zA-Z]{5,10}$\""
        },
        {
            "type": "title",
            "message": "must not be blank"
        },
        {
            "type": "capacity",
            "message": "must be between 2 and 10"
        }
    ]
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메세지를 추가하는게 더 좋아보입니다~

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다음과 같이 메시지를 추가했습니다.

{
    "code": 400,
    "status": "Bad Request",
    "result": "예외가 발생했습니다",
    "data": [
        {
            "type": "password",
            "message": "비밀번호는 5자 이상 10자 이하의 영문 또는 숫자로 입력해 주세요"
        },
        {
            "type": "title",
            "message": "공백이 아닌 문자를 1개 이상 입력해 주세요"
        },
        {
            "type": "capacity",
            "message": "정원은 2명 이상 10명 이하로 입력해 주세요"
        }
    ]
}
{
    "code": 400,
    "status": "Bad Request",
    "result": "예외가 발생했습니다",
    "data": [
        {
            "type": "password",
            "message": "비밀번호는 5자 이상 10자 이하의 영문 또는 숫자로 입력해 주세요"
        },
        {
            "type": "title",
            "message": "제목은 1자 이상 30자 이하로 입력해 주세요"
        },
        {
            "type": "capacity",
            "message": "정원은 2명 이상 10명 이하로 입력해 주세요"
        }
    ]
}

"GoldenGlider", "CrimsonFalcon", "EchoingWhisper", "EmberPhoenix", "RadiantRebel"
);

int randomIndex = ThreadLocalRandom.current().nextInt(samples.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분, 설명해줄 수 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThreadLocalRandom은 난수를 생성하는 클래스입니다.
각 스레드마다 고유한 난수 생성기를 사용하기 때문에 서로 간섭받지 않고 독립적인 수를 생성할 수 있습니다.
또한 동기화를 사용하지 않기 때문에 Random 클래스보다 성능적인 이점이 있습니다.

  • .current() 는 현재 스레드에서 사용할 ThreadLocalRandom 인스턴스를 반환합니다.
  • .nextInt(int bound) 는 bound 내의 난수를 생성합니다. (이때, bound 값을 포함하지 않습니다.)

참고


// then
Room room = roomStorage.findById(roomCode.getRoomCode()).orElseThrow();
User user = userStorage.findById(sessionCode).orElseThrow();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋ optional 억지로 가져오고 싶으면 그냥 .get 사용해도 될거같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋ 굿

@ghkdgus29
Copy link
Contributor

잘봤습니다~!!!
로직, 테스트 코드, 응답이나 예외처리 다방면에서 고민하신 흔적이 보이네요.

역시 yeonise..

@yeonise
Copy link
Contributor Author

yeonise commented Mar 20, 2024

Test Coverage Report

Overall Project 94.7% -4.71% 🍏
Files changed 95.24% 🍏

File Coverage
CustomException.java 100% 🍏
ErrorType.java 100% 🍏
GlobalExceptionHandler.java 100% 🍏
RandomUtil.java 100% 🍏
ResponseResult.java 100% 🍏
SingleRoomParticipationViolationException.java 100% 🍏
Role.java 100% 🍏
RoomController.java 100% 🍏
RoomService.java 100% 🍏
CookieProperties.java 100% 🍏
WebConfig.java 100% 🍏
PropertiesConfig.java 100% 🍏
RedisConfig.java 100% 🍏
RoomCode.java 100% 🍏
Room.java 90.74% -9.26% 🍏
RoomSettings.java 87.5% -12.5% 🍏
User.java 86.84% -13.16% 🍏
ApiResponse.java 86.27% -13.73% 🍏
AddressArgumentResolver.java 86.11% -13.89% 🍏

@yeonise yeonise merged commit 1218ae2 into dev Mar 20, 2024
2 checks passed
@yeonise yeonise linked an issue Mar 20, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature: 방 만들기 기능 구현
2 participants