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

feat: 유저 리뷰 조회 기능 #152

Merged
merged 5 commits into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 26 additions & 2 deletions src/test/java/net/teumteum/integration/UserIntegrationTest.java
Copy link
Member Author

Choose a reason for hiding this comment

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

통합 테스트를 단위 테스트 이름으로 올렸네요 🥲

Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
import net.teumteum.core.error.ErrorResponse;
import net.teumteum.user.domain.User;
import net.teumteum.user.domain.UserFixture;
import net.teumteum.user.domain.request.ReviewRegisterRequest;
import net.teumteum.user.domain.response.FriendsResponse;
import net.teumteum.user.domain.response.UserGetResponse;
import net.teumteum.user.domain.response.UserMeGetResponse;
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.domain.response.UsersGetByIdResponse;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.test.web.reactive.server.WebTestClient.ResponseSpec;

@DisplayName("유저 통합테스트의")
class UserIntegrationTest extends IntegrationTest {
Expand Down Expand Up @@ -349,4 +348,29 @@ void Logout_user() {
.doesNotThrowAnyException();
}
}

@Nested
@DisplayName("회원 리뷰 조회 API는")
class Get_user_review_api {

@Test
@DisplayName("userId 유저의 리뷰 정보를 가져온다.")
void Get_user_review() {
// given
var existUser = repository.saveAndGetUser();

securityContextSetting.set(existUser.getId());

// when
var expected = api.getUserReviews(VALID_TOKEN);

// then
Assertions.assertThat(expected.expectStatus().isOk()
.expectBodyList(UserReviewsResponse.class)
.returnResult()
.getResponseBody())
.usingRecursiveComparison()
.isNotNull();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import static net.teumteum.unit.auth.common.SecurityValue.VALID_ACCESS_TOKEN;
import static net.teumteum.unit.auth.common.SecurityValue.VALID_REFRESH_TOKEN;
import static net.teumteum.user.domain.Review.별로에요;
import static net.teumteum.user.domain.Review.최고에요;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand All @@ -27,6 +34,7 @@
import net.teumteum.user.domain.request.UserRegisterRequest;
import net.teumteum.user.domain.request.UserWithdrawRequest;
import net.teumteum.user.domain.response.UserRegisterResponse;
import net.teumteum.user.domain.response.UserReviewsResponse;
import net.teumteum.user.service.UserService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -156,4 +164,34 @@ void Register_user_review_with_200_ok() throws Exception {
.andExpect(status().isOk());
}
}


@Nested
@DisplayName("회원 리뷰 조회 API는")
class Get_user_reviews_api_unit {

@Test
@DisplayName("로그인한 회원 id 에 해당하는 회원 리뷰와 200 OK을 반환한다.")
void Get_user_reviews_with_200_ok() throws Exception {
// given
var userId = 1L;

given(securityService.getCurrentUserId()).willReturn(userId);

given(userService.getUserReviews(anyLong()))
.willReturn(List.of(new UserReviewsResponse(별로에요, 2L),
new UserReviewsResponse(최고에요, 3L)));

// when & then
mockMvc.perform(get("/users/reviews")
.with(csrf())
.header(AUTHORIZATION, VALID_ACCESS_TOKEN))
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$", notNullValue()))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].count", is(2)))
.andExpect(jsonPath("$[0].review").value("별로에요"));
}
}
}
50 changes: 41 additions & 9 deletions src/test/java/net/teumteum/user/domain/UserRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package net.teumteum.user.domain;

import static net.teumteum.user.domain.Review.별로에요;
import static net.teumteum.user.domain.Review.좋아요;
import static net.teumteum.user.domain.Review.최고에요;

import jakarta.persistence.EntityManager;
import java.util.Optional;
import net.teumteum.core.config.AppConfig;
import net.teumteum.user.domain.response.UserReviewsResponse;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
Expand All @@ -10,8 +16,6 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;

import java.util.Optional;

@DataJpaTest
@Import(AppConfig.class)
@DisplayName("UserRepository 클래스의")
Expand Down Expand Up @@ -49,22 +53,50 @@ class FindById_method {
@DisplayName("저장된 유저의 id로 조회하면, 유저를 반환한다.")
void Find_success_if_exists_user_id_input() {
// given
var id = 1L;
var existsUser = UserFixture.getUserWithId(id);
var existsUser = UserFixture.getNullIdUser();

userRepository.saveAndFlush(existsUser);
entityManager.clear();

// when
var result = userRepository.findById(id);
var result = userRepository.findById(existsUser.getId());

// then
Assertions.assertThat(result)
.isPresent()
.usingRecursiveComparison()
.ignoringFields("value.createdAt", "value.updatedAt")
.isEqualTo(Optional.of(existsUser));
.usingRecursiveComparison()
.ignoringFields("value.createdAt", "value.updatedAt")
.isEqualTo(Optional.ofNullable(existsUser));
}
}


@Nested
@DisplayName("countUserReviewsByUserId 메소드는")
class CountUserReviewsByUserId_method {

@Test
@DisplayName("저장된 유저의 id 을 이용해서 유저의 리뷰 갯수를 조회하면, UserReviewResponse 을 반환한다.")
void Count_user_reviews_by_user_id() {
// given
var id = 1L;
var existUser = UserFixture.getUserWithId(id);

userRepository.saveAndFlush(existUser);
entityManager.clear();

// when
var result = userRepository.countUserReviewsByUserId(id);

// then
Assertions.assertThat(result)
.isNotEmpty()
.hasSize(3)
.extracting(UserReviewsResponse::review, UserReviewsResponse::count)
.contains(
Assertions.tuple(최고에요, 3L),
Assertions.tuple(별로에요, 1L),
Assertions.tuple(좋아요, 2L));

}
}
}