From 7b4a2a294a0d2aa9dcea62cf6e133c51eb323bf4 Mon Sep 17 00:00:00 2001 From: taem1n1 Date: Tue, 9 Aug 2022 08:07:31 +0900 Subject: [PATCH 1/3] =?UTF-8?q?AnswerService=20=EB=B6=80=EB=B6=84=20test?= =?UTF-8?q?=20coverage=20100%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/docs/answer.html | 25 +++- .../answer/service/AnswerServiceTest.java | 109 ++++++++++++++++++ 2 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/server/amething/domain/answer/service/AnswerServiceTest.java diff --git a/src/main/resources/static/docs/answer.html b/src/main/resources/static/docs/answer.html index 4e37901..8eb3c74 100644 --- a/src/main/resources/static/docs/answer.html +++ b/src/main/resources/static/docs/answer.html @@ -456,7 +456,7 @@

HTTP Request
POST /v1/1/answer HTTP/1.1
 Content-Type: application/json;charset=UTF-8
-Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjQ5MDQ5OTE3IiwiYXV0aCI6WyJST0xFX01FTUJFUiJdLCJpYXQiOjE2NTgxODc3NDcsImV4cCI6MTY1ODE5ODU0N30.JqHqsLVm5CrgRWoPDiniJ3SkJAxLpTTGE8uyBtMLCCnJZZL_aA5We0aE8ybtkqrcdHhv6Z-pK5woJAxdW5PBNQ
+Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjQ5MDQ5OTE3IiwiYXV0aCI6WyJST0xFX01FTUJFUiJdLCJpYXQiOjE2NTk5OTk5OTIsImV4cCI6MTY2MDAxMDc5Mn0.9bAgBlFd4IFWsfEFTKTs-Cx_-Tduw4NMJdoTCjLBkLxyDReQSkZENk4jca0Jq1fhqcUkgge80lk83eD8ihP3VQ
 Content-Length: 30
 Host: localhost:8080
 
@@ -487,6 +487,27 @@ 

Path Parameter<
+

Request Header

+ ++++ + + + + + + + + + + + + +
NameDescription

Authorization

Bearer 토큰 타입의 AccessToken

+
+

Request Body

@@ -512,7 +533,7 @@

HTTP Response

