Skip to content

Commit

Permalink
✨ Feat: Briefing 상세 조회 V2 API 통합 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
swa07016 committed Mar 7, 2024
1 parent 0688e3a commit 46a54f8
Showing 1 changed file with 87 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.briefingapi.briefing.presentation;

import com.example.briefingcommon.domain.repository.article.BriefingRepository;
import com.example.briefingcommon.entity.Briefing;
import com.example.briefingcommon.entity.enums.BriefingType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -9,10 +11,19 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.junit.jupiter.api.Assertions.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
Expand All @@ -31,8 +42,81 @@ public void setup() {
}

@Test
@DisplayName("브리핑 상세 조회 기능 테스트입니다.")
void 브리핑_상세_조회() throws Exception {
@DisplayName("[BriefingV2Api] 상세 조회 - OK")
void 브리핑_상세_조회_OK() throws Exception {
// given
Briefing briefing =
Briefing.builder()
.title("제목")
.subtitle("부제목")
.content("내용")
.ranks(1)
.type(BriefingType.SOCIAL)
.build();
Briefing savedBriefing = briefingRepository.save(briefing);
Long briefingId = savedBriefing.getId();

// when & then
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/v2/briefings/{id}", briefingId);
mockMvc.perform(requestBuilder)
.andExpect(status().isOk())
.andExpect(jsonPath("$.isSuccess").value(true))
.andExpect(jsonPath("$.result.title").value("제목"))
.andExpect(jsonPath("$.result.id").value(briefingId));
}

@Test
@DisplayName("[BriefingV2Api] 상세 조회 - NOT FOUND")
void 브리핑_상세_조회_NOT_FOUND() throws Exception {
// given
Long briefingId = 0L;

// when & then
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/v2/briefings/{id}", briefingId);
mockMvc.perform(requestBuilder)
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.isSuccess").value(false));
}

@Test
@DisplayName("[BriefingV2Api] 상세 조회 - 100명이 동시에 조회하면 100의 조회수가 늘어나야 합니다.")
void 브리핑_상세_조회_동시_요청() throws Exception {
// given
final int numberOfThreads = 100;
ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
CountDownLatch latch = new CountDownLatch(numberOfThreads);
Briefing briefing =
Briefing.builder()
.title("제목")
.subtitle("부제목")
.content("내용")
.ranks(1)
.type(BriefingType.SOCIAL)
.build();
Briefing savedBriefing = briefingRepository.save(briefing);
Long briefingId = savedBriefing.getId();

// when
for (int i = 0; i < numberOfThreads; i++) {
executorService.execute(
() -> {
try {
MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/v2/briefings/{id}", briefingId);
mockMvc.perform(requestBuilder);
} catch (Exception e) {
e.printStackTrace();
}
finally {
latch.countDown();
}
});
}

latch.await();
executorService.shutdown();

// then
Briefing updatedBriefing = briefingRepository.findById(briefingId).orElseThrow();
assertEquals(numberOfThreads, updatedBriefing.getViewCount());
}
}

0 comments on commit 46a54f8

Please sign in to comment.