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

test: ContentController, ContentService 테스트 코드 작성 #287

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public DataResponseDto<ContentDetailResponseDto> getContentDetail(
@PostMapping("/detail/{contentDetailId}/feedback")
public DataResponseDto<MemberContentDetailResponseDto> createFeedback(
@PathVariable Long contentDetailId,
@RequestBody UserSentenceRequestDto userAnswerRequestDto) {
return DataResponseDto.of(contentService.createFeedback(contentDetailId, userAnswerRequestDto));
@RequestBody UserSentenceRequestDto userSentenceRequestDto) {
return DataResponseDto.of(contentService.createFeedback(contentDetailId, userSentenceRequestDto));
}

@Operation(summary = "연습실 피드백 조회 API")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
@Builder
public class AIPracticeFeedbackResponseDto {

private Double contextScore;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.gsm.blabla.content.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserSentenceRequestDto {

private String userSentence;
Expand Down
111 changes: 105 additions & 6 deletions src/test/java/com/gsm/blabla/content/api/ContentControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.gsm.blabla.content.api;

import com.gsm.blabla.content.dto.ContentDetailDto;
import com.gsm.blabla.content.dto.ContentDetailsResponseDto;
import com.gsm.blabla.content.dto.ContentsResponseDto;
import com.gsm.blabla.content.dto.*;
import com.gsm.blabla.crew.dto.ScheduleRequestDto;
import com.gsm.blabla.global.ControllerTestSupport;
import com.gsm.blabla.global.WithCustomMockUser;
import com.gsm.blabla.content.dto.ContentDetailResponseDto;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand Down Expand Up @@ -69,7 +75,7 @@ void getContentDetails() throws Exception {
.contentDetails(List.of(contentDetailDto1, contentDetailDto2))
.build();

given(contentService.getContentDetails(1L)).willReturn(contentDetailsResponseDto);
given(contentService.getContentDetails(any(Long.class))).willReturn(contentDetailsResponseDto);

// when // then
mockMvc.perform(
Expand Down Expand Up @@ -109,7 +115,7 @@ void getContentDetail() throws Exception {
.stoppedAtSec(42020)
.endedAtSec(42025)
.build();
given(contentService.getContentDetail(1L)).willReturn(contentDetailsResponseDto);
given(contentService.getContentDetail(any(Long.class))).willReturn(contentDetailsResponseDto);

// when // then
mockMvc.perform(
Expand All @@ -131,4 +137,97 @@ void getContentDetail() throws Exception {
.andExpect(jsonPath("$.data.endedAtSec").value(42025));
}

@DisplayName("연습실 피드백을 생성한다.")
@Test
@WithCustomMockUser
void createFeedback() throws Exception {
// given
MemberContentDetailResponseDto memberContentDetailResponseDto = MemberContentDetailResponseDto.builder()
.contextRating(2)
.userSentence("I am Jules Ostin. I'm the ceo of About the Fit.")
.targetSentence("I'm Jules Ostin. I'm the founder of About the Fit.")
.longFeedback("test long feedback")
.build();
UserSentenceRequestDto userSentenceRequestDto = UserSentenceRequestDto.builder()
.userSentence("I am Jules Ostin. I'm the ceo of About the Fit.")
.build();
given(contentService.createFeedback(any(Long.class), any(UserSentenceRequestDto.class))).willReturn(memberContentDetailResponseDto);

// when // then
mockMvc.perform(
post("/api/v1/contents/detail/{contentDetailId}/feedback", 1L)
.with(csrf())
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(userSentenceRequestDto))
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.message").value("OK"))
.andExpect(jsonPath("$.data").isNotEmpty())
.andExpect(jsonPath("$.data").isMap())
.andExpect(jsonPath("$.data.contextRating").value(2))
.andExpect(jsonPath("$.data.userSentence").value("I am Jules Ostin. I'm the ceo of About the Fit."))
.andExpect(jsonPath("$.data.targetSentence").value("I'm Jules Ostin. I'm the founder of About the Fit."))
.andExpect(jsonPath("$.data.longFeedback").value("test long feedback"));
}

@DisplayName("연습실 피드백을 조회한다.")
@Test
@WithCustomMockUser
void getFeedback() throws Exception {
// given
MemberContentDetailResponseDto memberContentDetailResponseDto = MemberContentDetailResponseDto.builder()
.contextRating(2)
.userSentence("I am Jules Ostin. I'm the ceo of About the Fit.")
.targetSentence("I'm Jules Ostin. I'm the founder of About the Fit.")
.longFeedback("test long feedback")
.build();
given(contentService.getFeedback(any(Long.class))).willReturn(memberContentDetailResponseDto);

// when // then
mockMvc.perform(
get("/api/v1/contents/detail/{contentDetailId}/feedback", 1L)
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.message").value("OK"))
.andExpect(jsonPath("$.data").isNotEmpty())
.andExpect(jsonPath("$.data").isMap())
.andExpect(jsonPath("$.data.contextRating").value(2))
.andExpect(jsonPath("$.data.userSentence").value("I am Jules Ostin. I'm the ceo of About the Fit."))
.andExpect(jsonPath("$.data.targetSentence").value("I'm Jules Ostin. I'm the founder of About the Fit."))
.andExpect(jsonPath("$.data.longFeedback").value("test long feedback"));
}

@DisplayName("연습 기록을 저장한다.")
@Test
@WithCustomMockUser
void createPracticeHistory() throws Exception {
// given
MockMultipartFile file1 = new MockMultipartFile("files", "test1.wav", "audio/wav", "test1".getBytes());
MockMultipartFile file2 = new MockMultipartFile("files", "test2.wav", "audio/wav", "test2".getBytes());
MockMultipartFile file3 = new MockMultipartFile("files", "test3.wav", "audio/wav", "test3".getBytes());

given(contentService.savePracticeHistory(any(Long.class), ArgumentMatchers.<MultipartFile>anyList()))
.willReturn(Collections.singletonMap("message", "연습 기록 음성 파일이 저장 완료되었습니다."));

// when // then
mockMvc.perform(
MockMvcRequestBuilders.multipart("/api/v1/contents/detail/{contentDetailId}/practice", 1L)
.file(file1)
.file(file2)
.file(file3)
.with(csrf())
)
.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.message").value("OK"))
.andExpect(jsonPath("$.data").isNotEmpty())
.andExpect(jsonPath("$.data").isMap())
.andExpect(jsonPath("$.data.message").value("연습 기록 음성 파일이 저장 완료되었습니다."));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
import com.gsm.blabla.content.domain.Content;
import com.gsm.blabla.content.domain.ContentDetail;
import com.gsm.blabla.content.domain.MemberContentDetail;
import com.gsm.blabla.content.dto.ContentDetailResponseDto;
import com.gsm.blabla.content.dto.ContentDetailsResponseDto;
import com.gsm.blabla.content.dto.ContentsResponseDto;
import com.gsm.blabla.content.dto.*;
import com.gsm.blabla.global.IntegrationTestSupport;
import com.gsm.blabla.global.WithCustomMockUser;
import com.gsm.blabla.member.domain.Member;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;

class ContentServiceTest extends IntegrationTestSupport {

Expand Down Expand Up @@ -133,4 +142,38 @@ void getContentDetail() {
.containsExactly("소개 인사하기", "인턴을 통해 소개 인사를 배워봅시다.", "https://www.youtu.be/sHpGT4SQwgw", "나는 오스틴 입니다. About the Fit의 창업자 입니다.", "I'm Jules Ostin. I'm the founder of About the Fit.", 43200, 43210, 43220);
}

@DisplayName("연습실 피드백을 조회한다.")
@WithCustomMockUser
@Test
void getFeedback() {
// given

// when
MemberContentDetailResponseDto memberContentDetailResponseDto = contentService.getFeedback(1L);

// then
assertThat(memberContentDetailResponseDto)
.extracting("userSentence", "longFeedback", "contextRating")
.containsExactly("test", "test", 3);
}

@DisplayName("연습 기록을 저장한다.")
@WithCustomMockUser
@Test
void createPracticeHistory() {
// given
List<MultipartFile> files = List.of(
new MockMultipartFile("files", "test1.wav", "audio/wav", "test1".getBytes()),
new MockMultipartFile("files", "test2.wav", "audio/wav", "test2".getBytes()),
new MockMultipartFile("files", "test3.wav", "audio/wav", "test3".getBytes())
);

contentDetailRepository.save(contentDetailEn1);

// when
contentService.savePracticeHistory(contentDetailEn1.getId(), files);

// then
assertThat(practiceHistoryRepository.findAll()).hasSize(3);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.gsm.blabla.content.dao.ContentDetailRepository;
import com.gsm.blabla.content.dao.ContentRepository;
import com.gsm.blabla.content.dao.MemberContentDetailRepository;
import com.gsm.blabla.content.dao.PracticeHistoryRepository;
import com.gsm.blabla.content.domain.Content;
import com.gsm.blabla.content.domain.ContentDetail;
import com.gsm.blabla.content.domain.MemberContentDetail;
Expand Down Expand Up @@ -97,6 +98,9 @@ public abstract class IntegrationTestSupport {
@Autowired
protected AccuseRepository accuseRepository;

@Autowired
protected PracticeHistoryRepository practiceHistoryRepository;

@AfterEach
void cleanUpDatabase() {
databaseCleanup.execute();
Expand Down
Loading