Skip to content

Commit

Permalink
Merge pull request #147 from kakao-tech-campus-2nd-step3/week10
Browse files Browse the repository at this point in the history
Hotfix 배포
  • Loading branch information
JaeBin2019 authored Nov 14, 2024
2 parents 4c4ae8c + 73b236d commit 9437c10
Show file tree
Hide file tree
Showing 21 changed files with 1,724 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ public class CommentListRequestDto {

public CommentListRequestDto() {
}

public CommentListRequestDto(int page, int size, Sort.Direction sortDirection, String sortBy) {
this.page = page;
this.size = size;
this.sortDirection = sortDirection;
this.sortBy = sortBy;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,14 @@ public void validateMemberIsParticipant(Member member) {
}
}

public void setStatus(ConsultationStatus consultationStatus) {
this.consultationStatus = consultationStatus;
}

public void inputRating(int rating, String review) {
this.rating = rating;
this.review = review;

}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public class ConsultationRequestDto {
LocalDateTime consultationDateTime;
String title;
String content;

public ConsultationRequestDto(Long mentorId, LocalDateTime consultationDateTime, String title, String content) {
this.mentorId = mentorId;
this.consultationDateTime = consultationDateTime;
this.title = title;
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class ScheduleRequestDto {
private Map<String, List<Integer>> list; // LocalDate 문자열을 키로 사용

public ScheduleRequestDto(Map<String, List<Integer>> list) {
this.list = list;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public class EmailVerificationRequestDto {

@NotBlank(message = "인증 코드는 필수입니다")
private String verificationCode;

public EmailVerificationRequestDto(String email, String verificationCode) {
this.email = email;
this.verificationCode = verificationCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void registerStompEndpoints(StompEndpointRegistry registry) {
.addInterceptors(new JwtHandshakeInterceptor(jwtUtil))
.setHandshakeHandler(new WebSocketHandShakeHandler())
.withSockJS();

registry.addEndpoint("/ws/chat/{roomId}")
.setAllowedOrigins("http://localhost:8080", "http://localhost:5173", "http://54.252.224.76:80")
.addInterceptors(new JwtHandshakeInterceptor(jwtUtil))
.setHandshakeHandler(new WebSocketHandShakeHandler())
.withSockJS();
}

}
4 changes: 4 additions & 0 deletions boomerang/src/main/java/boomerang/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public void updateNickname(String nickname) {
this.memberRole = MemberRole.COMPLETE_USER;
}

public void registerMentor(Mentor mentor) {
this.mentor = mentor;
}

public void verifyEmail() {
this.emailVerified = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,13 @@ public class SubStepInfo {
@Convert(converter = StringListConverter.class)
@Column(length = 2000)
private List<String> inputs = new ArrayList<>();

public SubStepInfo(Long id, SubStepEnum subStepEnum, String subStepName, String content, List<String> inputs) {
this.id = id;
this.subStepEnum = subStepEnum;
this.subStepName = subStepName;
this.content = content;
this.inputs = inputs;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ public class ProgressTypeRequestDto {

private Boolean isInsured; //보험 가입 여부
private LeaseTypeEnum leaseType; //계약 종류

public ProgressTypeRequestDto(Boolean isInsured, LeaseTypeEnum leaseType) {
this.isInsured = isInsured;
this.leaseType = leaseType;
}
}
206 changes: 206 additions & 0 deletions boomerang/src/test/java/boomerang/board/service/BoardServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package boomerang.board.service;

import boomerang.board.domain.Board;
import boomerang.board.domain.BoardType;
import boomerang.board.dto.BoardBestListRequestDto;
import boomerang.board.dto.BoardListRequestDto;
import boomerang.board.dto.BoardRequestDto;
import boomerang.board.repository.BoardRepository;
import boomerang.file.service.FileService;
import boomerang.global.exception.BusinessException;
import boomerang.global.response.ErrorCode;
import boomerang.member.domain.Member;
import boomerang.member.dto.MemberServiceDto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.multipart.MultipartFile;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class BoardServiceTest {

@Mock
private BoardRepository boardRepository;

@Mock
private FileService fileService;

@InjectMocks
private BoardService boardService;

private Board board;
private Member member;
private BoardRequestDto boardRequestDto;

@BeforeEach
void setUp() {
member = new Member(new MemberServiceDto("user@example.com", "nickname"));
boardRequestDto = new BoardRequestDto("Test Title", "Test Content", BoardType.ENTIRE, null);
board = new Board(1L, boardRequestDto, member);
}

@Test
void testGetBestBoards() {
// given
BoardBestListRequestDto requestDto = new BoardBestListRequestDto();
requestDto.setSize(10);
PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "score"));
Page<Board> pageResult = new PageImpl<>(List.of(board));

given(boardRepository.findByBoardType(requestDto.getBoard_type(), pageRequest)).willReturn(pageResult);

// when
Page<Board> bestBoards = boardService.getBestBoards(requestDto);

// then
assertThat(bestBoards.getContent()).containsExactly(board);
then(boardRepository).should(times(1)).findByBoardType(any(), eq(pageRequest));
}

@Test
void testGetAllBoards() {
// given
BoardListRequestDto requestDto = new BoardListRequestDto();
requestDto.setBoard_type(BoardType.ENTIRE);
requestDto.setSearch_word("Test");
PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Direction.DESC, "id"));
Page<Board> pageResult = new PageImpl<>(List.of(board));

given(boardRepository.findByBoardTypeAndTitleContaining(any(), any(), eq(pageRequest))).willReturn(pageResult);

// when
Page<Board> boards = boardService.getAllBoards(requestDto);

// then
assertThat(boards.getContent()).containsExactly(board);
then(boardRepository).should(times(1)).findByBoardTypeAndTitleContaining(any(), any(), eq(pageRequest));
}

@Test
void testGetBoard() {
// given
given(boardRepository.findById(anyLong())).willReturn(Optional.of(board));

// when
Board foundBoard = boardService.getBoard(board.getId());

// then
assertThat(foundBoard).isEqualTo(board);
then(boardRepository).should(times(1)).findById(anyLong());
}


@Test
void testGetBoard_NotFound() {
// given
given(boardRepository.findById(anyLong())).willReturn(Optional.empty());

// when / then
assertThatThrownBy(() -> boardService.getBoard(1L))
.isInstanceOf(BusinessException.class)
.hasMessage(ErrorCode.BOARD_NOT_FOUND_ERROR.getMessage());

then(boardRepository).should(times(1)).findById(anyLong());
}

@Test
void testCreateBoard() throws MalformedURLException {
// given
MultipartFile file = mock(MultipartFile.class);
URL mockUrl = new URL("http://example.com/image.jpg");
List<MultipartFile> images = List.of(file, file); // 이미지 파일 2개

// BoardRequestDto의 content에 이미지 태그 2개 설정
String initialContent = "<p>Test content</p><img src=? /><img src=? />";
boardRequestDto.setContent(initialContent);

given(fileService.upload(anyString(), any())).willReturn(mockUrl);
given(boardRepository.save(any(Board.class))).willAnswer(invocation -> {
Board savedBoard = invocation.getArgument(0);
return savedBoard;
});

// when
Board createdBoard = boardService.createBoard(boardRequestDto, member, images);

// then
assertThat(createdBoard.getTitle()).isEqualTo(boardRequestDto.getTitle());
assertThat(createdBoard.getContent()).contains("<img src='http://example.com/image.jpg' />");

then(fileService).should(times(images.size())).upload(anyString(), any());
then(boardRepository).should(times(1)).save(any(Board.class));
}

@Test
void testUpdateBoard_NotOwner() {
// given
Member anotherMember = new Member(new MemberServiceDto("another@example.com", "anotherNickname"));
Board anotherBoard = new Board(boardRequestDto, anotherMember);
Long boardId = 1L;

given(boardRepository.findById(boardId)).willReturn(Optional.of(anotherBoard));

// when / then
assertThatThrownBy(() -> boardService.updateBoard(boardId, boardRequestDto, member, Collections.emptyList()))
.isInstanceOf(BusinessException.class)
.hasMessage(ErrorCode.BOARD_DONT_HAS_OWNERSHIP_ERROR.getMessage());

then(boardRepository).should(times(1)).findById(boardId);
then(boardRepository).shouldHaveNoMoreInteractions();
}

@Test
void testDeleteBoard() {
// given
Long boardId = 1L;

given(boardRepository.findById(boardId)).willReturn(Optional.of(board));

// when
boardService.deleteBoard(member, boardId);

// then
then(boardRepository).should(times(1)).findById(boardId);
then(boardRepository).should(times(1)).deleteById(boardId);
}

@Test
void testDeleteBoard_NotOwner() {
// given
Member anotherMember = new Member(new MemberServiceDto("another@example.com", "anotherNickname"));
Board anotherBoard = new Board(boardRequestDto, anotherMember);
Long boardId = 1L;

given(boardRepository.findById(boardId)).willReturn(Optional.of(anotherBoard));

// when / then
assertThatThrownBy(() -> boardService.deleteBoard(member, boardId))
.isInstanceOf(BusinessException.class)
.hasMessage(ErrorCode.BOARD_DONT_HAS_OWNERSHIP_ERROR.getMessage());

then(boardRepository).should(times(1)).findById(boardId);
then(boardRepository).shouldHaveNoMoreInteractions();
}

}
Loading

0 comments on commit 9437c10

Please sign in to comment.