Skip to content

Commit

Permalink
[kiworkshop#108] 범용게시판 만들기
Browse files Browse the repository at this point in the history
- 범용게시판 CRUD 구현
- 서비스 테스트 작성
  • Loading branch information
harrisleesh committed Aug 23, 2020
1 parent f11e79e commit 4ceef44
Show file tree
Hide file tree
Showing 9 changed files with 272 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.kiworkshop.community.article.api;

import lombok.RequiredArgsConstructor;
import org.kiworkshop.community.article.api.dto.ArticleRequestDto;
import org.kiworkshop.community.article.api.dto.ArticleResponseDto;
import org.kiworkshop.community.article.service.ArticleService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
public class ArticleController {
private final ArticleService articleService;

@GetMapping("/{id}")
public ResponseEntity<ArticleResponseDto> read(@PathVariable Long id) {
return ResponseEntity.ok(articleService.read(id));
}

@PostMapping
public ResponseEntity<Long> create(@RequestBody ArticleRequestDto articleRequestDto) {
return ResponseEntity.ok(articleService.create(articleRequestDto));
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
articleService.delete(id);
return ResponseEntity.ok().build();
}

@PutMapping("/{id}")
public ResponseEntity<Void> update(@PathVariable Long id, @RequestBody ArticleRequestDto articleRequestDto) {
articleService.update(id, articleRequestDto);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.kiworkshop.community.article.api.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.kiworkshop.community.article.entity.Article;

@Getter
@NoArgsConstructor
public class ArticleRequestDto {
private String title;

@Builder
public ArticleRequestDto(String title, String content) {
this.title = title;
this.content = content;
}

private String content;

public Article toEntity() {
return Article.builder()
.title(title)
.content(content)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.kiworkshop.community.article.api.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.kiworkshop.community.article.entity.Article;

@Getter
@NoArgsConstructor
public class ArticleResponseDto {
private String title;
private String content;

@Builder
public ArticleResponseDto(String title, String content) {
this.title = title;
this.content = content;
}

public static ArticleResponseDto from(Article article) {
return ArticleResponseDto.builder()
.title(article.getTitle())
.content(article.getContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.kiworkshop.community.article.entity;

import lombok.Builder;
import lombok.Getter;
import org.kiworkshop.community.common.domain.BaseEntity;

import javax.persistence.Entity;

@Entity
@Getter
public class Article extends BaseEntity {
private String title;
private String content;
private Long userId;

@Builder
public Article(String title, String content, Long userId) {
this.title = title;
this.content = content;
this.userId = userId;
}

public void update(Article article) {
this.title = article.title;
this.content = article.content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.kiworkshop.community.article.entity;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ArticleRepository extends JpaRepository<Article, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.kiworkshop.community.article.exception;

public class ArticleException extends RuntimeException {
public ArticleException(String s) {
super(s);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.kiworkshop.community.article.service;

import lombok.RequiredArgsConstructor;
import org.kiworkshop.community.article.api.dto.ArticleRequestDto;
import org.kiworkshop.community.article.api.dto.ArticleResponseDto;
import org.kiworkshop.community.article.entity.Article;
import org.kiworkshop.community.article.entity.ArticleRepository;
import org.kiworkshop.community.article.exception.ArticleException;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ArticleService {
private final ArticleRepository articleRepository;

public Long create(ArticleRequestDto articleRequestDto) {
Article article = articleRepository.save(articleRequestDto.toEntity());
return article.getId();
}

public ArticleResponseDto read(Long id) {
Article article = findById(id);
return ArticleResponseDto.from(article);
}

public void update(Long id, ArticleRequestDto articleRequestDto) {
Article article = findById(id);
article.update(articleRequestDto.toEntity());
}

public void delete(Long id) {
articleRepository.deleteById(id);
}

private Article findById(Long id) {
return articleRepository.findById(id).orElseThrow(() -> new ArticleException("게시물을 찾을 수 없습니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.kiworkshop.community.article.entity;

import org.springframework.test.util.ReflectionTestUtils;

import java.time.ZonedDateTime;

public class ArticleTest {
public static Article createArticleTestFixture() {
Article article = Article.builder().title("title").content("content").build();
ReflectionTestUtils.setField(article, "id", 1L);
ReflectionTestUtils.setField(article, "updatedAt", ZonedDateTime.now());
ReflectionTestUtils.setField(article, "createdAt", ZonedDateTime.now());
return article;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.kiworkshop.community.article.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kiworkshop.community.article.api.dto.ArticleRequestDto;
import org.kiworkshop.community.article.api.dto.ArticleResponseDto;
import org.kiworkshop.community.article.entity.Article;
import org.kiworkshop.community.article.entity.ArticleRepository;
import org.kiworkshop.community.article.exception.ArticleException;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.kiworkshop.community.article.entity.ArticleTest.createArticleTestFixture;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;

@ExtendWith(MockitoExtension.class)
class ArticleServiceTest {
private ArticleService articleService;
@Mock
private ArticleRepository articleRepository;

@BeforeEach
void setUp() {
this.articleService = new ArticleService(articleRepository);
}

@Test
void create() {
//given
ArticleRequestDto articleRequestDto = ArticleRequestDto.builder().title("title").content("content").build();
Article article = createArticleTestFixture();
given(articleRepository.save(any(Article.class))).willReturn(article);
//when
Long id = articleService.create(articleRequestDto);
//then
assertThat(id).isNotNull();
then(articleRepository).should().save(any(Article.class));
}

@Test
void read() {
//given
Article article = createArticleTestFixture();
given(articleRepository.findById(anyLong())).willReturn(Optional.of(article));
//when
ArticleResponseDto articleResponseDto = articleService.read(1L);
//then
assertThat(articleResponseDto.getTitle()).isEqualTo(article.getTitle());
assertThat(articleResponseDto.getContent()).isEqualTo(article.getContent());
then(articleRepository).should().findById(anyLong());
}

@Test
void update() {
//given
ArticleRequestDto articleRequestDto = ArticleRequestDto.builder().title("title1").content("content1").build();
Article article = createArticleTestFixture();
given(articleRepository.findById(anyLong())).willReturn(Optional.of(article));
//when
articleService.update(1L, articleRequestDto);
//then
then(articleRepository).should().findById(anyLong());
}

@Test
void delete() {
//when
articleService.delete(1L);
//then
then(articleRepository).should().deleteById(anyLong());
}

@Test
void findByIdThrowsException() {
//given
ArticleRequestDto articleRequestDto = ArticleRequestDto.builder().title("title1").content("content1").build();
//when & then
assertThrows(ArticleException.class, () -> articleService.read(1L));
assertThrows(ArticleException.class, () -> articleService.update(1L, articleRequestDto));
}
}

0 comments on commit 4ceef44

Please sign in to comment.