Skip to content

Commit

Permalink
test: 카테고리별 조회한 뉴스 개수 관련 테스트 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjy committed Jun 17, 2024
1 parent ca64137 commit f8b8136
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.member.repository.MemberRepository;
import com.rollthedice.backend.domain.news.entity.News;
import com.rollthedice.backend.domain.news.entity.NewsCategory;
import com.rollthedice.backend.domain.news.entity.ReadNews;
import com.rollthedice.backend.support.RepositoryTest;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -34,15 +35,34 @@ void setUp() {

@Test
@DisplayName("하루동안 조회한 뉴스 개수를 반환할 수 있는가")
void findReadNewsByDate() {
void getCountOfReadNewsByDate() {
//given
int expect = 5;
for (int i = 0; i < expect; i++) {
readNewsRepository.save(ReadNews.builder().member(member).news(newsRepository.save(News.builder().id((long) i).build())).build());
}

//when
Long result = readNewsRepository.findReadNewsByDate(LocalDate.now());
Long result = readNewsRepository.getCountOfReadNewsByDate(LocalDate.now());

//then
assertThat(result).isEqualTo(expect);
}

@Test
@DisplayName("회원이 카테고리별로 조회한 뉴스 개수를 반환할 수 있는가")
void getCountOfReadNewsByCategory() {
//given
int expect = 5;
String category = NewsCategory.SCIENCE.getName();
for (int i = 0; i < expect; i++) {
readNewsRepository.save(ReadNews.builder().member(member)
.news(newsRepository.save(
News.builder().id((long) i).category(category).build())).build());
}

//when
Long result = readNewsRepository.getCountOfReadNewsByCategory(member, category);

//then
assertThat(result).isEqualTo(expect);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.rollthedice.backend.domain.statistics;

import com.rollthedice.backend.domain.statistics.dto.DateViewStatisticsResponse;
import com.rollthedice.backend.domain.news.entity.NewsCategory;
import com.rollthedice.backend.domain.statistics.dto.response.CategoryStatisticsResponse;
import com.rollthedice.backend.domain.statistics.dto.response.DateViewStatisticsResponse;

import java.time.LocalDate;

Expand All @@ -12,4 +14,11 @@ public static DateViewStatisticsResponse DATE_VIEW_STATISTICS_RESPONSE() {
.dateTime(LocalDate.of(2024, 6, 15))
.build();
}

public static CategoryStatisticsResponse CATEGORY_STATISTICS_RESPONSE() {
return CategoryStatisticsResponse.builder()
.views(2L)
.category(NewsCategory.SCIENCE.getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.rollthedice.backend.domain.statistics.api;

import com.rollthedice.backend.domain.statistics.api.StatisticsController;
import com.rollthedice.backend.domain.statistics.dto.DateViewStatisticsResponse;
import com.rollthedice.backend.domain.statistics.dto.response.CategoryStatisticsResponse;
import com.rollthedice.backend.domain.statistics.dto.response.DateViewStatisticsResponse;
import com.rollthedice.backend.domain.statistics.service.StatisticsService;
import com.rollthedice.backend.global.BaseControllerTest;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -13,6 +13,7 @@

import java.util.List;

import static com.rollthedice.backend.domain.statistics.StatisticsFixture.CATEGORY_STATISTICS_RESPONSE;
import static com.rollthedice.backend.domain.statistics.StatisticsFixture.DATE_VIEW_STATISTICS_RESPONSE;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -45,4 +46,25 @@ void getViewsOfDates() throws Exception {
.andExpect(jsonPath("$..['views']").exists())
.andExpect(jsonPath("$..['dateTime']").exists());
}

@Test
@DisplayName("카테고리별 조회수 조회 API가 수행되는 ")
void getCategoryStatistics() throws Exception {
//given
List<CategoryStatisticsResponse> responses = List.of(CATEGORY_STATISTICS_RESPONSE());
given(statisticsService.getCategoryStatistics()).willReturn(responses);

//when
final ResultActions perform = mockMvc.perform(
get("/statistics/categories")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + accessToken)
).andDo(print());

//perform
perform.andExpect(status().isOk())
.andExpect(jsonPath("$..['views']").exists())
.andExpect(jsonPath("$..['category']").exists());

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.rollthedice.backend.domain.statistics.service;

import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.news.entity.NewsCategory;
import com.rollthedice.backend.domain.news.repository.ReadNewsRepository;
import com.rollthedice.backend.domain.statistics.dto.DateViewStatisticsResponse;
import com.rollthedice.backend.domain.statistics.dto.response.CategoryStatisticsResponse;
import com.rollthedice.backend.domain.statistics.dto.response.DateViewStatisticsResponse;
import com.rollthedice.backend.global.LoginTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -30,12 +33,27 @@ public class StatisticsServiceTest extends LoginTest {
void getViewsOfDates() {
//given
int oneWeek = 7;
given(readNewsRepository.findReadNewsByDate(LocalDate.now())).willReturn(anyLong());
given(readNewsRepository.getCountOfReadNewsByDate(LocalDate.now())).willReturn(anyLong());

//when
List<DateViewStatisticsResponse> result = statisticsService.getViewsOfDates();

//then
assertThat(result).hasSize(oneWeek);
}

@Test
@DisplayName("카테고리별 조회수가 조회되는가")
void getCategoryStatistics() {
//given
long views = 3;
String category = NewsCategory.SCIENCE.getName();
given(readNewsRepository.getCountOfReadNewsByCategory(loginUser, category)).willReturn(views);

//when
List<CategoryStatisticsResponse> result = statisticsService.getCategoryStatistics();

//then
assertThat(result).hasSize(NewsCategory.values().length);
}
}

0 comments on commit f8b8136

Please sign in to comment.