diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..4594ea3 Binary files /dev/null and b/.DS_Store differ 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..e1bc687 --- /dev/null +++ b/.github/workflows/run_test.yml @@ -0,0 +1,34 @@ +# 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' + 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 diff --git a/build.gradle b/build.gradle index 4619d59..8743ab4 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,8 @@ 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' + 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' @@ -36,3 +38,8 @@ dependencies { tasks.named('test') { useJUnitPlatform() } +test { + testLogging { + events "PASSED", "SKIPPED", "FAILED" + } +} \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..7b67fa9 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000..a91efb9 Binary files /dev/null and b/src/main/.DS_Store differ 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..2c5dc4f --- /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; +} \ No newline at end of file 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..fedd763 --- /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()); + } +} \ No newline at end of file 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..1a60b8a --- /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()); + } +} \ No newline at end of file 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..7980144 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,33 +1,45 @@ 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 lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +@Slf4j @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(); + } + log.info("정상 저장 확인"); return postRepository.save(postRequest.toEntity()); } public List getPosts() { return postRepository.findAll(); } - public Optional getPost(Integer postId) { return postRepository.findById(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()); @@ -36,9 +48,8 @@ public Optional updatePost(Integer postId, PostRequest postRequest) { return postRepository.save(post); }); } - public void deletePost(Integer postId) { postRepository.findById(postId) .ifPresent(postRepository::delete); } -} +} \ No newline at end of file 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..8f75fe5 --- /dev/null +++ b/src/main/java/com/cnu/real_coding_server/service/valid/PostValidService.java @@ -0,0 +1,19 @@ +package com.cnu.real_coding_server.service.valid; + +import java.util.List; +import org.springframework.stereotype.Service; + +@Service +public class PostValidService { + public boolean isSlangInclude(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/main/resources/.DS_Store b/src/main/resources/.DS_Store new file mode 100644 index 0000000..ffb1cab Binary files /dev/null and b/src/main/resources/.DS_Store differ 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-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/main/resources/application.yml b/src/main/resources/application.yml index 9f1e564..13c3efe 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -13,10 +13,11 @@ 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/.DS_Store b/src/test/.DS_Store new file mode 100644 index 0000000..20ba85a Binary files /dev/null and b/src/test/.DS_Store differ diff --git a/src/test/java/.DS_Store b/src/test/java/.DS_Store new file mode 100644 index 0000000..4e1452c Binary files /dev/null and b/src/test/java/.DS_Store differ diff --git a/src/test/java/com/.DS_Store b/src/test/java/com/.DS_Store new file mode 100644 index 0000000..18638a8 Binary files /dev/null and b/src/test/java/com/.DS_Store differ diff --git a/src/test/java/com/cnu/.DS_Store b/src/test/java/com/cnu/.DS_Store new file mode 100644 index 0000000..7996fc2 Binary files /dev/null and b/src/test/java/com/cnu/.DS_Store differ diff --git a/src/test/java/com/cnu/real_coding_server/.DS_Store b/src/test/java/com/cnu/real_coding_server/.DS_Store new file mode 100644 index 0000000..3ffe3b3 Binary files /dev/null and b/src/test/java/com/cnu/real_coding_server/.DS_Store differ diff --git a/src/test/java/com/cnu/real_coding_server/service/.DS_Store b/src/test/java/com/cnu/real_coding_server/service/.DS_Store new file mode 100644 index 0000000..3b80fae Binary files /dev/null and b/src/test/java/com/cnu/real_coding_server/service/.DS_Store differ diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/.DS_Store b/src/test/java/com/cnu/real_coding_server/service/week1/.DS_Store new file mode 100644 index 0000000..42dc8ee Binary files /dev/null and b/src/test/java/com/cnu/real_coding_server/service/week1/.DS_Store differ diff --git a/src/test/java/com/cnu/real_coding_server/service/week1/practice/.DS_Store b/src/test/java/com/cnu/real_coding_server/service/week1/practice/.DS_Store new file mode 100644 index 0000000..aafaabb Binary files /dev/null and b/src/test/java/com/cnu/real_coding_server/service/week1/practice/.DS_Store differ 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..9351aef --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostServiceTest.java @@ -0,0 +1,82 @@ +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 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; +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.test.context.ActiveProfiles; +import org.springframework.transaction.annotation.Transactional; + +@ActiveProfiles("test") +@SpringBootTest +public class PostServiceTest { + + @Autowired + PostService postService; + + @Autowired + ObjectMapper mapper; + + @BeforeEach + void init() { + + } + + @DisplayName("글 저장 테스트") +// @Transactional + @Test + void createPost() throws JsonProcessingException { + PostRequest postRequest = PostFixture.getNormalPostRequest(); + + Post post = postService.createPost(postRequest); + assertAll("verify object", + () -> assertThat(post.getTitle()).isEqualTo(postRequest.getTitle()), + () -> assertThat(post.getContents()).isEqualTo(postRequest.getContents()), + () -> assertThat(post.getTag()).isEqualTo(postRequest.getTag()) + ); + } + + @DisplayName("비속어 글 저장 테스트") + @Transactional + @Test + void createPostWithSlang() { + PostRequest postRequest = PostFixture.getSlangPostRequest(); + assertThrows(SlangBadRequestException.class, () -> postService.createPost(postRequest)); + } + + + @DisplayName("글 업데이트 테스트") + @Transactional + @Test + void updatePost() { + // given + PostRequest postRequest = PostFixture.getNormalPostRequest(); + Post post = postService.createPost(postRequest); + + PostRequest updatedPostRequest = PostFixture.getNormalPostRequestUpdated(); + // when + Optional optPost = postService.updatePost(post.getId(), updatedPostRequest); + + // then + Post updatedPost = optPost.get(); + assertAll("verify object", + () -> assertThat(updatedPost.getTitle()).isEqualTo(updatedPostRequest.getTitle()), + () -> assertThat(updatedPost.getContents()).isEqualTo(updatedPostRequest.getContents()), + () -> assertThat(updatedPost.getTag()).isEqualTo(updatedPostRequest.getTag()) + ); + } +} \ No newline at end of file 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 new file mode 100644 index 0000000..56f9d56 --- /dev/null +++ b/src/test/java/com/cnu/real_coding_server/service/week1/practice/service/PostValidServiceTest.java @@ -0,0 +1,45 @@ +package com.cnu.real_coding_server.service.week1.practice.service; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.cnu.real_coding_server.service.valid.PostValidService; +import java.util.List; +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.test.context.ActiveProfiles; + +@ActiveProfiles("test") +@SpringBootTest +public class PostValidServiceTest { + + @Autowired + PostValidService postValidService; + + + @DisplayName("post 제목에 비속어가 있나 테스트") + @Test + void testValidPostIncludeSlang_title() { + // given 시나리오 + String testTitle = "제목"; + String testContent = "비속어"; + List slangList = List.of("비속어", "비속어2"); + + boolean validPost = postValidService.isSlangInclude(slangList, testTitle, testContent); + // then 검증 + assertThat(validPost).isEqualTo(true); + } + @DisplayName("post 본문에 비속어가 있나 테스트") + @Test + void testValidPostIncludeSlang_content() { + // given 시나리오 + String testTitle = "본문"; + String testContent = "비속어"; + List slangList = List.of("비속어", "비속어2"); + + boolean validPost = postValidService.isSlangInclude(slangList, testTitle, testContent); + // then 검증 + assertThat(validPost).isEqualTo(true); + } +} \ No newline at end of file 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..d53004b --- /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); + } + } +} \ No newline at end of file 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