From 578358d4085674148dda6520019a915eb634858e Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 16:09:11 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat(testing/week1)=20-=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EA=B8=B0=EB=B3=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 39 ++------------ build.gradle | 1 + .../week1/base/service/PostServiceTest.java | 24 +++++++++ .../service/PostServiceWithSpringTest.java | 24 +++++++++ .../PostServiceWithSpringWithMockTest.java | 53 +++++++++++++++++++ 5 files changed, 105 insertions(+), 36 deletions(-) create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceTest.java create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringTest.java create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringWithMockTest.java diff --git a/README.md b/README.md index 32187be..8cf6d12 100644 --- a/README.md +++ b/README.md @@ -1,39 +1,6 @@ -### Q. entity와 model은 왜 나뉘어 있나요? -PostRequest와 Post가 유사하게 생겼는데 클라이언트로부터 Post객체 그대로 요청 받아서 Post를 그대로 저장하면 편하고 관리할 클래스도 적어지지 않을까요? -### A. Entity와 DTO를 구분하기 위함입니다. -우선 Entity와 DTO의 정의부터 살펴보겠습니다. +### 실전 코딩 테스트 +- 코딩 과정 중 테스트 +- 코딩 이외의 테스트 -**Entity**는 DB 테이블에 존재하는 컬럼들을 필드로 가지는 객체를 뜻합니다. (즉, Entity는 DB 테이블 설계와 동일한 모습을 하고있겠죠.) -Entity는 DB 테이블과 1:1 대응이며, 테이블을 가지지 않는 컬럼을 필드로 갖지 않습니다. -**DTO**는 Data Transfer Object의 준말로 데이터가 계층을 오갈 때(transfer) 사용하는 객체입니다. DTO는 로직을 갖지 않는 순수한 데이터 객체이며, -getter/setter 메서드만을 가집니다. -Entity와 DTO를 구분지어 사용하는 이유는 '각 레이어간의 의존도를 줄이기 위함'입니다. 더 쉽게 예시를 들어보면 아래와 같습니다. -1. Post와 Tag를 각각 저장하도록 테이블을 별도로 설계했다고 해봅시다. - * 현재 우리 코드에서는 Post에 Tag를 같이 저장하도록 했지만, Tag의 내용이 많아 Post와 Tag를 각각 저장하도록 설계를 변경했습니다. - Post와 Tag의 테이블은 분리했지만 보통 우리가 게시글을 작성할 때 태그도 함께 작성하니 Request Body도 Post와 Tag를 한 번에 받아야 좋겠죠. - Post 클래스 하나로 요청과 DB 저장을 모두 처리했다면 위와 같은 상황 대처가 어려웠을텐데 Post와 PostRequest가 나뉘어 있으니 요청은 현재 PostRequest가 받던대로 게시글 내용과 Tag를 함께 받고 - 저장할 때 Post와 Tag 엔티티로 저장하면 위와 같은 상황에 대처가 가능해집니다. -2. 기획자가 게시글 작성과 관련한 기획을 변경한 상황을 가정해봅시다. - * 기능의 요구사항은 언제든 추가/변경/삭제가 생깁니다. 그럼 요청을 받아야하는 객체의 필드에도 추가/변경/삭제가 언제든 발생할 수 있습니다. - PostRequest가 없었다면, Post 클래스의 필드가 수시로 변경되어 DB에 데이터를 저장할 때 장애가 발생하기 쉽습니다. (없는 필드를 저장하려고 한다던지 등등) - ---- - -### Q. WebMVCConfig 파일은 왜 있나요? -### A. CORS을 허용하기 위해서입니다. -http://localhost:8080 이라는 url이 있다고 합시다. - -Origin은 host부분인 localhost와 포트인 8080을 모두 합친 것을 말합니다. http://localhost:9090은 포트가 다르니 Origin이 같다고 할 수 없죠. -서버가 이미 http://localhost:8080에서 돌고 있으니, 프론트 프로그램은 8080포트가 아닌 다른 포트(예를들면 9090)에서 돌게 됩니다. -브라우저는 보안상의 이유로 SOP(Same Origin Policy) 정책을 요구하고 있습니다. 프론트에서 api를 통해 정보를 얻어왔지만, -브라우저가 정보를 요청한 쪽(http://localhost:9090)과 정보를 준 쪽(http://localhost:8080)의 Origin이 달라 서버로부터 받아온 응답을 거절합니다. - -하지만 위처럼 서로 다른 Origin끼리 데이터를 주고 받아야하는 상황이 발생하기 때문에 별도의 예외 사항을 두었습니다. CORS가 바로 이 예외상황에 해당합니다. -CORS(Cross Origin Resource Sharing)는 다른 Origin으로 요청을 보내기 위해 지켜야하는 정책으로, 원래대로라면 SOP에 의해 막히게 될 요청을 풀어주는 정책입니다. -WebMvcConfig에서 CORS를 위해 추가한 파일이라고 알아주시면 될 것 같습니다!(프론트 수업을 위해 추가한 설정파일) - ❗️프론트엔드 수업하려면 해당 파일이 필수이니 프론트엔드 수업 전까지 반드시 pull하기❗️ - -글솜씨가 없어 간단히 적었지만, 토이프로젝트를 하다보면 정말 자주 마주치는 문제 중 하나가 CORS입니다!!! 기본적인 지식이기도 하니 시간되실 때 아래의 블로그를 읽어보셔도 좋을 것 같습니다 - -📌 https://it-eldorado.tistory.com/163 diff --git a/build.gradle b/build.gradle index 4619d59..097c408 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.junit.jupiter:junit-jupiter:5.8.1' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' //TODO: annotation build 활성화 언급하기 implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2' diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceTest.java new file mode 100644 index 0000000..48f0014 --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceTest.java @@ -0,0 +1,24 @@ +package com.cnu.real_coding_server.service.week1.base.service; + + +import com.cnu.real_coding_server.entity.Post; +import com.cnu.real_coding_server.service.PostService; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.assertj.core.api.Assertions.assertThat; + +class PostServiceTest { + + + + @DisplayName("테스트 코드 실행해보기") + @Test + void testDoCode() { + System.out.println("테스트 코드 실행!"); + } + +} \ No newline at end of file diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringTest.java new file mode 100644 index 0000000..832ecb1 --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringTest.java @@ -0,0 +1,24 @@ +package com.cnu.real_coding_server.service.week1.base.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.cnu.real_coding_server.entity.Post; +import com.cnu.real_coding_server.service.PostService; +import java.util.Optional; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class PostServiceWithSpringTest { + @Autowired + PostService postService; + + @DisplayName("테스트 코드에서 Spring Component 실행해보기") + @Test + void testDoCodeRun() { + Optional optPost = postService.getPost(1); + assertThat(optPost.isPresent()).isFalse(); + } +} diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringWithMockTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringWithMockTest.java new file mode 100644 index 0000000..65cbba5 --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostServiceWithSpringWithMockTest.java @@ -0,0 +1,53 @@ +package com.cnu.real_coding_server.service.week1.base.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import com.cnu.real_coding_server.entity.Post; +import com.cnu.real_coding_server.model.type.Tag; +import com.cnu.real_coding_server.repository.PostRepository; +import com.cnu.real_coding_server.service.PostService; +import java.util.Optional; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +@SpringBootTest +public class PostServiceWithSpringWithMockTest { + + @Autowired + PostService postService; + + @MockBean + PostRepository postRepository; + + @BeforeEach + void init() { + + } + + @DisplayName("테스트 코드에서 Spring Component 실행해보기") + @Test + void testDoCodeRun() { + // given : 로직 수행을 위한 상황(데이터) 제시 + String title = "만나서"; + String contents = "반가워요"; + Tag tag = Tag.SPRINGBOOT; + + Post testPostData = Post.builder() + .title(title) + .contents(contents) + .tag(tag) + .build(); + when(postService.getPost(1)).thenReturn(Optional.of(testPostData)); // when 때 실행 시 줄 데이터 세팅 + + // when : 로직을 수행 + Optional optPost = postService.getPost(1); + // then : 생각한 대로 결과가 나오는지 확인 + assertThat(optPost.isPresent()).isTrue(); + } +} From 2c1d928418f94efb46adcd5d88689354fb10923c Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 16:43:08 +0900 Subject: [PATCH 2/6] =?UTF-8?q?feat(testing/week1-practice)=20-=20?= =?UTF-8?q?=EB=B9=84=EC=86=8D=EC=96=B4=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 제목, 본문 확인 --- .../service/valid/PostValidService.java | 20 +++++++++++ .../base/service/PostBaseServiceTest.java | 15 ++++++++ .../week1/slang/service/PostServiceTest.java | 34 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostBaseServiceTest.java create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java diff --git a/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java b/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java new file mode 100644 index 0000000..f315450 --- /dev/null +++ b/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java @@ -0,0 +1,20 @@ +package com.cnu.real_coding_server.service.valid; + +import java.util.ArrayList; +import java.util.List; +import org.springframework.stereotype.Service; + +@Service +public class PostValidService { + public boolean isValidPost(List slangList, + String title, + String postContent) { + for (String slang : slangList) { + if(title.contains(slang) + || postContent.contains(slang)) { + return true; + } + } + return false; + } +} diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostBaseServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostBaseServiceTest.java new file mode 100644 index 0000000..6aaa24b --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/base/service/PostBaseServiceTest.java @@ -0,0 +1,15 @@ +package com.cnu.real_coding_server.service.week1.base.service; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class PostBaseServiceTest { + + + @DisplayName("테스트 코드 실행해보기") + @Test + void testDoCode() { + System.out.println("테스트 코드 실행!"); + } + +} \ No newline at end of file diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java new file mode 100644 index 0000000..7413eda --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java @@ -0,0 +1,34 @@ +package com.cnu.real_coding_server.service.week1.slang.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; + +import com.cnu.real_coding_server.service.valid.PostValidService; +import java.util.ArrayList; +import java.util.List; +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.test.context.SpringBootTest; + +@SpringBootTest +public class PostServiceTest { + + @Autowired + PostValidService postValidService; + + + @DisplayName("post 제목/본문에 비속어가 있나 테스트") + @Test + void testValidPostIncludeMockSlang() { + // given 시나리오 + String testTitle = "비속어가 섞인 제목"; + String testContent = "비속어가 섞인 욕"; + List slangList = List.of("비속어", "비속어2"); + + boolean validPost = postValidService.isValidPost(slangList, testTitle, testContent); + // then 검증 + assertThat(validPost).isEqualTo(true); + } +} From cb67ecd7a77c12fcf137f31e9fee7ef92f4b072c Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 18:21:54 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat(testing-week1-practice1)=20-=20error?= =?UTF-8?q?=20=ED=95=B8=EB=93=A4=EB=A7=81=20=EC=B6=94=EA=B0=80,=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5,=20=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../error/ErrorResponse.java | 14 +++ .../error/RealCodingExceptionHandler.java | 14 +++ .../error/SlangBadRequestException.java | 17 +++ .../service/PostService.java | 16 ++- .../service/valid/PostValidService.java | 3 +- .../practice/service/PostServiceTest.java | 101 ++++++++++++++++++ .../service/PostValidServiceTest.java} | 11 +- 7 files changed, 164 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/cnu/real_coding_server/error/ErrorResponse.java create mode 100644 src/main/java/com/cnu/real_coding_server/error/RealCodingExceptionHandler.java create mode 100644 src/main/java/com/cnu/real_coding_server/error/SlangBadRequestException.java create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java rename src/test/java/com/cnu/real_coding_server/service/week1/{slang/service/PostServiceTest.java => practice/service/PostValidServiceTest.java} (70%) diff --git a/src/main/java/com/cnu/real_coding_server/error/ErrorResponse.java b/src/main/java/com/cnu/real_coding_server/error/ErrorResponse.java new file mode 100644 index 0000000..a6672c1 --- /dev/null +++ b/src/main/java/com/cnu/real_coding_server/error/ErrorResponse.java @@ -0,0 +1,14 @@ +package com.cnu.real_coding_server.error; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class ErrorResponse { + String code; // 클라이언트랑 약속한 작동 + String message; +} diff --git a/src/main/java/com/cnu/real_coding_server/error/RealCodingExceptionHandler.java b/src/main/java/com/cnu/real_coding_server/error/RealCodingExceptionHandler.java new file mode 100644 index 0000000..17cdd60 --- /dev/null +++ b/src/main/java/com/cnu/real_coding_server/error/RealCodingExceptionHandler.java @@ -0,0 +1,14 @@ +package com.cnu.real_coding_server.error; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class RealCodingExceptionHandler { + @ExceptionHandler(SlangBadRequestException.class) + public ResponseEntity handleSlangRequestException(SlangBadRequestException exception) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.getErrorResponse()); + } +} diff --git a/src/main/java/com/cnu/real_coding_server/error/SlangBadRequestException.java b/src/main/java/com/cnu/real_coding_server/error/SlangBadRequestException.java new file mode 100644 index 0000000..7bd3bc7 --- /dev/null +++ b/src/main/java/com/cnu/real_coding_server/error/SlangBadRequestException.java @@ -0,0 +1,17 @@ +package com.cnu.real_coding_server.error; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.BAD_REQUEST) +public class SlangBadRequestException extends RuntimeException { + private static final long serialVersionUID = -4785136912743477236L; + + public SlangBadRequestException() { + super("비속어가 포함된 글은 등록할 수 없습니다"); + } + + public ErrorResponse getErrorResponse() { + return new ErrorResponse("slangInput", this.getMessage()); + } +} diff --git a/src/main/java/com/cnu/real_coding_server/service/PostService.java b/src/main/java/com/cnu/real_coding_server/service/PostService.java index 490a745..024ecaa 100644 --- a/src/main/java/com/cnu/real_coding_server/service/PostService.java +++ b/src/main/java/com/cnu/real_coding_server/service/PostService.java @@ -1,21 +1,28 @@ package com.cnu.real_coding_server.service; import com.cnu.real_coding_server.entity.Post; +import com.cnu.real_coding_server.error.SlangBadRequestException; import com.cnu.real_coding_server.model.request.PostRequest; import com.cnu.real_coding_server.repository.PostRepository; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; - +import com.cnu.real_coding_server.service.valid.PostValidService; import java.util.List; import java.util.Optional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor public class PostService { private final PostRepository postRepository; + private static final List slangList = List.of("비속어1", "비속어2"); + private final PostValidService postValidService; public Post createPost(PostRequest postRequest) { + + if (postValidService.isSlangInclude(slangList, postRequest.getTitle(), postRequest.getContents())) { + throw new SlangBadRequestException(); + } return postRepository.save(postRequest.toEntity()); } @@ -28,6 +35,9 @@ public Optional getPost(Integer postId) { } public Optional updatePost(Integer postId, PostRequest postRequest) { + if (postValidService.isSlangInclude(slangList, postRequest.getTitle(), postRequest.getContents())) { + throw new SlangBadRequestException(); + } return postRepository.findById(postId) .map(post -> { post.setTitle(postRequest.getTitle()); diff --git a/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java b/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java index f315450..22f8e51 100644 --- a/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java +++ b/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java @@ -1,12 +1,11 @@ package com.cnu.real_coding_server.service.valid; -import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; @Service public class PostValidService { - public boolean isValidPost(List slangList, + public boolean isSlangInclude(List slangList, String title, String postContent) { for (String slang : slangList) { diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java new file mode 100644 index 0000000..9c03d9e --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java @@ -0,0 +1,101 @@ +package com.cnu.real_coding_server.service.week1.practice.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import com.cnu.real_coding_server.entity.Post; +import com.cnu.real_coding_server.error.SlangBadRequestException; +import com.cnu.real_coding_server.model.request.PostRequest; +import com.cnu.real_coding_server.model.type.Tag; +import com.cnu.real_coding_server.service.PostService; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.Optional; +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.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +@SpringBootTest +public class PostServiceTest { + + @Autowired + PostService postService; + + @Autowired + ObjectMapper mapper; + + @BeforeEach + void init() { + + } + + @DisplayName("비속어 제목에 포함된 저장 테스트") + @Transactional + @Test + void createPost() throws JsonProcessingException { + String title = "비속어"; + String content = "비속어"; + Tag tag = Tag.SPRINGBOOT; + PostRequest postRequest = mapper.readValue( + """ + { + "title": "%s", + "contents": "%s", + "tag": "%s" + } + """.formatted(title, content, tag) + , PostRequest.class); + + Post post = postService.createPost(postRequest); + assertAll("verify object", + () -> assertThat(post.getTitle()).isEqualTo(title), + () -> assertThat(post.getContents()).isEqualTo(content), + () -> assertThat(post.getTag()).isEqualTo(tag) + ); + } + + @DisplayName("비속어 제목에 포함된 저장/업데이트 테스트") + @Transactional + @Test + void updatePost() throws JsonProcessingException { + // given + String title = "정상 제목"; + String content = "정상 본문"; + Tag tag = Tag.SPRINGBOOT; + PostRequest postRequest = mapper.readValue( + """ + { + "title": "%s", + "contents": "%s", + "tag": "%s" + } + """.formatted(title, content, tag) + , PostRequest.class); + Post post = postService.createPost(postRequest); + String updatedTitle = "정상 제목1"; + String updatedContent = "정상 본문2"; + Tag updatedTag = Tag.JAVA; + PostRequest updatedPostRequest = mapper.readValue( + """ + { + "title": "%s", + "contents": "%s", + "tag": "%s" + } + """.formatted(updatedTitle, updatedContent, updatedTag) + , PostRequest.class); + // when + Optional optPost = postService.updatePost(post.getId(), updatedPostRequest); + + // then + Post updatedPost = optPost.get(); + assertAll("verify object", + () -> assertThat(updatedPost.getTitle()).isEqualTo(updatedTitle), + () -> assertThat(updatedPost.getContents()).isEqualTo(updatedContent), + () -> assertThat(updatedPost.getTag()).isEqualTo(updatedTag) + ); + } +} diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java similarity index 70% rename from src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java rename to src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java index 7413eda..716ca0e 100644 --- a/src/test/java/com/cnu/real_coding_server/service/week1/slang/service/PostServiceTest.java +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java @@ -1,19 +1,16 @@ -package com.cnu.real_coding_server.service.week1.slang.service; +package com.cnu.real_coding_server.service.week1.practice.service; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; import com.cnu.real_coding_server.service.valid.PostValidService; -import java.util.ArrayList; import java.util.List; -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.test.context.SpringBootTest; @SpringBootTest -public class PostServiceTest { +public class PostValidServiceTest { @Autowired PostValidService postValidService; @@ -21,13 +18,13 @@ public class PostServiceTest { @DisplayName("post 제목/본문에 비속어가 있나 테스트") @Test - void testValidPostIncludeMockSlang() { + void testValidPostIncludeSlang() { // given 시나리오 String testTitle = "비속어가 섞인 제목"; String testContent = "비속어가 섞인 욕"; List slangList = List.of("비속어", "비속어2"); - boolean validPost = postValidService.isValidPost(slangList, testTitle, testContent); + boolean validPost = postValidService.isSlangInclude(slangList, testTitle, testContent); // then 검증 assertThat(validPost).isEqualTo(true); } From 5833ed916e255bd48722d98719ab6350d0807b8c Mon Sep 17 00:00:00 2001 From: inspire12 Date: Fri, 21 Apr 2023 21:44:50 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat(testing/week1-practice1)=20-=20?= =?UTF-8?q?=EC=8B=A4=EC=8A=B5=EC=9A=A9=20base=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: inspire12 --- .../service/PostValidServiceTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java index 716ca0e..0840506 100644 --- a/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java @@ -28,4 +28,23 @@ void testValidPostIncludeSlang() { // then 검증 assertThat(validPost).isEqualTo(true); } + + @DisplayName("post 제목에 비속어가 있나 테스트") + @Test + void testValidPostIncludeSlangInTitle() { + // given + String testTitle = "비속어가 섞인 제목"; + List slangList = List.of("비속어", "비속어같지만일만어"); + // when postValidService 에 제목에 비속어가 있는지 확인하는 함수를 만들어 실행 결과 반환 + // then 결과가 true인지 확인 + } + + @DisplayName("post 본문에 비속어가 있나 테스트") + @Test + void testValidPostIncludeSlangInContent() { + String testContent = "비속어가 섞인 제목"; + List slangList = List.of("비속어", "비속어같지만일만어"); + // when postValidService 에 본문에 비속어가 있는지 확인하는 함수를 만들어 실행 결과 반환 + // then 결과가 true인지 확인 + } } From 4f9befdfe080fad42de50071a1445544e26e3482 Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 23 Apr 2023 20:08:21 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat(testing/week1-practice1)=20-=20update?= =?UTF-8?q?=20=EB=B6=80=EB=B6=84=20=EC=A0=95=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: inspire12 --- .../service/week1/practice/service/PostServiceTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java index 9c03d9e..7a79b1d 100644 --- a/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java @@ -4,7 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertAll; import com.cnu.real_coding_server.entity.Post; -import com.cnu.real_coding_server.error.SlangBadRequestException; import com.cnu.real_coding_server.model.request.PostRequest; import com.cnu.real_coding_server.model.type.Tag; import com.cnu.real_coding_server.service.PostService; @@ -58,7 +57,6 @@ void createPost() throws JsonProcessingException { } @DisplayName("비속어 제목에 포함된 저장/업데이트 테스트") - @Transactional @Test void updatePost() throws JsonProcessingException { // given From 08fcb377038952770b0e216b74e83a446b518775 Mon Sep 17 00:00:00 2001 From: kimhyeonju Date: Thu, 11 May 2023 15:56:16 +0900 Subject: [PATCH 6/6] PR --- .DS_Store | Bin 0 -> 6148 bytes src/.DS_Store | Bin 0 -> 6148 bytes src/main/.DS_Store | Bin 0 -> 6148 bytes src/main/java/.DS_Store | Bin 0 -> 6148 bytes .../service/valid/PostValidService.java | 2 +- 5 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/main/.DS_Store create mode 100644 src/main/java/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..73ca56a3bd252a106254f7f7ca2b406217612793 GIT binary patch literal 6148 zcmeHK%}T>S5Z-O8O({YS3Oz1(Eg0LPh?h|73mDOZN=;1BV9b^zHHT8jSzpK}@p+ut z-GIfMMeGdhe)GGV{UH0p7~|tbc*vN|7_*=ua#U&r-L;{HNk-&2Mm7&(8G!W>%uVdC z1AcpxWh`Y6LGk_j<0#7==aV;@t*!00)wa6Uo%bXQFZc6BHulpSv@WHLgG%>cpbmMi!BQiRf__yF PNEZP`2zA83FEH>0dNxUY literal 0 HcmV?d00001 diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7b29ec7d86c0bf8de468d3801f8a3aba7534e4cd GIT binary patch literal 6148 zcmeHK%}T>S5Z-O0O({YS3OxqA7L09C#7n641&ruHr6#0kFx@RpY7V84v%Zi|;`2DO zyAg}^Dq?3~_nY6{><8H&#uy*W<3q+A#+VHak)yIk(7iTPGs%b?$B2q-8l^G<`_07u zI^efkY{Ej8F!A;KqiLFCx#zv}THV^(ZrBaGW#0vlvIz1ppJ#(`c7xWXlxbAyVRV&@ zi;=T?CPf}5Vmwm?Nj!#>+v`NcvKYv@h%;3i=z!g{n4M(x z9W9qldvE{X^kVdsPK12XgmU0p$&SGaUP1X>&vP&nsT2?3tMaQnLSldzAO?tm&1Jxx z33h99DWH`T1H`}&4B-CYLPPWomKxR80UchSF>WEEfR1koMAx8iu+#_<5UxuBbtyMb z46e(;@0vW{V5w1;Gp=Taam>usS5Z<-XrW7Fug&r5Y7OZVi#7l_v1&ruHr6#0kG-gYaHiuHkSzpK}@p+ut z-H63{6|pn0`_1oe_JiyXV~l&#=zuYYF=j(U6xeTXnl`H|<;hK^A@<o3*BUyNInnszb^>x5**o~pnn$LUvPFr+4M~k+YA9q3T z_YN0}hP}JDe{w#2N}f~sq6y`|w~}px6}*D-xt^InNfVjggRjc3@(76mVt^PR2G*AW zb0*l$^`(GTP7DwOKQMs%gMfzU8Z0%ctphr|K4aWML;)S&5{SZ}Yp~P^5fH9R0d*-i zPYkZh!7ognYp~R)%NbWQ!#HN<>hZ$W?BEwFopDzq^~3-%u*^VBn+~4;XYk9ceB>{e zkVOm-1OJQxZVmi_2a7Ui>$m0MSu3F3K|{g35)}~8mo5Qd;6Bn)K^+&UL!N7})QF>? SUzG#WML-ck9Wn3=4155pF-kfB literal 0 HcmV?d00001 diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..226c0e3f87b36202954b719717e4d62d2f3d9c78 GIT binary patch literal 6148 zcmeHK%}T>S5Z<-bZYe?z3OxqA7L09C#7l_v1&ruHr6#6mFwK@GHHT8jSzpK}@p+ut z-GIfMMeGdhe)GGV{UH0p7~|d|I%3RXj0tFn9F-bDcWr3PBqMShBb$YZ48i&cXD0U7 z0l&S$9+_m*u=xJ{Nu1@5^U1dw&F!6*Xo-%v_n&3q=Rv;6ykLHd)|He=Sm{A{9Z!m} zwRbMlJc!fDTouI81XAv9;xv+lCl_gysajtLL|e4SR(H7^44t0ccTQG4dwJRieKWKkjpoRh5A8crduEA2H+B%@a>odlih$x`rTLRHG=o&0FLIi~CQb1kG%@c#` za`4+G&ox+T)a8t;nPD6=bM<)PYIgA3DxGmxBlW}pF|f`+Lz@0 slangList, String title, String postContent) {