Skip to content

Commit

Permalink
test: 모든 util 클래스 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
peeerr committed Nov 7, 2024
1 parent 8191df6 commit 69f9291
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 22 deletions.
28 changes: 28 additions & 0 deletions src/test/java/com/potatocake/everymoment/util/IdExtractorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.potatocake.everymoment.util;

import static org.assertj.core.api.Assertions.assertThat;

import com.potatocake.everymoment.entity.Member;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class IdExtractorTest {

@Test
@DisplayName("IdExtractor 가 성공적으로 ID를 추출한다.")
void should_ExtractId_When_ValidInput() {
// given
Member member = Member.builder()
.id(1L)
.build();

IdExtractor<Member> extractor = Member::getId;

// when
Long extractedId = extractor.extractId(member);

// then
assertThat(extractedId).isEqualTo(1L);
}

}
127 changes: 127 additions & 0 deletions src/test/java/com/potatocake/everymoment/util/JwtUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.potatocake.everymoment.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.test.util.ReflectionTestUtils;

class JwtUtilTest {

private JwtUtil jwtUtil;
private static final String TEST_SECRET = "dGVzdF9zZWNyZXRfdGVzdF9zZWNyZXRfdGVzdF9zZWNyZXRfdGVzdF9zZWNyZXRfdGVzdF9zZWNyZXQ=";

@BeforeEach
void setUp() {
jwtUtil = new JwtUtil();
ReflectionTestUtils.setField(jwtUtil, "secret", TEST_SECRET);
jwtUtil.init();
}

@Test
@DisplayName("토큰이 성공적으로 생성된다.")
void should_CreateToken_When_ValidInput() {
// given
Long id = 1L;

// when
String token = jwtUtil.create(id);

// then
assertThat(token).isNotEmpty();
assertThatCode(() -> jwtUtil.getId(token))
.doesNotThrowAnyException();
assertThat(jwtUtil.getId(token)).isEqualTo(id);
}

@Test
@DisplayName("유효한 토큰에서 ID가 성공적으로 추출된다.")
void should_ExtractId_When_ValidToken() {
// given
Long expectedId = 1L;
String token = jwtUtil.create(expectedId);

// when
Long extractedId = jwtUtil.getId(token);

// then
assertThat(extractedId).isEqualTo(expectedId);
}

@Test
@DisplayName("만료되지 않은 토큰은 유효하다고 판단된다.")
void should_ReturnFalse_When_TokenNotExpired() {
// given
String token = jwtUtil.create(1L);

// when
boolean isExpired = jwtUtil.isExpired(token);

// then
assertThat(isExpired).isFalse();
}

@Test
@DisplayName("잘못된 형식의 토큰은 만료되었다고 판단된다.")
void should_ReturnTrue_When_InvalidToken() {
// given
String invalidToken = "invalid.token.format";

// when
boolean isExpired = jwtUtil.isExpired(invalidToken);

// then
assertThat(isExpired).isTrue();
}

@Test
@DisplayName("Authorization 헤더에서 토큰이 성공적으로 추출된다.")
void should_ResolveToken_When_ValidAuthorizationHeader() {
// given
String token = "valid-token";
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(HttpHeaders.AUTHORIZATION, jwtUtil.PREFIX + token);

// when
Optional<String> resolvedToken = jwtUtil.resolveToken(request);

// then
assertThat(resolvedToken)
.isPresent()
.contains(token);
}

@Test
@DisplayName("Authorization 헤더가 없으면 빈 Optional 이 반환된다.")
void should_ReturnEmpty_When_NoAuthorizationHeader() {
// given
HttpServletRequest request = new MockHttpServletRequest();

// when
Optional<String> resolvedToken = jwtUtil.resolveToken(request);

// then
assertThat(resolvedToken).isEmpty();
}

@Test
@DisplayName("Bearer 접두사가 없는 Authorization 헤더는 빈 Optional 을 반환한다.")
void should_ReturnEmpty_When_NoBearerPrefix() {
// given
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader(HttpHeaders.AUTHORIZATION, "invalid-format-token");

// when
Optional<String> resolvedToken = jwtUtil.resolveToken(request);

// then
assertThat(resolvedToken).isEmpty();
}

}
112 changes: 90 additions & 22 deletions src/test/java/com/potatocake/everymoment/util/PagingUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.potatocake.everymoment.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.domain.Sort.Direction.ASC;

import com.potatocake.everymoment.entity.Member;
import java.util.List;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Window;

