Skip to content

Commit

Permalink
test: 게시글 쓰기 단위 테스트 코드 구현
Browse files Browse the repository at this point in the history
* refactor: Post 관련 테스트 메소드 네이밍 수정

* fix: boardFixture 메소드로 가져오도록 수정 및 usedBoard 필드 추가

* fix: 게시판 조회 쿼리문 수정

* refactor: 엔티티 생성하는 메소드 내부로 분리

* test: 정상적인 boardId, MemberId가 주어졌다면 게시글 쓰기가 성공하는 테스트 코드 작성

* test: 정상적인 boardId, MemberId가 주어졌다면 게시글 쓰기가 성공합니다.(이미지 O)

* test: 유효하지 않은 boardID가 주어졌다면 게시글 쓰기에 예외를 발생하는 테스트 코드 구현

* test: 유효하지 않은 memberId가 주어졌다면 게시글 쓰기에 예외를 발생하는 테스트 코드 구현
  • Loading branch information
jjuny0310 authored Oct 19, 2023
1 parent 5b430b2 commit 3342317
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 346 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package com.ssafy.ssafsound.domain.board.dto;

import com.ssafy.ssafsound.domain.board.domain.Board;

import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
public class GetBoardElement {
private Long boardId;
private String title;
private String imageUrl;
private String description;
private Long boardId;
private String title;
private String imageUrl;
private Boolean usedBoard;
private String description;

public static GetBoardElement from(Board board) {
return GetBoardElement.builder()
.boardId(board.getId())
.title(board.getTitle())
.imageUrl(board.getImageUrl())
.description(board.getDescription())
.build();
}
public static GetBoardElement from(Board board) {
return GetBoardElement.builder()
.boardId(board.getId())
.title(board.getTitle())
.imageUrl(board.getImageUrl())
.usedBoard(board.getUsedBoard())
.description(board.getDescription())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.ssafy.ssafsound.domain.board.repository;

import java.util.List;

import com.ssafy.ssafsound.domain.board.domain.Board;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
List<Board> findAllByUsedBoardTrue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class BoardService {

@Transactional(readOnly = true)
public GetBoardResDto findBoards() {
List<Board> boards = boardRepository.findAll();
List<Board> boards = boardRepository.findAllByUsedBoardTrue();
return GetBoardResDto.from(boards);
}
}
136 changes: 80 additions & 56 deletions src/main/java/com/ssafy/ssafsound/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package com.ssafy.ssafsound.domain.post.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import com.ssafy.ssafsound.domain.BaseTimeEntity;
import com.ssafy.ssafsound.domain.board.domain.Board;
import com.ssafy.ssafsound.domain.comment.domain.Comment;
import com.ssafy.ssafsound.domain.member.domain.Member;
import lombok.*;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity(name = "post")
@Getter
Expand All @@ -21,54 +35,64 @@
@Where(clause = "deleted_post = false")
public class Post extends BaseTimeEntity {

@Id
@Column(name = "post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String title;

@Column
private String content;

@Column
private Long view;

@Builder.Default
@Column
private Boolean deletedPost = Boolean.FALSE;

@Column
private Boolean anonymity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Board board;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostImage> images = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostLike> likes = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostScrap> scraps = new ArrayList<>();

public void updatePost(String title, String content, Boolean anonymity){
this.title = title;
this.content = content;
this.anonymity = anonymity;
}
@Id
@Column(name = "post_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column
private String title;

@Column
private String content;

@Column
private Long view;

@Builder.Default
@Column
private Boolean deletedPost = Boolean.FALSE;

@Column
private Boolean anonymity;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "board_id")
private Board board;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostImage> images = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostLike> likes = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<Comment> comments = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "post")
private List<PostScrap> scraps = new ArrayList<>();

public void updatePost(String title, String content, Boolean anonymity) {
this.title = title;
this.content = content;
this.anonymity = anonymity;
}

public static Post of(Board board, Member member, String title, String content, Boolean anonymity) {
return Post.builder()
.board(board)
.member(member)
.title(title)
.content(content)
.anonymity(anonymity)
.build();
}
}
47 changes: 31 additions & 16 deletions src/main/java/com/ssafy/ssafsound/domain/post/domain/PostImage.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
package com.ssafy.ssafsound.domain.post.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

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

import javax.persistence.*;

@Entity(name="post_image")
@Entity(name = "post_image")
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PostImage {

@Id
@Column(name = "post_image_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Id
@Column(name = "post_image_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
@Column
private String imagePath;

@Column
private String imagePath;
@Column
private String imageUrl;

@Column
private String imageUrl;
@Column
private Integer renderOrder;

@Column
private Integer renderOrder;
public static PostImage of(Post post, String imagePath, String imageUrl) {
return PostImage.builder()
.post(post)
.imagePath(imagePath)
.imageUrl(imageUrl)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ssafy.ssafsound.domain.post.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@Builder
public class ImageInfo {
private String imagePath;
private String imageUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,26 +184,17 @@ public PostIdElement writePost(Long boardId, Long loginMemberId, PostPostWriteRe

List<ImageInfo> images = postPostWriteReqDto.getImages();

Post post = Post.builder()
.board(board)
.member(loginMember)
.title(postPostWriteReqDto.getTitle())
.content(postPostWriteReqDto.getContent())
.anonymity(postPostWriteReqDto.isAnonymity())
.build();
postRepository.save(post);
Post post = Post.of(board, loginMember, postPostWriteReqDto.getTitle(), postPostWriteReqDto.getContent(),
postPostWriteReqDto.isAnonymity());
Long postId = postRepository.save(post).getId();

if (images.size() > 0) {
for (ImageInfo image : images) {
PostImage postImage = PostImage.builder()
.post(post)
.imagePath(image.getImagePath())
.imageUrl(image.getImageUrl())
.build();
PostImage postImage = PostImage.of(post, image.getImagePath(), image.getImageUrl());
postImageRepository.save(postImage);
}
}
return new PostIdElement(post.getId());
return new PostIdElement(postId);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
package com.ssafy.ssafsound.domain.board.controller;

import com.ssafy.ssafsound.domain.board.domain.Board;
import com.ssafy.ssafsound.domain.board.dto.GetBoardResDto;
import com.ssafy.ssafsound.global.docs.ControllerTest;
import com.ssafy.ssafsound.global.util.fixture.BoardFixture;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.restdocs.payload.JsonFieldType;

import static com.ssafy.ssafsound.global.docs.snippet.CookieDescriptionSnippet.requestCookieAccessTokenNeedless;
import static com.ssafy.ssafsound.global.docs.snippet.CookieDescriptionSnippet.requestCookieAccessTokenOptional;
import static com.ssafy.ssafsound.global.util.fixture.BoardFixture.GET_BOARD_RES_DTO1;
import static org.mockito.Mockito.doReturn;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;

import java.util.List;

class BoardControllerTest extends ControllerTest {
private final BoardFixture boardFixture = new BoardFixture();

@Test
@DisplayName("게시판 목록 조회 데이터 반환")
public void findBoards() {
doReturn(GET_BOARD_RES_DTO1)
List<Board> boards = List.of(boardFixture.getFreeBoard(), boardFixture.getJobBoard());
GetBoardResDto getBoardResDto = GetBoardResDto.from(boards);

doReturn(getBoardResDto)
.when(boardService)
.findBoards();

Expand All @@ -33,6 +42,7 @@ public void findBoards() {
fieldWithPath("boardId").type(JsonFieldType.NUMBER).description("게시판 목록의 ID"),
fieldWithPath("title").type(JsonFieldType.STRING).description("게시판 목록의 제목, 자유 게시판 | 취업 게시판 | 맛집 게시판 | 질문 게시판 | 싸피 예비생 게시판"),
fieldWithPath("imageUrl").type(JsonFieldType.STRING).description("게시판 배너 이미지"),
fieldWithPath("usedBoard").type(JsonFieldType.BOOLEAN).description("게시판 사용 여부"),
fieldWithPath("description").type(JsonFieldType.STRING).description("게시판의 특성을 설명해주는 설명문")
)));
}
Expand Down
Loading

0 comments on commit 3342317

Please sign in to comment.