From 578358d4085674148dda6520019a915eb634858e Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 16:09:11 +0900 Subject: [PATCH 01/10] =?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 02/10] =?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 03/10] =?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 e75f283f9b50d39fb85168e0a47c61099e32fb90 Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 19:36:59 +0900 Subject: [PATCH 04/10] =?UTF-8?q?feat(week1-practice2)=20-=20jpa=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=EB=90=9C=20=EB=82=B4=EC=9A=A9=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8,=20dev=20properties=20=EC=B6=94=EA=B0=80,=20h2=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../service/PostService.java | 3 + src/main/resources/application-dev.yml | 25 +++++++ src/main/resources/application.yml | 4 +- .../service/PostServiceWithSpringTest.java | 4 +- .../practice/service/PostServiceTest.java | 73 +++++++------------ .../service/PostValidServiceTest.java | 2 + .../practice/service/fixture/PostFixture.java | 71 ++++++++++++++++++ 8 files changed, 135 insertions(+), 48 deletions(-) create mode 100644 src/main/resources/application-dev.yml create mode 100644 src/test/java/com/cnu/real_coding_server/service/week1/practice/service/fixture/PostFixture.java diff --git a/build.gradle b/build.gradle index 097c408..c6bb5f6 100644 --- a/build.gradle +++ b/build.gradle @@ -23,6 +23,7 @@ dependencies { 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' + testImplementation 'org.projectlombok:lombok:1.18.22' 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/main/java/com/cnu/real_coding_server/service/PostService.java b/src/main/java/com/cnu/real_coding_server/service/PostService.java index 024ecaa..30e901e 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 @@ -8,8 +8,10 @@ import java.util.List; import java.util.Optional; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +@Slf4j @Service @RequiredArgsConstructor public class PostService { @@ -23,6 +25,7 @@ public Post createPost(PostRequest postRequest) { if (postValidService.isSlangInclude(slangList, postRequest.getTitle(), postRequest.getContents())) { throw new SlangBadRequestException(); } + log.info("정상 저장 확인"); return postRepository.save(postRequest.toEntity()); } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml new file mode 100644 index 0000000..7e8c235 --- /dev/null +++ b/src/main/resources/application-dev.yml @@ -0,0 +1,25 @@ +spring: + # H2 Setting Info (H2 Console? ???? ?? ???? ??) + h2: + console: + enabled: true # H2 Console? ???? ?? (H2 Console? H2 Database? UI? ????? ??) + path: /h2-console # H2 Console? Path + # Database Setting Info (Database? H2? ???? ?? H2?? ?? ??) + datasource: + driver-class-name: org.h2.Driver # Database? H2? ?????. + url: jdbc:h2:file:~/demodb # H2 ?? ?? + username: sa # H2 ?? ? ??? username ?? (??? ??? ??) + password: # H2 ?? ? ??? password ?? (??? ??? ??) + + jpa: + hibernate: + ddl-auto: none # ??????? ??? ? ??????? ????? ?? ??? ?? + naming: + physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl + implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl + properties: + hibernate: + format_sql: true # ???? query? ??? + +logging.level: + org.hibernate.SQL: debug \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9f1e564..f26d12b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -13,10 +13,12 @@ spring: jpa: hibernate: - ddl-auto: create # ??????? ??? ? ??????? ????? ?? ??? ?? + ddl-auto: none # ??????? ??? ? ??????? ????? ?? ??? ?? + properties: hibernate: format_sql: true # ???? query? ??? + open-in-view: false logging.level: org.hibernate.SQL: debug \ 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 index 832ecb1..d276b67 100644 --- 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 @@ -4,6 +4,7 @@ import com.cnu.real_coding_server.entity.Post; import com.cnu.real_coding_server.service.PostService; +import com.cnu.real_coding_server.service.week1.practice.service.fixture.PostFixture; import java.util.Optional; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -18,7 +19,8 @@ public class PostServiceWithSpringTest { @DisplayName("테스트 코드에서 Spring Component 실행해보기") @Test void testDoCodeRun() { - Optional optPost = postService.getPost(1); + Post post = postService.createPost(PostFixture.getNormalPostRequest()); + Optional optPost = postService.getPost(post.getId()); assertThat(optPost.isPresent()).isFalse(); } } 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..ba101ea 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 @@ -2,12 +2,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertThrows; 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.cnu.real_coding_server.service.week1.practice.service.fixture.PostFixture; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Optional; @@ -16,8 +18,10 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.transaction.annotation.Transactional; +@ActiveProfiles("dev") @SpringBootTest public class PostServiceTest { @@ -32,70 +36,47 @@ void init() { } - @DisplayName("비속어 제목에 포함된 저장 테스트") - @Transactional + @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); + PostRequest postRequest = PostFixture.getNormalPostRequest(); Post post = postService.createPost(postRequest); assertAll("verify object", - () -> assertThat(post.getTitle()).isEqualTo(title), - () -> assertThat(post.getContents()).isEqualTo(content), - () -> assertThat(post.getTag()).isEqualTo(tag) + () -> assertThat(post.getTitle()).isEqualTo(postRequest.getTitle()), + () -> assertThat(post.getContents()).isEqualTo(postRequest.getContents()), + () -> assertThat(post.getTag()).isEqualTo(postRequest.getTag()) ); } - @DisplayName("비속어 제목에 포함된 저장/업데이트 테스트") + @DisplayName("비속어 글 저장 테스트") @Transactional @Test - void updatePost() throws JsonProcessingException { + void createPostWithSlang() { + PostRequest postRequest = PostFixture.getSlangPostRequest(); + assertThrows(SlangBadRequestException.class, () -> postService.createPost(postRequest)); + } + + + @DisplayName("글 업데이트 테스트") + @Transactional + @Test + void updatePost() { // 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); + PostRequest postRequest = PostFixture.getNormalPostRequest(); 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); + + PostRequest updatedPostRequest = PostFixture.getNormalPostRequestUpdated(); // 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) + () -> assertThat(updatedPost.getTitle()).isEqualTo(updatedPostRequest.getTitle()), + () -> assertThat(updatedPost.getContents()).isEqualTo(updatedPostRequest.getContents()), + () -> assertThat(updatedPost.getTag()).isEqualTo(updatedPostRequest.getTag()) ); } } 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..392ac08 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 @@ -8,7 +8,9 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +@ActiveProfiles("dev") @SpringBootTest public class PostValidServiceTest { diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/fixture/PostFixture.java b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/fixture/PostFixture.java new file mode 100644 index 0000000..9ef2611 --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/fixture/PostFixture.java @@ -0,0 +1,71 @@ +package com.cnu.real_coding_server.service.week1.practice.service.fixture; + +import com.cnu.real_coding_server.model.request.PostRequest; +import com.cnu.real_coding_server.model.type.Tag; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class PostFixture { + + static ObjectMapper mapper = new ObjectMapper(); + + public static PostRequest getSlangPostRequest() { + String title = "비속어1"; + String content = "비속어2"; + Tag tag = Tag.SPRINGBOOT; + + try { + return mapper.readValue( + """ + { + "title": "%s", + "contents": "%s", + "tag": "%s" + } + """.formatted(title, content, tag) + , PostRequest.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + public static PostRequest getNormalPostRequest() { + String title = "정상 제목"; + String content = "정상 본문"; + Tag tag = Tag.SPRINGBOOT; + + try { + return mapper.readValue( + """ + { + "title": "%s", + "contents": "%s", + "tag": "%s" + } + """.formatted(title, content, tag) + , PostRequest.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + public static PostRequest getNormalPostRequestUpdated() { + String title = "정상 제목2"; + String content = "정상 본문2"; + Tag tag = Tag.JAVA; + + try { + return mapper.readValue( + """ + { + "title": "%s", + "contents": "%s", + "tag": "%s" + } + """.formatted(title, content, tag) + , PostRequest.class); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } +} From a2f8d11aff67090f48668920131ea2e3e9202d68 Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 19:39:48 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feat(week1-practice2)=20-=20ddl,=20dml=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/generate-ddl.sql | 0 src/test/resources/generate-dml.sql | 1 + 2 files changed, 1 insertion(+) create mode 100644 src/test/resources/generate-ddl.sql create mode 100644 src/test/resources/generate-dml.sql diff --git a/src/test/resources/generate-ddl.sql b/src/test/resources/generate-ddl.sql new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/generate-dml.sql b/src/test/resources/generate-dml.sql new file mode 100644 index 0000000..e7621b4 --- /dev/null +++ b/src/test/resources/generate-dml.sql @@ -0,0 +1 @@ +insert into Posts (title, contents, tag, createdAt, updatedAt) values ('안녕', '하세요', 'JAVA', NOW(), NOW()); \ No newline at end of file From b15e89b5500ace5f005a833e30f20c872a868995 Mon Sep 17 00:00:00 2001 From: inspire12 Date: Sun, 16 Apr 2023 23:20:51 +0900 Subject: [PATCH 06/10] fix(week1-practice2) - bugfix --- .../service/week1/base/service/PostServiceWithSpringTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 index d276b67..b374342 100644 --- 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 @@ -10,7 +10,9 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +@ActiveProfiles("dev") @SpringBootTest public class PostServiceWithSpringTest { @Autowired @@ -21,6 +23,6 @@ public class PostServiceWithSpringTest { void testDoCodeRun() { Post post = postService.createPost(PostFixture.getNormalPostRequest()); Optional optPost = postService.getPost(post.getId()); - assertThat(optPost.isPresent()).isFalse(); + assertThat(optPost.isPresent()).isTrue(); } } From ae8bb6897c9d1e30d9c46e8533f6daa76e53734a Mon Sep 17 00:00:00 2001 From: inspire12 Date: Mon, 24 Apr 2023 02:13:50 +0900 Subject: [PATCH 07/10] =?UTF-8?q?feat(homework/testing)=20-=20=EB=B2=A0?= =?UTF-8?q?=EC=9D=B4=EC=8A=A4=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/deployment.yml | 76 -------------------------------- .github/workflows/run_test.yml | 36 +++++++++++++++ build.gradle | 6 +++ 3 files changed, 42 insertions(+), 76 deletions(-) delete mode 100644 .github/workflows/deployment.yml create mode 100644 .github/workflows/run_test.yml diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml deleted file mode 100644 index 2be1220..0000000 --- a/.github/workflows/deployment.yml +++ /dev/null @@ -1,76 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. -# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle - -name: Deployment - -on: - workflow_dispatch: - push: - branches: [ "main" ] - -permissions: - contents: read - -jobs: - build: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Set up JDK 17 - uses: actions/setup-java@v3 - with: - java-version: '17' - distribution: 'temurin' - - name: Build with Gradle - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 - with: - arguments: build - - uses: actions/upload-artifact@v3 - with: - name: jar - path: build/libs - - send-jar: - needs: build - runs-on: ubuntu-latest - steps: - - name: Download jar - uses: actions/download-artifact@v3 - with: - name: jar - - name: Send jar to remote server - uses: appleboy/scp-action@master - with: - host: {나의 public ip} - username: {나의 userName} - source: "real_coding_server-0.0.1-SNAPSHOT.jar" - target: "/home/{나의 userName}" - key: ${{ secrets.PRIVATE_KEY }} - - run-app: - needs: send-jar - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Move deploy.sh - uses: appleboy/scp-action@master - with: - host: {나의 public ip} - username: {나의 userName} - source: "deploy.sh" - target: "/home/{나의 userName}" - key: ${{ secrets.PRIVATE_KEY }} - - name: Execute script - uses: appleboy/ssh-action@master - with: - username: {나의 userName} - host: {나의 public ip} - key: ${{ secrets.PRIVATE_KEY }} - script_stop: true - script: cd /home/{나의 userName} && chmod +x deploy.sh && ./deploy.sh diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml new file mode 100644 index 0000000..32bd14b --- /dev/null +++ b/.github/workflows/run_test.yml @@ -0,0 +1,36 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: Deployment + +on: + workflow_dispatch: + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + - name: Build with Gradle + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: build + - uses: actions/upload-artifact@v3 + with: + name: jar + path: build/libs diff --git a/build.gradle b/build.gradle index c6bb5f6..95e9721 100644 --- a/build.gradle +++ b/build.gradle @@ -38,3 +38,9 @@ dependencies { tasks.named('test') { useJUnitPlatform() } + +test { + testLogging { + events "PASSED", "SKIPPED", "FAILED" + } +} \ No newline at end of file From c76f7d509ec8bb4f1c26a029bf9a814bce197444 Mon Sep 17 00:00:00 2001 From: inspire12 Date: Mon, 24 Apr 2023 02:23:06 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat(homework/testing)=20-=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8C=85=20=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application-test.yml | 22 +++++++++++++++++++ .../service/PostServiceWithSpringTest.java | 2 +- .../practice/service/PostServiceTest.java | 2 +- .../service/PostValidServiceTest.java | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/application-test.yml diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml new file mode 100644 index 0000000..55127d4 --- /dev/null +++ b/src/main/resources/application-test.yml @@ -0,0 +1,22 @@ +spring: + # H2 Setting Info (H2 Console? ???? ?? ???? ??) + h2: + console: + enabled: true # H2 Console? ???? ?? (H2 Console? H2 Database? UI? ????? ??) + path: /h2-console # H2 Console? Path + # Database Setting Info (Database? H2? ???? ?? H2?? ?? ??) + datasource: + driver-class-name: org.h2.Driver # Database? H2? ?????. + url: jdbc:h2:mem:devblog # H2 ?? ?? + username: sa # H2 ?? ? ??? username ?? (??? ??? ??) + password: # H2 ?? ? ??? password ?? (??? ??? ??) + + jpa: + hibernate: + ddl-auto: create-drop # ??????? ??? ? ??????? ????? ?? ??? ?? + properties: + hibernate: + format_sql: true # ???? query? ??? + +logging.level: + org.hibernate.SQL: debug \ 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 index b374342..1071082 100644 --- 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 @@ -12,7 +12,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -@ActiveProfiles("dev") +@ActiveProfiles("test") @SpringBootTest public class PostServiceWithSpringTest { @Autowired 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 ba101ea..74f0df1 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 @@ -21,7 +21,7 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.transaction.annotation.Transactional; -@ActiveProfiles("dev") +@ActiveProfiles("test") @SpringBootTest public class PostServiceTest { 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 392ac08..7fd8b66 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 @@ -10,7 +10,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; -@ActiveProfiles("dev") +@ActiveProfiles("test") @SpringBootTest public class PostValidServiceTest { From faedb5f03fbcd6043b00b4210763c7d256038fc4 Mon Sep 17 00:00:00 2001 From: inspire12 Date: Mon, 24 Apr 2023 02:27:18 +0900 Subject: [PATCH 09/10] =?UTF-8?q?feat(homework/testing)=20-=20test=20?= =?UTF-8?q?=EC=9A=A9=EC=9C=BC=EB=A1=9C=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 --- .github/workflows/run_test.yml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml index 32bd14b..6af923e 100644 --- a/.github/workflows/run_test.yml +++ b/.github/workflows/run_test.yml @@ -26,11 +26,9 @@ jobs: with: java-version: '17' distribution: 'temurin' - - name: Build with Gradle - uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 - with: - arguments: build - - uses: actions/upload-artifact@v3 - with: - name: jar - path: build/libs + cache: gradle + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Test with Gradle + run: ./gradlew test \ No newline at end of file From 6aaf49e37aa4c09ea1d6f414daa9c2bc5ccb2942 Mon Sep 17 00:00:00 2001 From: TAEHONG-LEE <2t_hong@naver.com> Date: Wed, 10 May 2023 17:03:42 +0900 Subject: [PATCH 10/10] Assignment --- .../practice/service/PostServiceTest.java | 2 + .../service/PostValidServiceTest.java | 42 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) 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 74f0df1..3db0bb4 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 @@ -80,3 +80,5 @@ void updatePost() { ); } } + + 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 7fd8b66..f738550 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 @@ -1,6 +1,7 @@ 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.service.valid.PostValidService; import java.util.List; @@ -30,4 +31,43 @@ void testValidPostIncludeSlang() { // then 검증 assertThat(validPost).isEqualTo(true); } -} + + @DisplayName("post 제목에 비속어가 있나 테스트") + @Test + void testValidPostTitleIncludeSlang() { + // given 시나리오 + String testSlangTitle = "비속어가 섞인 제목"; + String testNoneSlangTitle = "비1속어가 없는 제목"; + String testContent = "비1속어가 없는 내용"; + List slangList = List.of("비속어", "비속어2"); + + boolean validSlangPostTitle = postValidService.isSlangInclude(slangList, testSlangTitle, testContent); + boolean validNoneSlangPostTitle = postValidService.isSlangInclude(slangList, testNoneSlangTitle, testContent); + + + // then 검증 + assertAll("verify object", + () -> assertThat(validSlangPostTitle).isEqualTo(true), + () -> assertThat(validNoneSlangPostTitle).isEqualTo(false) + ); + } + @DisplayName("post 내용에 비속어가 있나 테스트") + @Test + void testValidPostContentIncludeSlang() { + // given 시나리오 + String testNoneSlangTitle = "비1속어가 없는 제목"; + String testSlangContent = "비속어가 있는 내용"; + String testNoneSlangContent = "비1속어가 없는 내용"; + List slangList = List.of("비속어", "비속어2"); + + boolean validSlangPostContent = postValidService.isSlangInclude(slangList, testNoneSlangTitle, testSlangContent); + boolean validNoneSlangPostContent = postValidService.isSlangInclude(slangList, testNoneSlangTitle, testNoneSlangContent); + + + // then 검증 + assertAll("verify object", + () -> assertThat(validSlangPostContent).isEqualTo(true), + () -> assertThat(validNoneSlangPostContent).isEqualTo(false) + ); + } +} \ No newline at end of file