Skip to content

Commit

Permalink
[feature/InhaBas#251] 첨부파일 개편으로 전체적인 test 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
whitem4rk committed Mar 22, 2024
1 parent f41aeea commit d6b0691
Show file tree
Hide file tree
Showing 20 changed files with 136 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
public class ScholarshipBoardServiceImpl implements ScholarshipBoardService {

private final BoardFileRepository boardFileRepository;

private final ScholarshipBoardRepository scholarshipBoardRepository;
private final MenuRepository menuRepository;
private final MemberRepository memberRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,17 @@ public ResponseEntity<PagedResponseDto<ScholarshipBoardDto>> getBoardList(
@ApiResponses({
@ApiResponse(
responseCode = "200",
content = {@Content(schema = @Schema(implementation = ScholarshipBoardDto.class))}),
content = {@Content(schema = @Schema(implementation = ScholarshipBoardDetailDto.class))}),
@ApiResponse(
responseCode = "404",
description = "데이터가 존재하지 않습니다.",
responseCode = "400",
description = "입력값이 없거나, 타입이 유효하지 않습니다.",
content =
@Content(
schema = @Schema(implementation = ErrorResponse.class),
examples =
@ExampleObject(
value =
"{\"status\": 404, \"code\": \"G004\", \"message\": \"데이터가 존재하지 않습니다.\"}")))
"{\"status\": 400, \"code\": \"G003\", \"message\": \"입력값이 없거나, 타입이 유효하지 않습니다.\"}")))
})
public ResponseEntity<ScholarshipBoardDetailDto> getBoard(
@PathVariable Long boardId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.springframework.test.util.ReflectionTestUtils;

import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.menu.domain.Menu;
import org.mockito.Mock;
Expand All @@ -21,7 +22,7 @@
class ContestBoardTest {

@Mock private Menu menu;

@Mock private Member writer;
@Mock private ContestField contestField;

private ContestBoard contestBoard;
Expand Down Expand Up @@ -109,8 +110,8 @@ void updateFilesTest() {
// Given

List<BoardFile> files = new ArrayList<>();
files.add(new BoardFile("file1.jpg", "/url1", contestBoard));
files.add(new BoardFile("file2.pdf", "/url2", contestBoard));
files.add(new BoardFile("random", "file1.jpg", "/url1", writer, 10L, "image/jpeg"));
files.add(new BoardFile("random", "file2.pdf", "/url2", writer, 10L, "image/jpeg"));

// When
contestBoard.updateFiles(files);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void createContestBoardDetailDto() {
.dateContestEnd(LocalDate.now().plusDays(10))
.dateCreated(LocalDateTime.now())
.dateUpdated(LocalDateTime.now())
.thumbnail(new FileDownloadDto("thumbnail.jpg", "thumbnailUrl"))
.thumbnail(
new FileDownloadDto("random", "thumbnail.jpg", "/thumbnailUrl", 10L, "image/jpeg"))
.images(new ArrayList<>())
.otherFiles(new ArrayList<>())
.build();
Expand Down Expand Up @@ -82,7 +83,8 @@ public void nullTitleTest() {
.dateContestEnd(LocalDate.now().plusDays(10))
.dateCreated(LocalDateTime.now())
.dateUpdated(LocalDateTime.now())
.thumbnail(new FileDownloadDto("thumbnail.jpg", "thumbnailUrl"))
.thumbnail(
new FileDownloadDto("random", "thumbnail.jpg", "/thumbnailUrl", 10L, "image/jpeg"))
.images(new ArrayList<>())
.otherFiles(new ArrayList<>())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public void createContestBoardDto() {
String association = "테스트 협회";
LocalDate dateContestStart = LocalDate.of(2024, 1, 1);
LocalDate dateContestEnd = LocalDate.of(2024, 3, 1);
FileDownloadDto thumbnail = new FileDownloadDto("thumbnail.jpg", "thumbnailUrl");
FileDownloadDto thumbnail =
new FileDownloadDto("random", "thumbnail.jpg", "/thumbnailUrl", 10L, "image/jpeg");

// when
ContestBoardDto contestBoardDto =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -31,7 +33,7 @@
import com.inhabas.api.domain.contest.repository.ContestBoardRepository;
import com.inhabas.api.domain.contest.repository.ContestFieldRepository;
import com.inhabas.api.domain.file.dto.FileDownloadDto;
import com.inhabas.api.domain.file.usecase.S3Service;
import com.inhabas.api.domain.file.repository.BoardFileRepository;
import com.inhabas.api.domain.member.domain.entity.MemberTest;
import com.inhabas.api.domain.menu.domain.Menu;
import com.inhabas.api.domain.menu.repository.MenuRepository;
Expand All @@ -57,7 +59,7 @@ public class ContestBoardServiceImplTest {

@Mock private MenuRepository menuRepository;

@Mock private S3Service s3Service;
@Mock private BoardFileRepository boardFileRepository;

@DisplayName("Contest board 게시글 목록을 조회한다.")
@Transactional(readOnly = true)
Expand All @@ -73,7 +75,8 @@ void getContestBoards() {
.topic("테스트 주제")
.dateContestStart(LocalDate.now())
.dateContestEnd(LocalDate.now().plusDays(10))
.thumbnail(new FileDownloadDto("thumbnail.jpg", "thumbnailUrl"))
.thumbnail(
new FileDownloadDto("random", "thumbnail.jpg", "/thumbnailUrl", 10L, "image/jpeg"))
.build();

given(
Expand Down Expand Up @@ -211,16 +214,22 @@ void updateContestBoard() {

ReflectionTestUtils.setField(contestBoard, "id", 1L);

Member writer = MemberTest.chiefMember();

given(memberRepository.findById(any())).willReturn(Optional.of(writer));
given(contestFieldRepository.findById(any())).willReturn(Optional.of(contestField));
given(contestBoardRepository.findById(any())).willReturn(Optional.of(contestBoard));
given(contestBoardRepository.save(any())).willReturn(contestBoard);
given(boardFileRepository.getAllByIdInAndUploader(anyList(), any()))
.willReturn(new ArrayList<>());

// when
contestBoardService.updateContestBoard(contestBoard.getId(), CONTEST, saveContestBoardDto);
contestBoardService.updateContestBoard(contestBoard.getId(), CONTEST, saveContestBoardDto, 1L);

// then
then(memberRepository).should(times(1)).findById(any());
then(contestBoardRepository).should(times(1)).findById(any());
then(contestBoardRepository).should(times(1)).save(any());
then(contestBoardRepository).should(times(1)).findById(any());
then(boardFileRepository).should(times(1)).getAllByIdInAndUploader(anyList(), any());
}

@DisplayName("Contest board 게시글을 삭제한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@

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

import com.inhabas.api.domain.board.domain.BaseBoard;
import org.mockito.Mockito;
import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import org.mockito.Mock;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class BoardFileTest {

@Mock private Member uploader;

@DisplayName("Board 에 첨부되는 File 을 생성한다.")
@Test
void constructorTest() {
// given
String name = "fileName";
String url = "fileUrl";
BaseBoard baseBoard = Mockito.mock(BaseBoard.class);

// when
BoardFile boardFile = new BoardFile(name, url, baseBoard);
BoardFile boardFile = new BoardFile("random", name, url, uploader, 10L, "image/jpeg");

// then
assertThat(boardFile.getName()).isEqualTo(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public static void close() {
public void validationTest() {
// given
FileDownloadDto fileDownloadDto =
FileDownloadDto.builder().name("fileName").url("http://localhost/upload").build();
FileDownloadDto.builder()
.id("random")
.name("fileName")
.url("http://localhost/upload")
.size(10L)
.type("image/jpeg")
.build();

// when
Set<ConstraintViolation<FileDownloadDto>> violations = validator.validate(fileDownloadDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.menu.domain.Menu;
import org.assertj.core.api.Assertions;
Expand All @@ -16,6 +17,7 @@
public class NormalBoardTest {

@Mock private Menu menu;
@Mock private Member writer;

private NormalBoard normalBoard;

Expand Down Expand Up @@ -57,7 +59,8 @@ public void updateTextTest() {
public void updateFilesTest() {
// given
List<BoardFile> files = new ArrayList<>();
BoardFile file = new BoardFile("fileName", "/hello", normalBoard);
BoardFile file =
new BoardFile("random", "thumbnail.jpg", "/thumbnailUrl", writer, 10L, "image/jpeg");
files.add(file);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public static void close() {
@Test
public void SaveNormalBoardDto_is_OK() {
// given
SaveNormalBoardDto saveNormalBoardDto =
new SaveNormalBoardDto("title", "content", emptyList, 2);
SaveNormalBoardDto saveNormalBoardDto = new SaveNormalBoardDto("title", "content", null, 2);

// when
Set<ConstraintViolation<SaveNormalBoardDto>> violations =
Expand All @@ -54,7 +53,7 @@ public void SaveNormalBoardDto_is_OK() {
@Test
public void Content_is_null() {
// given
SaveNormalBoardDto saveNormalBoardDto = new SaveNormalBoardDto("title", null, emptyList, 2);
SaveNormalBoardDto saveNormalBoardDto = new SaveNormalBoardDto("title", null, null, 2);

// when
Set<ConstraintViolation<SaveNormalBoardDto>> violations =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

Expand All @@ -20,7 +22,7 @@

import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository;
import com.inhabas.api.domain.file.usecase.S3Service;
import com.inhabas.api.domain.file.repository.BoardFileRepository;
import com.inhabas.api.domain.member.domain.entity.MemberTest;
import com.inhabas.api.domain.menu.domain.Menu;
import com.inhabas.api.domain.menu.repository.MenuRepository;
Expand All @@ -45,7 +47,7 @@ public class NormalBoardServiceImplTest {
@Mock MemberRepository memberRepository;
@Mock NormalBoardRepository normalBoardRepository;
@Mock MenuRepository menuRepository;
@Mock S3Service s3Service;
@Mock BoardFileRepository boardFileRepository;

@DisplayName("normal board 게시글 목록을 조회한다.")
@Transactional(readOnly = true)
Expand Down Expand Up @@ -125,16 +127,20 @@ void update() {
Menu menu = getNormalNoticeMenu(getNormalMenuGroup());
NormalBoard normalBoard = new NormalBoard("title", menu, "content", false, LocalDateTime.now());
ReflectionTestUtils.setField(normalBoard, "id", 1L);
Member writer = MemberTest.chiefMember();

given(memberRepository.findById(any())).willReturn(Optional.of(writer));
given(normalBoardRepository.findById(any())).willReturn(Optional.of(normalBoard));
given(normalBoardRepository.save(any())).willReturn(normalBoard);
given(boardFileRepository.getAllByIdInAndUploader(anyList(), any()))
.willReturn(new ArrayList<>());

// when
normalBoardService.update(normalBoard.getId(), NOTICE, saveNormalBoardDto);
normalBoardService.update(normalBoard.getId(), NOTICE, saveNormalBoardDto, 1L);

// then
then(memberRepository).should(times(1)).findById(any());
then(normalBoardRepository).should(times(1)).findById(any());
then(normalBoardRepository).should(times(1)).save(any());
then(boardFileRepository).should(times(1)).getAllByIdInAndUploader(anyList(), any());
}

@DisplayName("normal board 게시글을 삭제한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.List;

import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.menu.domain.Menu;
import org.assertj.core.api.Assertions;
Expand All @@ -16,6 +17,7 @@
public class ProjectBoardTest {

@Mock private Menu menu;
@Mock private Member writer;

private ProjectBoard projectBoard;

Expand Down Expand Up @@ -57,7 +59,7 @@ public void updateTextTest() {
public void updateFilesTest() {
// given
List<BoardFile> files = new ArrayList<>();
BoardFile file = new BoardFile("fileName", "/hello", projectBoard);
BoardFile file = new BoardFile("random", "fileName", "/hello", writer, 10L, "image/jpeg");
files.add(file);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public static void close() {
@Test
public void SaveProjectBoardDto_is_OK() {
// given
SaveProjectBoardDto saveProjectBoardDto =
new SaveProjectBoardDto("title", "content", emptyList, 2);
SaveProjectBoardDto saveProjectBoardDto = new SaveProjectBoardDto("title", "content", null, 2);

// when
Set<ConstraintViolation<SaveProjectBoardDto>> violations =
Expand All @@ -54,7 +53,7 @@ public void SaveProjectBoardDto_is_OK() {
@Test
public void Content_is_null() {
// given
SaveProjectBoardDto saveProjectBoardDto = new SaveProjectBoardDto("title", null, emptyList, 2);
SaveProjectBoardDto saveProjectBoardDto = new SaveProjectBoardDto("title", null, null, 2);

// when
Set<ConstraintViolation<SaveProjectBoardDto>> violations =
Expand Down
Loading

0 comments on commit d6b0691

Please sign in to comment.