Skip to content

Commit

Permalink
[정유정] 12주차 과제 제출 (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
xyzwv authored Oct 8, 2024
2 parents 1224535 + b2e0494 commit 2b0d8b6
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 9 deletions.
1 change: 1 addition & 0 deletions community/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'com.h2database:h2'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import efub.assignment.community.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post,Long> {
import java.util.List;

public interface PostRepository extends JpaRepository<Post,Long> {
List<Post> findByWriterOpen(Boolean writerOpen);

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public PostResponseDto getOnePost(@PathVariable(name="post_id")Long post_id){
return PostResponseDto.from(post, post.getAccount().getNickname(), post.getBoard().getBoardId());
}

@PutMapping("/posts/{post_id}")
@PatchMapping("/posts/{post_id}")
@ResponseStatus(value = HttpStatus.OK)
public PostResponseDto updatePost(@PathVariable(name="post_id")Long post_id,
@RequestBody @Valid PostUpdateDto dto){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class Post extends BaseTimeEntity {
@Column(nullable = false, length = 1000)
private String content;

@Column(nullable = false, length = 10)
private String writerOpen;
@Column(nullable = false)
private Boolean writerOpen;

/* mappedBy : 연관관계의 주인 */
/* cascade : 엔티티 삭제 시 연관된 엔티티의 처리 방식 */
Expand All @@ -55,7 +55,7 @@ public class Post extends BaseTimeEntity {
private List<PostHeart> postHeartList = new ArrayList<>();

@Builder
public Post(Account account, Board board, String title, String content, String writerOpen){
public Post(Account account, Board board, String title, String content, Boolean writerOpen){
this.account = account;
this.board = board;
this.title = title;
Expand All @@ -66,5 +66,6 @@ public Post(Account account, Board board, String title, String content, String w
public void update(PostUpdateDto dto){
this.title = dto.getTitle();
this.content = dto.getContent();
this.writerOpen = dto.getWriterOpen();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class PostRequestDto {
@NotBlank(message = "내용은 필수입니다.")
private String content;

@Value("${writer.open:false}")
private String writerOpen;
@NotBlank
private Boolean writerOpen;

public Post toEntity(Board board, Account account){
return Post.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PostResponseDto {
private String writerNickname;
private String title;
private String content;
private String writerOpen;
private Boolean writerOpen;
private LocalDateTime createdDate;
private LocalDateTime modifiedDate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ public class PostUpdateDto {
@NotBlank(message = "글의 내용은 필수입니다.")
private String content;

@NotBlank
private Boolean writerOpen;

@NotBlank(message = "글의 익명여부는 필수입니다.")
@Builder
public PostUpdateDto(String title, String content){
public PostUpdateDto(String title, String content, Boolean writerOpen){
this.title = title;
this.content = content;
this.writerOpen = writerOpen;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package efub.assignment.community.account;

import efub.assignment.community.account.domain.Account;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
class AccountRepositoryTest {

@Autowired
private AccountRepository accountRepository;

@Test
@DisplayName("이메일 중복 확인 - 성공")
void validateEmail() {
// given
Account account = Account.builder()
.email("[email protected]")
.password("password123")
.nickname("fubie")
.university("이화여자대학교")
.studentId("1234567")
.build();
accountRepository.save(account);

// when
Boolean exists = accountRepository.existsByEmail("[email protected]");

// then
assertTrue(exists, "이메일이 존재합니다.");
}

@Test
@DisplayName("닉네임으로 계정 조회 - 실패")
void findByNickname() {
// given
Account account = Account.builder()
.email("[email protected]")
.password("password123")
.nickname("fubie")
.university("이화여자대학교")
.studentId("1234567")
.build();
accountRepository.save(account);
String searchNickname = "iamfubie";

// when
Optional<Account> searchAccount = accountRepository.findByNickname(searchNickname);

// then
assertFalse(searchAccount.isPresent(), "존재하지 않는 닉네임입니다.");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package efub.assignment.community.account.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ActiveProfiles;

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

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
class AccountTest {

@Test
@DisplayName("Account 생성 - 성공")
void createAccount() {
// given
String email = "[email protected]";
String password = "password123";
String nickname = "fubie";
String university = "이화여자대학교";
String studentId = "1234567";

// when
Account account = Account.builder()
.email(email)
.password(password)
.nickname(nickname)
.university(university)
.studentId(studentId)
.build();

// then
assertAll("Account 필드 값 검증",
() -> assertEquals(email, account.getEmail()),
() -> assertEquals(password, account.getPassword()),
() -> assertEquals(nickname, account.getNickname()),
() -> assertEquals(university, account.getUniversity()),
() -> assertEquals(studentId, account.getStudentId()),
() -> assertEquals(AccountStatus.REGISTERED, account.getStatus())
);
}

@Test
@DisplayName("비어 있는 닉네임으로 업데이트 - 실패")
void updateAccount(){
// given
Account account = Account.builder()
.email("[email protected]")
.password("password123")
.nickname("fubie")
.university("이화여자대학교")
.studentId("1234567")
.build();

// when
String newNickname = "";

// then
assertTrue(newNickname.isEmpty(), "닉네임은 비어 있을 수 없습니다.");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package efub.assignment.community.post;

import efub.assignment.community.account.AccountRepository;
import efub.assignment.community.account.domain.Account;
import efub.assignment.community.board.BoardRepository;
import efub.assignment.community.board.domain.Board;
import efub.assignment.community.post.domain.Post;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.Optional;

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

@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("test")
class PostRepositoryTest {
@Autowired
private TestEntityManager testEntityManager;

@Autowired
private PostRepository postRepository;

@Autowired
private AccountRepository accountRepository;

@Autowired
private BoardRepository boardRepository;

private Account account;
private Board board;

@BeforeEach
void setup() {
account = Account.builder()
.email("[email protected]")
.password("password123")
.nickname("fubie")
.university("이화여자대학교")
.studentId("1234567")
.build();
testEntityManager.persist(account);

board = Board.builder()
.account(account)
.boardName("테스트 게시판")
.boardDescription("테스트 설명")
.boardNotice("테스트 공지사항")
.build();
testEntityManager.persist(board);
testEntityManager.flush();
}

@Test
@DisplayName("writerOpen이 true인 Post 조회 - 성공")
void findPublicPost() {
// given
String title1 = "공개 게시글";
String content1 = "이건 보여야 함";
Boolean writerOpen1 = true;

String title2 = "비공개 게시글";
String content2 = "이건 안 보여야 함";
Boolean writerOpen2 = false;

Post publicPost = Post.builder()
.account(account)
.board(board)
.title(title1)
.content(content1)
.writerOpen(writerOpen1)
.build();
testEntityManager.persist(publicPost);

Post privatePost = Post.builder()
.account(account)
.board(board)
.title(title2)
.content(content2)
.writerOpen(writerOpen2)
.build();
testEntityManager.persist(privatePost);
testEntityManager.flush();

// when
List<Post> openPosts = postRepository.findByWriterOpen(true);

// then
assertEquals(1, openPosts.size(), "writerOpen이 true인 게시글이 하나여야 합니다.");
assertEquals(title1, openPosts.get(0).getTitle(), "조회된 게시글의 제목이 일치해야 합니다.");
}


@Test
@DisplayName("Post 조회 실패 - 존재하지 않는 ID")
void findPostByDeletedId() {
// given
Post post = Post.builder()
.account(account)
.board(board)
.title("제목")
.content("내용")
.writerOpen(true)
.build();
testEntityManager.persist(post);
testEntityManager.flush();
Long deletedId = post.getPostId();
postRepository.delete(post);
testEntityManager.flush();

// when
Optional<Post> searchPost = postRepository.findById(deletedId);

// then
assertFalse(searchPost.isPresent(), "존재하지 않는 ID로 Post를 조회할 수 없습니다. ");
}

}
Loading

0 comments on commit 2b0d8b6

Please sign in to comment.