class PagingUtilTest {
Expand All @@ -24,52 +23,121 @@ void setUp() {

@Test
@DisplayName("스크롤 위치가 성공적으로 생성된다.")
void should_CreateScrollPosition_When_KeyIsNull() {
void should_CreateScrollPosition_When_GivenKey() {
// given
Long key = 1L;

// when
ScrollPosition position = pagingUtil.createScrollPosition(key);

// then
assertThat(position).isNotNull();
}

@Test
@DisplayName("key 가 null 일 때 offset 스크롤 위치가 생성된다.")
void should_CreateOffsetPosition_When_KeyIsNull() {
// given
Long key = null;

// when
ScrollPosition position = pagingUtil.createScrollPosition(null);
ScrollPosition position = pagingUtil.createScrollPosition(key);

// then
Assertions.assertThat(position).isNotNull();
assertThat(position).isEqualTo(ScrollPosition.offset());
}

@Test
@DisplayName("페이지 정보가 성공적으로 생성된다.")
void should_CreatePageable_When_ValidSizeProvided() {
void should_CreatePageable_When_ValidInput() {
// given
int size = 10;
Direction direction = Direction.DESC;

// when
Pageable pageable = pagingUtil.createPageable(10, ASC);
Pageable pageable = pagingUtil.createPageable(size, direction);

// then
assertThat(pageable).isNotNull();
assertThat(pageable.getPageSize()).isEqualTo(10);
assertThat(pageable.getPageSize()).isEqualTo(size);
assertThat(pageable.getSort().getOrderFor("id").getDirection()).isEqualTo(direction);
}

@Test
@DisplayName("다음 페이지 키가 성공적으로 반환된다.")
void should_ReturnNextKey_When_WindowHasNext() {
@DisplayName("페이지 정보가 올바른 정렬 순서를 가진다.")
void should_CreatePageableWithCorrectSort_When_DirectionGiven() {
// given
List<Member> members = List.of(Member.builder().id(1L).build());
Window<Member> window = Window.from(members, ScrollPosition::offset, true);
int size = 10;
Direction direction = Direction.ASC;

// when
Long nextKey = pagingUtil.getNextKey(window, Member::getId);
Pageable pageable = pagingUtil.createPageable(size, direction);

// then
assertThat(nextKey).isNotNull();
assertThat(nextKey).isEqualTo(1L);
Sort sort = pageable.getSort();
assertThat(sort.getOrderFor("id")).isNotNull();
assertThat(sort.getOrderFor("id").getDirection()).isEqualTo(direction);
}

@Test
@DisplayName("다음 페이지가 존재하지 않을 때, 키 값으로 null 을 반환한다.")
void should_ReturnNull_When_WindowHasNoNext() {
@DisplayName("다음 키가 성공적으로 생성된다.")
void should_GetNextKey_When_ValidWindow() {
// given
List<Member> members = List.of(Member.builder().id(1L).build());
Window<Member> window = Window.from(members, ScrollPosition::offset, false);
TestEntity entity1 = new TestEntity(1L);
TestEntity entity2 = new TestEntity(2L);

List<TestEntity> content = List.of(entity1, entity2);
ScrollPosition scrollPosition = ScrollPosition.offset();

Window<TestEntity> window = Window.from(content, i -> scrollPosition, true);

// when
Long nextKey = pagingUtil.getNextKey(window, Member::getId);
Long nextKey = pagingUtil.getNextKey(window, TestEntity::getId);

// then
assertThat(nextKey).isEqualTo(2L);
}

@Test
@DisplayName("다음 페이지가 없으면 null 을 반환한다.")
void should_ReturnNull_When_NoNextPage() {
// given
TestEntity entity = new TestEntity(1L);
ScrollPosition scrollPosition = ScrollPosition.offset();

Window<TestEntity> window = Window.from(List.of(entity), i -> scrollPosition, false);

// when
Long nextKey = pagingUtil.getNextKey(window, TestEntity::getId);

// then
assertThat(nextKey).isNull();
}

@Test
@DisplayName("빈 윈도우에 대해 null 을 반환한다.")
void should_ReturnNull_When_EmptyWindow() {
// given
ScrollPosition scrollPosition = ScrollPosition.offset();

Window<TestEntity> window = Window.from(List.of(), i -> scrollPosition, false);

// when
Long nextKey = pagingUtil.getNextKey(window, TestEntity::getId);

// then
assertThat(nextKey).isNull();
}

private static class TestEntity {
private final Long id;

TestEntity(Long id) {
this.id = id;
}

public Long getId() {
return id;
}
}

}
Loading

0 comments on commit 69f9291

Please sign in to comment.