Skip to content

Commit

Permalink
[LH-92] manager(create, updateLike) test 코드 추가(#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonjun-An committed Aug 4, 2023
1 parent 96d2490 commit fcbcf35
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public class BoardController {
private final BoardManager boardManager;

@PostMapping("/board")
public ResponseEntity<ResponseDto> create(@RequestBody @Valid BoardCreateRequest boardCreateRequest) {
public ResponseEntity<ResponseDto<Long>> create(@RequestBody @Valid BoardCreateRequest boardCreateRequest) {

boardManager.create(boardCreateRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(ResponseDto.builder()
Long questionId = boardManager.create(boardCreateRequest);
return ResponseEntity.status(HttpStatus.CREATED).body(ResponseDto.<Long>builder()
.code("20100")
.message("Successfully created")
.data(questionId)
.build()); //보드 정보 내려줌
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ public class Question extends BaseEntity {


@Builder
public Question(Member createdMember, Category category, Integer likes, String contents) {
public Question(Long id, Member createdMember, Category category, Integer likes, String contents) {
this.id = id;
this.createdMember = createdMember;
this.category = category;
this.likes = likes;
this.likes = 0;
this.contents = contents;
this.isValid = Boolean.FALSE;
this.isRecommended = Boolean.FALSE;
}

public void addLikes(Integer likes) {
this.likes = likes+1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BoardManager {
private final BoardService boardService;
private final LikeMemberService likeMemberService; // 이걸 주입안하면 아예 서비스 쓸 수가 없음/ 왜인지 알아보자

public void create(BoardCreateRequest boardCreateRequest) {
public Long create(BoardCreateRequest boardCreateRequest) {

Member member = memberService.findById(boardCreateRequest.getMemberId());
Category category = categoryService.findById(boardCreateRequest.getCategoryId());
Expand All @@ -36,6 +36,7 @@ public void create(BoardCreateRequest boardCreateRequest) {
.contents(boardCreateRequest.getContent())
.build();
boardService.save(question);
return question.getId();
}


Expand All @@ -54,6 +55,9 @@ public BoardLikeResponse updateLike(BoardUpdateLikeRequest boardUpdateLikeReques

Member member = memberService.findById(boardUpdateLikeRequest.getMember_id());
Question question = boardService.findById(questionId);

question.addLikes(question.getLikes());

BoardLikeResponse boardLikeResponse = new BoardLikeResponse();
boardLikeResponse.setQuestionId(question.getId());
boardLikeResponse.setMemberId(question.getCreatedMember().getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -18,4 +19,10 @@ public class Category extends BaseEntity {
private Long id;

private String name;

@Builder
public Category(Long id, String name) {
this.id = id;
this.name = name;
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/lighthouse/lingoswap/member/entity/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.*;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -25,7 +26,20 @@ public class Member extends BaseEntity {
private String profileImage;
private String email;

@Builder
public Member(Long id, String gender, Boolean isValid, LocalDate birthday, String name, String description, String profileImage, String email) {
this.id = id;
this.gender = gender;
this.isValid = isValid;
this.birthday = birthday;
this.name = name;
this.description = description;
this.profileImage = profileImage;
this.email = email;
}

public Member(String email) {
this.email = email;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.lighthouse.lingoswap.board.service;

import com.lighthouse.lingoswap.board.dto.BoardCreateRequest;
import com.lighthouse.lingoswap.board.dto.BoardLikeResponse;
import com.lighthouse.lingoswap.board.dto.BoardResponse;
import com.lighthouse.lingoswap.board.dto.BoardUpdateLikeRequest;
import com.lighthouse.lingoswap.board.entity.Question;
import com.lighthouse.lingoswap.member.entity.Category;
import com.lighthouse.lingoswap.member.entity.Member;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(SpringExtension.class)
@SpringBootTest
class BoardManagerTest {

@PersistenceContext EntityManager em;

@Autowired BoardManager boardManager;
@Autowired BoardService boardService;

@Test
@Transactional
void create() {
//given
Member member = Member.builder()
.gender("M")
.name("A")
.description("회원1")
.build();

em.persist(member);

Category category = Category.builder()
.name("요리")
.build();

em.persist(category);


//when
BoardCreateRequest boardCreateRequest = new BoardCreateRequest();
boardCreateRequest.setCategoryId(category.getId());
boardCreateRequest.setMemberId(member.getId());
boardCreateRequest.setContent("첫 게시물 \n 첫 내용");
Long questionId = boardManager.create(boardCreateRequest);

//then
Question question = boardService.findById((questionId));
Assertions.assertThat(member.getId()).isEqualTo(question.getCreatedMember().getId());
Assertions.assertThat("첫 게시물 \n 첫 내용").isEqualTo(question.getContents());
Assertions.assertThat(Boolean.FALSE).isEqualTo(question.getIsValid());
Assertions.assertThat(Boolean.FALSE).isEqualTo(question.getIsRecommended());

em.clear();
}

@Test
@Transactional
void updateLike() {
//given
Member member = Member.builder()
.gender("M")
.name("A")
.description("회원1")
.build();

em.persist(member);

Category category = Category.builder()
.name("요리")
.build();

em.persist(category);

BoardCreateRequest boardCreateRequest = new BoardCreateRequest();
boardCreateRequest.setCategoryId(category.getId());
boardCreateRequest.setMemberId(member.getId());
boardCreateRequest.setContent("첫 게시물 \n 첫 내용");
Long questionId = boardManager.create(boardCreateRequest);

Assertions.assertThat(boardService.findById(questionId).getLikes()).isEqualTo(0);

//when
BoardUpdateLikeRequest boardUpdateLikeRequest = new BoardUpdateLikeRequest();
boardUpdateLikeRequest.setMember_id(member.getId());
BoardLikeResponse boardLikeResponse = boardManager.updateLike(boardUpdateLikeRequest, questionId);
Assertions.assertThat(boardLikeResponse.getLikes()).isEqualTo(1);
Assertions.assertThat(boardLikeResponse.getQuestionId()).isEqualTo(questionId);
Assertions.assertThat(boardLikeResponse.getMemberId()).isEqualTo(member.getId());





}

@Test
void read() {
}
}

0 comments on commit fcbcf35

Please sign in to comment.