diff --git a/src/test/java/com/server/amething/domain/answer/service/AnswerServiceTest.java b/src/test/java/com/server/amething/domain/answer/service/AnswerServiceTest.java new file mode 100644 index 0000000..fd33f8e --- /dev/null +++ b/src/test/java/com/server/amething/domain/answer/service/AnswerServiceTest.java @@ -0,0 +1,109 @@ +package com.server.amething.domain.answer.service; + +import com.server.amething.domain.answer.dto.RegistrationAnswerDto; +import com.server.amething.domain.answer.repository.AnswerRepository; +import com.server.amething.domain.question.Question; +import com.server.amething.domain.question.enum_type.QuestionType; +import com.server.amething.domain.question.repository.QuestionRepository; +import com.server.amething.domain.user.User; +import com.server.amething.domain.user.enum_type.Role; +import com.server.amething.domain.user.repository.UserRepository; +import com.server.amething.global.util.UserUtil; +import org.assertj.core.api.Assertions; +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.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@Transactional +public class AnswerServiceTest { + + @Autowired QuestionRepository questionRepository; + @Autowired AnswerRepository answerRepository; + @Autowired UserRepository userRepository; + @Autowired AnswerService answerService; + @Autowired UserUtil userUtil; + + @BeforeEach + void init() { + User taemin = User.builder() + .nickname("김태민") + .oauthId(1111L) + .roles(Collections.singletonList(Role.ROLE_MEMBER)) + .build(); + + userRepository.save(taemin); + + UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken( + taemin.getId(), + "", + taemin.getRoles()); + + SecurityContext securityContext = SecurityContextHolder.getContext(); + securityContext.setAuthentication(token); + } + + @Test + @DisplayName("답변이 잘 등록 되나요?") + void registrationAnswerTest() { + User currentUser = userUtil.getCurrentUser(); + + Question question = questionRepository.save(Question.builder() + .id(1L) + .title("title") + .type(QuestionType.UNREPLY) + .user(currentUser) + .build()); + + RegistrationAnswerDto answerDto = new RegistrationAnswerDto("답변입니다."); + answerService.registrationAnswer(question.getId(), answerDto); + + Assertions.assertThat(question.getType()).isEqualTo(QuestionType.PIN); + } + + @Test + @DisplayName("등록이 안된 질문을 불러올 시 예외가 잘 반환되나요?") + void notApplyQuestionExceptionTest() { + RegistrationAnswerDto answerDto = new RegistrationAnswerDto("답변입니다."); + + assertThrows( + IllegalArgumentException.class, + () -> answerService.registrationAnswer(1L, answerDto)); + } + + @Test + @DisplayName("자신에게 달린 질문이 아닌 질문에 답변을 할 시 예외가 잘 반환되나요?") + void notMineQuestionExceptionTest() { + User user = userRepository.save(User.builder() + .nickname("다른 사용자") + .oauthId(1234L) + .roles(Collections.singletonList(Role.ROLE_MEMBER)) + .build()); + + Question question = questionRepository.save(Question.builder() + .id(1L) + .title("title") + .type(QuestionType.UNREPLY) + .user(user) + .build()); + + RegistrationAnswerDto answerDto = new RegistrationAnswerDto("답변입니다."); + + assertThrows( + IllegalArgumentException.class, + () -> answerService.registrationAnswer(question.getId(), answerDto) + ); + } +} From 18a72a6e9092c179a0a444ba0827b6d26a9fc80e Mon Sep 17 00:00:00 2001 From: taem1n1 Date: Tue, 9 Aug 2022 08:19:49 +0900 Subject: [PATCH 2/3] spring-rest-docs gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index bf724ef..b50445c 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,7 @@ out/ .vscode/ ### Sensitive Information ### -/src/main/resources/application.yml \ No newline at end of file +/src/main/resources/application.yml + +### Spring Rest Docs +/src/main/resources/static/docs/*.html \ No newline at end of file From 00b861b296700e74301f11ecd96308b554cdcbd0 Mon Sep 17 00:00:00 2001 From: taem1n1 Date: Tue, 9 Aug 2022 08:20:51 +0900 Subject: [PATCH 3/3] fixed untracked files --- gradlew.bat | 178 +++---- src/main/resources/static/docs/answer.html | 540 --------------------- 2 files changed, 89 insertions(+), 629 deletions(-) delete mode 100644 src/main/resources/static/docs/answer.html diff --git a/gradlew.bat b/gradlew.bat index ac1b06f..107acd3 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,89 +1,89 @@ -@rem -@rem Copyright 2015 the original author or authors. -@rem -@rem Licensed under the Apache License, Version 2.0 (the "License"); -@rem you may not use this file except in compliance with the License. -@rem You may obtain a copy of the License at -@rem -@rem https://www.apache.org/licenses/LICENSE-2.0 -@rem -@rem Unless required by applicable law or agreed to in writing, software -@rem distributed under the License is distributed on an "AS IS" BASIS, -@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -@rem See the License for the specific language governing permissions and -@rem limitations under the License. -@rem - -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Resolve any "." and ".." in APP_HOME to make it shorter. -for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto execute - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto execute - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto execute + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/resources/static/docs/answer.html b/src/main/resources/static/docs/answer.html deleted file mode 100644 index 8eb3c74..0000000 --- a/src/main/resources/static/docs/answer.html +++ /dev/null @@ -1,540 +0,0 @@ - - - - - - - -Spring REST Docs - - - - - -
-
-

답변

-
-
-

답변 등록

-
-

HTTP Request

-
-
-
POST /v1/1/answer HTTP/1.1
-Content-Type: application/json;charset=UTF-8
-Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyMjQ5MDQ5OTE3IiwiYXV0aCI6WyJST0xFX01FTUJFUiJdLCJpYXQiOjE2NTk5OTk5OTIsImV4cCI6MTY2MDAxMDc5Mn0.9bAgBlFd4IFWsfEFTKTs-Cx_-Tduw4NMJdoTCjLBkLxyDReQSkZENk4jca0Jq1fhqcUkgge80lk83eD8ihP3VQ
-Content-Length: 30
-Host: localhost:8080
-
-{"description": "description"}
-
-
-
-
-

Path Parameter

- - ---- - - - - - - - - - - - - -
Table 1. /v1/{questionId}/answer
ParameterDescription

questionId

Question Id(PK)

-
-
-

Request Header

- ---- - - - - - - - - - - - - -
NameDescription

Authorization

Bearer 토큰 타입의 AccessToken

-
-
-

Request Body

-
-
-
{"description": "description"}
-
-
-
-
-

HTTP Response

-
-
-
HTTP/1.1 201 Created
-Content-Type: application/json
-Content-Length: 60
-
-{"success":true,"code":1,"message":"성공하였습니다."}
-
-
-
-
-
-
-
- - - \ No newline at end of file