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] story 테스트 코드 추가 #128

Merged
merged 2 commits into from
Mar 6, 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 @@ -408,4 +408,107 @@ void findStoryByHeart() throws Exception {
perform.andDo(print())
.andDo(document("find story by heart", getDocumentRequest(), getDocumentResponse()));
}

@Test
@DisplayName("GET /stories/find")
void getAllStory() throws Exception {
//given
FindAllStoryGroupResponse response = new FindAllStoryGroupResponse(
List.of(
new FindAllStoryResponse(UUID.randomUUID(), "선풍기 청소한 날", "이야기 내용입니다 못난이 내용 내용 내용 내용 내용 내용 내용 내용 내용", "https://owori.s3.ap-northeast-2.amazonaws.com/story/Group%2010_f985a58a-1257-4691-88ee-e2b75977fb3e.png", Boolean.FALSE, 2, 2, "고구마", LocalDate.of(2022, 02, 01), LocalDate.of(2022, 02, 02)),
new FindAllStoryResponse(UUID.randomUUID(), "못난이 외식 했지롱", "이야기 내용입니다 내용 내용 내용 내용 내용 내용 내용 내용 내용", null, Boolean.FALSE, 0, 0, "구운계란", LocalDate.of(2005, 02, 01), LocalDate.of(2019, 02, 03))),
false
);
given(storyService.findAllStory2(any())).willReturn(response);

//when
ResultActions perform =
mockMvc.perform(
get("/stories/find")
.param("sort", "created_at")
.header("Authorization", "Bearer ghuriewhv32j12.oiuwhftg32shdi.ogiurhw0gb")
.header("member_id", UUID.randomUUID().toString())
.contentType(MediaType.APPLICATION_JSON)
);

//then
perform.andExpect(status().isOk());
}

@Test
@DisplayName("GET /stories/search/temp")
void searchStory() throws Exception {
//given
FindAllStoryGroupResponse response = new FindAllStoryGroupResponse(
List.of(
new FindAllStoryResponse(UUID.randomUUID(), "선풍기 청소한 날", "이야기 내용입니다 못난이 내용 내용 내용 내용 내용 내용 내용 내용 내용", "https://owori.s3.ap-northeast-2.amazonaws.com/story/Group%2010_f985a58a-1257-4691-88ee-e2b75977fb3e.png", Boolean.FALSE, 2, 2, "고구마", LocalDate.of(2022, 02, 01), LocalDate.of(2022, 02, 02)),
new FindAllStoryResponse(UUID.randomUUID(), "못난이 외식 했지롱", "이야기 내용입니다 내용 내용 내용 내용 내용 내용 내용 내용 내용", null, Boolean.FALSE, 0, 0, "구운계란", LocalDate.of(2005, 02, 01), LocalDate.of(2019, 02, 03))),
false
);
given(storyService.findStoryBySearch2(any(), any())).willReturn(response);

//when
ResultActions perform =
mockMvc.perform(
get("/stories/search/temp")
.param("sort", "created_at")
.param("keyword","검색어")
.header("Authorization", "Bearer ghuriewhv32j12.oiuwhftg32shdi.ogiurhw0gb")
.header("member_id", UUID.randomUUID().toString())
.contentType(MediaType.APPLICATION_JSON)
);

//then
perform.andExpect(status().isOk());
}

@Test
@DisplayName("GET /stories/member/find")
void findStoryByWriter() throws Exception {
//given
FindAllStoryGroupResponse response = new FindAllStoryGroupResponse(
List.of(
new FindAllStoryResponse(UUID.randomUUID(), "선풍기 청소한 날", "이야기 내용입니다 못난이 내용 내용 내용 내용 내용 내용 내용 내용 내용", "https://owori.s3.ap-northeast-2.amazonaws.com/story/Group%2010_f985a58a-1257-4691-88ee-e2b75977fb3e.png", Boolean.FALSE, 2, 2, "고구마", LocalDate.of(2022, 02, 01), LocalDate.of(2022, 02, 02)),
new FindAllStoryResponse(UUID.randomUUID(), "못난이 외식 했지롱", "이야기 내용입니다 내용 내용 내용 내용 내용 내용 내용 내용 내용", null, Boolean.FALSE, 0, 0, "구운계란", LocalDate.of(2005, 02, 01), LocalDate.of(2019, 02, 03))),
false
);
given(storyService.findStoryByWriter2()).willReturn(response);

//when
ResultActions perform =
mockMvc.perform(
get("/stories/member/find")
.header("Authorization", "Bearer ghuriewhv32j12.oiuwhftg32shdi.ogiurhw0gb")
.header("member_id", UUID.randomUUID().toString())
.contentType(MediaType.APPLICATION_JSON)
);

//then
perform.andExpect(status().isOk());
}

