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

Feature/answer/#39 #100

Merged
merged 8 commits into from
May 21, 2024
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 @@ -37,7 +37,7 @@ public AnswerCreateRequest(Long questionId, String content,
public static AnswerCreateRequest of(Long questionId, String content,
Boolean profileOnOff, String linkAttachments,
String musicName, String musicSinger, String musicAudioUrl, String imageUrl, boolean updateImage) {
return new AnswerCreateRequest(questionId, content, profileOnOff, linkAttachments,
return new AnswerCreateRequest(questionId, content, profileOnOff, linkAttachments,
musicName, musicSinger, musicAudioUrl, imageUrl, updateImage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AnswerDetailResponse {
private String questionContent;
private Long memberId;
private String content;
private String memberNickname;
private String nickname;
private Boolean profileOnOff;
private String linkAttachments;
private String musicName;
Expand All @@ -24,7 +24,7 @@ public class AnswerDetailResponse {


public AnswerDetailResponse(Long answerId, Long questionId, String questionContent, Long memberId,
String content, String memberNickname, Boolean profileOnOff,
String content, String nickname, Boolean profileOnOff,
String linkAttachments, String musicName, String musicSinger, String musicAudioUrl,
String imageUrl, LocalDateTime createdDate) {

Expand All @@ -34,7 +34,7 @@ public AnswerDetailResponse(Long answerId, Long questionId, String questionConte
this.questionContent = questionContent;
this.memberId = memberId;
this.content = content;
this.memberNickname = memberNickname;
this.nickname = nickname;
this.profileOnOff = profileOnOff;
this.linkAttachments = linkAttachments;
this.musicName = musicName;
Expand All @@ -45,10 +45,10 @@ public AnswerDetailResponse(Long answerId, Long questionId, String questionConte
}

public static AnswerDetailResponse of(Long answerId, Long questionId, String questionContent, Long memberId,
String content, String memberNickname, Boolean profileOnOff,
String content, String nickname, Boolean profileOnOff,
String linkAttachments, String musicName, String musicSinger, String musicAudioUrl,
String imageUrl, LocalDateTime createdDate) {
return new AnswerDetailResponse(answerId, questionId, questionContent, memberId, content, memberNickname,
return new AnswerDetailResponse(answerId, questionId, questionContent, memberId, content, nickname,
profileOnOff, linkAttachments, musicName, musicSinger, musicAudioUrl, imageUrl, createdDate);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ public class Answer {
@OnDelete(action = OnDeleteAction.CASCADE)
private Member member;


@Column(name = "image_file")
private String imageFile; // 이미지 파일 경로를 저장하는 리스트

@Column(name = "content", columnDefinition = "TEXT", nullable = false)
private String content;

@Column(name = "nickname")
private String nickname;

@OneToOne(mappedBy = "answer", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
private Music music;

Expand All @@ -65,10 +67,10 @@ public class Answer {
@Column(name = "profile_on_off", nullable = false)
private boolean profileOnOff;

public static Answer of(Long id, Question question, Member member, String content,
public static Answer of(Long id, Question question, Member member, String content, String nickname,
String imageFile, Music music, String linkAttachments, String imageUrl, LocalDateTime createdDate,
ReactionCount reactionCount, boolean profileOnOff) {
return new Answer(id, question, member, imageFile, content, music, linkAttachments, imageUrl, createdDate, null, reactionCount, profileOnOff);
return new Answer(id, question, member, imageFile, content, nickname, music, linkAttachments, imageUrl, createdDate, null, reactionCount, profileOnOff);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ public Answer toEntity(AnswerCreateRequest request, Question question, Member me
.musicAudioUrl(request.getMusicAudioUrl())
.build();

String senderNickname = question.getSender().getNickname();

// Answer 엔티티 생성 및 Music 설정
Answer answer = Answer.builder()
.question(question)
.member(member)
.content(request.getContent())
.nickname(senderNickname)
.linkAttachments(request.getLinkAttachments())
.profileOnOff(request.getProfileOnOff())
.createdDate(LocalDateTime.now())
Expand All @@ -46,13 +49,14 @@ public AnswerDetailResponse toDomain(Answer answer) {
Member member = answer.getMember();
Question question = answer.getQuestion();


return AnswerDetailResponse.of(
answer.getId(),
question.getId(),
question.getContent(),
member.getId(),
answer.getContent(),
member.getNickname(),
answer.getNickname(),
answer.isProfileOnOff(),
answer.getLinkAttachments(),
music != null ? music.getMusicName() : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public class AnswerTest {
@Autowired
private MockMvc mockMvc;

@Autowired
@MockBean
private MemberRepository memberRepository;

@Autowired
@MockBean
private QuestionRepository questionRepository;

@Autowired
@MockBean
private QuestionJpaRepository questionJpaRepository;

@MockBean
Expand All @@ -80,20 +80,29 @@ public class AnswerTest {

@BeforeEach
void setup() {
testMember = memberRepository.save(Member.builder()
// Mock 객체 초기화
testMember = Member.builder()
.email("[email protected]")
.nickname("장지효")
.memberType(MemberType.KAKAO)
.refreshToken("null")
.build());
.build();

testReceiver = memberRepository.save(Member.builder()
testReceiver = Member.builder()
.email("[email protected]")
.nickname("장지효2")
.memberType(MemberType.KAKAO)
.refreshToken("null")
.build());
.build();

// memberRepository.save() 메서드를 목킹하여 호출된 객체를 반환하도록 설정
when(memberRepository.save(any(Member.class))).thenAnswer(invocation -> invocation.getArgument(0));

// testMember와 testReceiver를 저장
testMember = memberRepository.save(testMember);
testReceiver = memberRepository.save(testReceiver);

// 토큰 생성 및 업데이트
refreshToken = tokenProvider.generateToken(testMember, Duration.ofDays(14));
testMember.updateRefreshToken(refreshToken);
memberRepository.save(testMember);
Expand All @@ -102,41 +111,43 @@ void setup() {
testReceiver.updateRefreshToken(refreshToken);
memberRepository.save(testReceiver);

testQuestion = questionRepository.save(new Question(null, testMember, testReceiver,"이것은 질문입니다.", "장지효", true, LocalDateTime.now(), false));
// memberRepository.save() 메서드를 목킹하여 호출된 객체를 반환하도록 설정
when(questionRepository.save(any(Question.class))).thenAnswer(invocation -> invocation.getArgument(0));

// testQuestion 저장
testQuestion = questionRepository.save(new Question(null, testMember, testReceiver, "이것은 질문입니다.", "장지효", true, LocalDateTime.now(), false));
}

@AfterEach
void tearDown() {
questionJpaRepository.deleteAll();
Optional<Member> member = memberRepository.findByEmail("[email protected]");
member.ifPresent(memberRepository::delete);
memberRepository.deleteAll();
}

@Test
@DisplayName("답변 생성 테스트(): 답변을 생성한다.")
public void createAnswerTest() throws Exception {
AnswerCreateRequest createRequest = new AnswerCreateRequest(
testQuestion.getId(), "이것은 답변입니다.", "장지효", true, "https://link.com",
testQuestion.getId(), "이것은 답변입니다.", true, "https://link.com",
"노래 제목", "가수 이름", "https://audio.url", "https://image.url", true
);
MockMultipartFile imageFile = new MockMultipartFile("imageFile", "image.jpg", MediaType.IMAGE_JPEG_VALUE, "image content".getBytes());
MockMultipartFile requestFile = new MockMultipartFile("request", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsBytes(createRequest));

when(answerService.createAnswer(any(AnswerCreateRequest.class), eq(testMember.getId()), any(MockMultipartFile.class)))
when(answerService.createAnswer(any(AnswerCreateRequest.class), eq(1L), any(MockMultipartFile.class)))
.thenReturn(new AnswerDetailResponse(
1L, testQuestion.getId(), testQuestion.getContent(), testMember.getId(), "이것은 답변입니다.",
testMember.getNickname(), "장지효", true, "https://link.com", "노래 제목", "가수 이름", "https://audio.url",
1L, testQuestion.getId(), testQuestion.getContent(),1L, "이것은 답변입니다.",
testMember.getNickname(), true, "https://link.com", "노래 제목", "가수 이름", "https://audio.url",
"https://image.url", LocalDateTime.now()
));

mockMvc.perform(MockMvcRequestBuilders.multipart("/api/answers/{memberId}", testMember.getId())
mockMvc.perform(MockMvcRequestBuilders.multipart("/api/answers/{memberId}", 1L)
.file(imageFile)
.file(requestFile)
.header("Authorization", "Bearer " + refreshToken)
.contentType(MediaType.MULTIPART_FORM_DATA))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("이것은 답변입니다."))
.andExpect(jsonPath("$.nickname").value("장지효"))
.andExpect(jsonPath("$.profileOnOff").value(true))
.andExpect(jsonPath("$.linkAttachments").value("https://link.com"))
.andExpect(jsonPath("$.musicName").value("노래 제목"))
Expand All @@ -152,16 +163,16 @@ public void createAnswerTest() throws Exception {
public void getAllAnswersTest() throws Exception {
AnswerDetailResponse answerDetailResponse = new AnswerDetailResponse(
1L, testQuestion.getId(), testQuestion.getContent(), testMember.getId(),
"이것은 답변입니다.", testMember.getNickname(), "장지효", true, "https://link.com",
"이것은 답변입니다.", testMember.getNickname(), true, "https://link.com",
"노래 제목", "가수 이름", "https://audio.url", "https://image.url", LocalDateTime.now()
);
List<AnswerDetailResponse> answerDetailResponseList = List.of(answerDetailResponse);
Page<AnswerDetailResponse> answerDetailResponsePage = new PageImpl<>(answerDetailResponseList, Pageable.unpaged(), 1);

when(answerService.getAllAnswers(eq(testMember.getId()), any(Long.class), any(Pageable.class)))
when(answerService.getAllAnswers(eq(1L), any(Long.class), any(Pageable.class)))
.thenReturn(answerDetailResponsePage);

mockMvc.perform(get("/api/answers/member/{memberId}", testMember.getId())
mockMvc.perform(get("/api/answers/member/{memberId}", 1L)
.param("categoryId", "1")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Expand All @@ -173,15 +184,15 @@ public void getAllAnswersTest() throws Exception {
public void updateAnswerTest() throws Exception {
// Given
AnswerCreateRequest updateRequest = new AnswerCreateRequest(
testQuestion.getId(), "이것은 수정된답변입니다.", "장지효", true, "https://link.com",
testQuestion.getId(), "이것은 수정된답변입니다.", true, "https://link.com",
"노래 제목", "가수 이름", "https://audio.url", "https://image.url", true
);
MockMultipartFile imageFile = new MockMultipartFile("imageFile", "image.jpg", MediaType.IMAGE_JPEG_VALUE, "image content".getBytes());
MockMultipartFile requestFile = new MockMultipartFile("request", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsBytes(updateRequest));

AnswerDetailResponse answerDetailResponse = new AnswerDetailResponse(
1L, testQuestion.getId(), testQuestion.getContent(), testMember.getId(), "이것은 수정된답변입니다.",
testMember.getNickname(), "장지효", true, "https://link.com", "노래 제목", "가수 이름", "https://audio.url",
testMember.getNickname(), true, "https://link.com", "노래 제목", "가수 이름", "https://audio.url",
"https://image.url", LocalDateTime.now()
);

Expand All @@ -198,7 +209,6 @@ public void updateAnswerTest() throws Exception {
.andDo(print()) // 응답 내용 출력
.andExpect(status().isOk())
.andExpect(jsonPath("$.content").value("이것은 수정된답변입니다."))
.andExpect(jsonPath("$.nickname").value("장지효"))
.andExpect(jsonPath("$.profileOnOff").value(true))
.andExpect(jsonPath("$.linkAttachments").value("https://link.com"))
.andExpect(jsonPath("$.musicName").value("노래 제목"))
Expand All @@ -223,7 +233,7 @@ public void hasReactedTest() throws Exception {
when(answerService.hasReacted(eq(1L), eq(testMember.getId()))).thenReturn(Map.of());

mockMvc.perform(get("/api/answers/{answerId}/reacted", 1L)
.param("memberId", String.valueOf(testMember.getId()))
.param("memberId", String.valueOf(1L))
.header("Authorization", "Bearer " + refreshToken)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
Expand Down
Loading
Loading