@Test
@DisplayName("GET /stories/heart/find")
void findStoryHeart() throws Exception {
//given
FindAllStoryGroupResponse response = new FindAllStoryGroupResponse(
List.of(
new FindAllStoryResponse(UUID.randomUUID(), "선풍기 청소한 날", "이야기 내용입니다 못난이 내용 내용 내용 내용 내용 내용 내용 내용 내용", "https://owori.s3.ap-northeast-2.amazonaws.com/story/Group%2010_f985a58a-1257-4691-88ee-e2b75977fb3e.png", Boolean.FALSE, 2, 2, "고구마", LocalDate.of(2022, 02, 01), LocalDate.of(2022, 02, 02)),
new FindAllStoryResponse(UUID.randomUUID(), "못난이 외식 했지롱", "이야기 내용입니다 내용 내용 내용 내용 내용 내용 내용 내용 내용", null, Boolean.FALSE, 0, 0, "구운계란", LocalDate.of(2005, 02, 01), LocalDate.of(2019, 02, 03))),
false
);
given(storyService.findStoryByHeart2()).willReturn(response);

//when
ResultActions perform =
mockMvc.perform(
get("/stories/heart/find")
.header("Authorization", "Bearer ghuriewhv32j12.oiuwhftg32shdi.ogiurhw0gb")
.header("member_id", UUID.randomUUID().toString())
.contentType(MediaType.APPLICATION_JSON)
);

//then
perform.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.owori.domain.story.service;

import com.owori.domain.family.entity.Family;
import com.owori.domain.family.repository.FamilyRepository;
import com.owori.domain.member.entity.Member;
import com.owori.domain.story.dto.response.FindStoryResponse;
import com.owori.domain.story.entity.Story;
import com.owori.domain.story.repository.StoryRepository;
import com.owori.support.database.DatabaseTest;
import com.owori.support.database.LoginTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;

@DatabaseTest
@DisplayName("Facade 서비스의")
public class FacadeServiceTest extends LoginTest {

@Autowired
private FacadeService facadeService;

@Autowired
private FamilyRepository familyRepository;

@Autowired
private StoryRepository storyRepository;


@Test
@DisplayName("id로 Story 조회가 잘 이루어지는가")
void findStory() {
//given
Member member = authService.getLoginUser();
Family family = new Family("우리집", member, "code");
String title = "기다리고 기다리던 하루";
Story story = new Story(title, "내용", LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), member);

familyRepository.save(family);
storyRepository.save(story);

//when
FindStoryResponse findStory = facadeService.findStory(story.getId());

//then
assertThat(findStory.getStoryId()).isEqualTo(story.getId());
assertThat(findStory.getTitle()).isEqualTo(title);

}

@Test
@DisplayName("id로 Story Entity 조회가 잘 이루어지는가")
void loadStoryEntity() {
//given
Member member = authService.getLoginUser();
Family family = new Family("우리집", member, "code");
String title = "기다리고 기다리던 하루";
Story story = new Story(title, "내용", LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), member);

familyRepository.save(family);
storyRepository.save(story);

//when
Story findStory = facadeService.loadStoryEntity(story.getId());

//then
assertThat(findStory.getId()).isEqualTo(story.getId());
assertThat(findStory.getTitle()).isEqualTo(title);

}
}
74 changes: 72 additions & 2 deletions src/test/java/com/owori/domain/story/service/StoryServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.owori.domain.heart.repository.HeartRepository;
import com.owori.domain.image.entity.Image;
import com.owori.domain.image.repository.ImageRepository;
import com.owori.domain.member.entity.AuthProvider;
import com.owori.domain.member.entity.Color;
import com.owori.domain.member.entity.Member;
import com.owori.domain.member.entity.OAuth2Info;
import com.owori.domain.member.service.AuthService;
import com.owori.domain.story.dto.request.PostStoryRequest;
import com.owori.domain.story.dto.request.UpdateStoryRequest;
Expand All @@ -19,6 +21,8 @@
import com.owori.domain.story.entity.Story;
import com.owori.domain.story.repository.StoryRepository;
import com.owori.global.exception.EntityNotFoundException;
import com.owori.global.exception.InvalidDateException;
import com.owori.global.exception.NoAuthorityException;
import com.owori.support.database.DatabaseTest;
import com.owori.support.database.LoginTest;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -49,8 +53,25 @@ public class StoryServiceTest extends LoginTest {


@Test
@DisplayName("이야기 생성이 수행되는가")
void addStory() {
@DisplayName("(이미지 O) 이야기 생성이 수행되는가")
void addStoryWithImg() {
//given
String title = "기다리고 기다리던 하루";
PostStoryRequest request = new PostStoryRequest(LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), title, "종강하면 동해바다로 가족 여행 가자고 한게 엊그제 같았는데...3박 4일 동해여행 너무 재밌었어!! 날씨도 너무 좋았고 특히 갈치조림이 대박 ㄹㅇ 맛집 인정... 2일차 점심 때 대림공원 안에서 피크닉한게 가장 기억에 남았던거 같아! 엄마가 만들어 준 샌드위치는 세상에서 젤 맛있어 이거 팔면 대박날듯 ㅋㅋㅋ ", List.of("image.png"));

//when
storyService.addStory(request);

//then
Story story = storyRepository.findAll().get(0);

assertThat(story.getTitle()).isEqualTo(title);
assertThat(story.getMember()).isEqualTo(authService.getLoginUser());
}

@Test
@DisplayName("(이미지 X) 이야기 생성이 수행되는가")
void addStoryWithoutImg() {
//given
String title = "기다리고 기다리던 하루";
PostStoryRequest request = new PostStoryRequest(LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), title, "종강하면 동해바다로 가족 여행 가자고 한게 엊그제 같았는데...3박 4일 동해여행 너무 재밌었어!! 날씨도 너무 좋았고 특히 갈치조림이 대박 ㄹㅇ 맛집 인정... 2일차 점심 때 대림공원 안에서 피크닉한게 가장 기억에 남았던거 같아! 엄마가 만들어 준 샌드위치는 세상에서 젤 맛있어 이거 팔면 대박날듯 ㅋㅋㅋ ", null);
Expand Down Expand Up @@ -263,4 +284,53 @@ void findStoryByHeart() {
assertThat(response.getStories().get(0).getContent()).isEqualTo("정답");
assertThat(response.getStories().get(0).getTitle()).isEqualTo("좋아요");
}

@Test
@DisplayName("start date가 end date보다 나중에 일어난 경우 예외 처리가 발생하는가")
void invalidDateException() {
//given
Story story = new Story("기다리고 기다리던 하루", "내용", LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), authService.getLoginUser());
Image image = new Image("https://owori.s3.ap-northeast-2.amazonaws.com/story/Logo+(1).png", 1L);
storyRepository.save(story);
imageRepository.save(image);

LocalDate start = LocalDate.of(2023, 03, 23);
LocalDate end = LocalDate.of(2023, 02, 01);

UpdateStoryRequest request = new UpdateStoryRequest(story.getId(), start, end, "제목", "내용", List.of("https://owori.s3.ap-northeast-2.amazonaws.com/story/Logo+(2).png","https://owori.s3.ap-northeast-2.amazonaws.com/story/Logo+(3).png"));

//when & then
assertThrows(InvalidDateException.class, () -> storyService.updateStory(request));
}

@Test
@DisplayName("게시글 수정 시 reqeust의 유저와 로그인 유저가 다르면 예외 처리가 발생하는가")
void updateAuthException() {
//given
Member member = new Member(new OAuth2Info("d3akdsa", AuthProvider.GOOGLE));
memberRepository.save(member);
Story story = new Story("기다리고 기다리던 하루", "내용", LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), member);
Image image = new Image("https://owori.s3.ap-northeast-2.amazonaws.com/story/Logo+(1).png", 1L);
storyRepository.save(story);
imageRepository.save(image);

UpdateStoryRequest request = new UpdateStoryRequest(story.getId(), LocalDate.of(2023, 03, 23), LocalDate.of(2023, 04, 01), "제목", "내용", List.of("https://owori.s3.ap-northeast-2.amazonaws.com/story/Logo+(2).png","https://owori.s3.ap-northeast-2.amazonaws.com/story/Logo+(3).png"));

//when & then
assertThrows(NoAuthorityException.class, () -> storyService.updateStory(request));
}

@Test
@DisplayName("이야기 삭제 시 reqeust의 유저와 로그인 유저가 다르면 예외 처리가 발생하는가")
void removeAuthException() {
//given
Member member = new Member(new OAuth2Info("d3akdsa", AuthProvider.GOOGLE));
memberRepository.save(member);
Story story = new Story("기다리고 기다리던 하루", "내용", LocalDate.parse("2017-12-25"), LocalDate.parse("2017-12-30"), member);
storyRepository.save(story);

//when & then
assertThrows(NoAuthorityException.class, () -> storyService.removeStory(story));
}

}
Loading