diff --git a/.github/workflows/actions.yml b/.github/workflows/actions.yml index 4a62845..751b3a0 100644 --- a/.github/workflows/actions.yml +++ b/.github/workflows/actions.yml @@ -3,11 +3,11 @@ name: spurt CICD on: push: branches: - - master + - main jobs: push_to_registry: - name: Push to ncp container registry + name: Push to aws container registry runs-on: ubuntu-latest steps: - name: Checkout @@ -19,14 +19,22 @@ jobs: java-version: '17' distribution: 'corretto' + - name: make application.yml + if: contains(github.ref, 'develop') || contains(github.ref, 'main') + run: | + cd ./src/main/resources + touch ./application.yml + echo "${{ secrets.YML_PROD }}" > ./application.yml + shell: bash + - name: Grant execute permission for gradlew run: chmod +x gradlew - name: Setup Gradle uses: gradle/gradle-build-action@v2 with: - arguments: build -x spotlessJavaCheck - cache-read-only: ${{ github.ref != 'refs/heads/master' }} + arguments: build -x spotlessJavaCheck + cache-read-only: ${{ github.ref != 'refs/heads/master' }} - name: save docs file uses: actions/upload-artifact@v3 @@ -37,59 +45,60 @@ jobs: - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Login to NCP Container Registry - uses: docker/login-action@v2 - with: - registry: ${{ secrets.NCP_CONTAINER_REGISTRY }} - username: ${{ secrets.NCP_ACCESS_KEY }} - password: ${{ secrets.NCP_SECRET_KEY }} + - name: Docker build & push to prod + if: contains(github.ref, 'develop') || contains(github.ref, 'main') + run: | + docker login -u ${{ secrets.DOCKER_USER }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -t spurt-be -f Dockerfile . + docker tag spurt-be:latest ${{ secrets.DOCKER_USER }}/spurt-be:latest + docker push ${{ secrets.DOCKER_USER }}/spurt-be:latest - - name: docker build and push - uses: docker/build-push-action@v3 - with: - context: . - file: ./Dockerfile - push: true - tags: ${{ secrets.NCP_CONTAINER_REGISTRY }}/spurt-backend:latest - cache-from: type=registry,ref=${{ secrets.NCP_CONTAINER_REGISTRY }}/spurt-backend:latest - cache-to: type=inline - secrets: | - GIT_AUTH_TOKEN=${{ secrets.GIT_TOKEN }} pull_from_registry: name: Connect server ssh and pull from container registry needs: push_to_registry runs-on: ubuntu-latest steps: - - name: connect ssh + - name: Deploy to prod + if: contains(github.ref, 'develop') || contains(github.ref, 'main') uses: appleboy/ssh-action@master with: - host: ${{ secrets.DEV_HOST }} - username: ${{ secrets.DEV_USERNAME }} - password: ${{ secrets.DEV_PASSWORD }} - port: ${{ secrets.DEV_PORT }} + host: ${{ secrets.HOST_NAME }} + username: ${{ secrets.AWS_USER_NAME }} + key: ${{ secrets.AWS_PRIVATE_KEY }} + port: ${{ secrets.AWS_PORT }} script: | - docker login ${{ secrets.NCP_CONTAINER_REGISTRY }} -u ${{ secrets.NCP_ACCESS_KEY }} -p ${{ secrets.NCP_SECRET_KEY }} - docker pull ${{ secrets.NCP_CONTAINER_REGISTRY }}/spurt-backend:latest - docker stop $(docker ps -a -q) - docker rm $(docker ps -a -q) - docker run -d -p 8080:8080 ${{ secrets.NCP_CONTAINER_REGISTRY }}/spurt-backend:latest - docker image prune -f + docker pull ${{ secrets.DOCKER_USER }}/spurt-be:latest + docker stop spurt-be + docker rm spurt-be + docker run -d --network spurt --name spurt-be -p 8080:8080 ${{ secrets.DOCKER_USER }}/spurt-be + if docker images -f "dangling=true" -q | grep . > /dev/null; then + docker rmi $(docker images -f "dangling=true" -q) + fi upload_docs: name: upload api docs - needs: pull_from_registry + needs: push_to_registry runs-on: ubuntu-latest + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }} + AWS_REGION: ap-northeast-2 + steps: - name: Download Artifact uses: actions/download-artifact@v3 with: - name: api-docs # Artifact name - path: artifact - - name: Upload Files to S3 + name: api-docs + + - name: Upload binary to S3 bucket + uses: jakejarvis/s3-sync-action@master env: - AWS_ACCESS_KEY_ID: ${{ secrets.NCP_ACCESS_KEY }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.NCP_SECRET_KEY }} - AWS_DEFAULT_REGION: us-east-1 - run: | - aws --endpoint-url=https://kr.object.ncloudstorage.com s3 cp artifact/index.html s3://spurt-backend-docs - aws --endpoint-url=https://kr.object.ncloudstorage.com/ s3 ls s3://spurt-backend-docs --recursive | awk '{cmd="aws --endpoint-url=https://kr.object.ncloudstorage.com/ s3api put-object-acl --bucket spurt-backend-docs --acl public-read --key "$4; system(cmd)}' \ No newline at end of file + AWS_S3_BUCKET: ${{ secrets.AWS_S3_BUCKET_NAME }} + SOURCE_DIR: . + + - name: Invalidate cache CloudFront + uses: chetan/invalidate-cloudfront-action@master + env: + DISTRIBUTION: ${{ secrets.AWS_CLOUDFRONT_DISTRIBUTION_ID }} + PATHS: '/*' + continue-on-error: true diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..ea6dff3 --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,37 @@ +name: spurt build check + +on: + pull_request: + branches: + - main + +jobs: + push_to_registry: + name: Push to aws container registry + 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: 'corretto' + + - name: make application.yml + if: github.event.pull_request.base.ref == 'main' + run: | + cd ./src/main/resources + touch ./application.yml + echo "${{ secrets.YML_PROD }}" > ./application.yml + shell: bash + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + with: + arguments: build + cache-read-only: ${{ github.ref != 'refs/heads/master' }} diff --git a/.gitignore b/.gitignore index 38aefa2..51aaa74 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,5 @@ out/ ### VS Code ### .vscode/ + +application.yml diff --git a/build.gradle b/build.gradle index 73eaa7a..d2c9b4d 100644 --- a/build.gradle +++ b/build.gradle @@ -54,6 +54,8 @@ dependencies { annotationProcessor "jakarta.annotation:jakarta.annotation-api" annotationProcessor "jakarta.persistence:jakarta.persistence-api" + testImplementation 'com.h2database:h2:2.1.214' + compileOnly('com.h2database:h2:2.1.214') } // === Querydsl 빌드 옵션 === @@ -77,6 +79,12 @@ task jsonDoclet(type: Javadoc, dependsOn: compileTestJava) { spotless { java { + // generated 폴더는 제외 + target project.fileTree(project.rootDir) { + include '**/*.java' + exclude '**/generated/**/*.*' + } + // 사용하지 않는 import 제거 removeUnusedImports() // 구글 자바 포맷 적용 @@ -91,7 +99,8 @@ spotless { } // 의존성을 설정해줌 test 끝나고 spotlessjava 수행되도록 -tasks.named("spotlessJava").configure { dependsOn("compileTestJava") } +// tasks.named("spotlessJava").configure { dependsOn("compileTestJava") } +tasks.named("spotlessJava").configure {dependsOn("asciidoctor")} // 테스트 실행 후, snipperDir에 snippet(.adoc 파일들) 생성 test { diff --git a/src/docs/asciidoc/index.adoc b/src/docs/asciidoc/index.adoc index e28ea7d..6d3204f 100644 --- a/src/docs/asciidoc/index.adoc +++ b/src/docs/asciidoc/index.adoc @@ -10,7 +10,7 @@ endif::[] :toclevels: 2 :sectlinks: -넥스터즈 시리우스 팀 api 명세서 +넥스터즈 게꽁지 팀 api 명세서 [[Spurt-API]] @@ -21,7 +21,7 @@ endif::[] |BASE URL |DEV -|https://sirius-spurt.duckdns.org +|https://api.spurtapp.com |=== == Header 정보 @@ -47,7 +47,7 @@ endif::[] |정상 처리 되었습니다 |1000 -|알 수 없는 애러가 발생했습니다. +|알 수 없는 에러가 발생했습니다. |2000 |인증에 실패했습니다. @@ -63,6 +63,22 @@ endif::[] |4000 |시간 형식이 맞지 않습니다. + +|5000 +|ALL category는 저장할 수 없습니다. + +|5001 +|category는 최소 1개 이상 입력하셔야 합니다. + +|6000 +|조회 결과가 없습니다. + +|7000 +|질문은 3초에 1번만 작성이 가능합니다. + +|7001 +|경험은 3초에 1번만 작성이 가능합니다. + |=== == 현재 오류 사항 @@ -84,12 +100,14 @@ include::{snippets}/category-controller-test/카테고리_전체_조회_테스 == 직군 API include::{snippets}/job-group-controller-test/유저_직군_저장/auto-section.adoc[] +include::{snippets}/job-group-controller-test/유저_직군_수정/auto-section.adoc[] == 유저 API include::{snippets}/user-controller-test/유저_존재_확인/auto-section.adoc[] include::{snippets}/user-controller-test/유저_최초_질문_답변_작성_확인/auto-section.adoc[] include::{snippets}/user-controller-test/유저_최초_핀고정_확인/auto-section.adoc[] include::{snippets}/user-controller-test/유저_정보_조회/auto-section.adoc[] +include::{snippets}/user-controller-test/유저_삭제_확인/auto-section.adoc[] == 프로젝트 API include::{snippets}/experience-controller-test/본인_경험_저장/auto-section.adoc[] diff --git a/src/main/java/com/sirius/spurt/common/config/RestTemplateConfig.java b/src/main/java/com/sirius/spurt/common/config/RestTemplateConfig.java new file mode 100644 index 0000000..b634abe --- /dev/null +++ b/src/main/java/com/sirius/spurt/common/config/RestTemplateConfig.java @@ -0,0 +1,14 @@ +package com.sirius.spurt.common.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class RestTemplateConfig { + + @Bean + public RestTemplate restTemplate() { + return new RestTemplate(); + } +} diff --git a/src/main/java/com/sirius/spurt/common/meta/ResultCode.java b/src/main/java/com/sirius/spurt/common/meta/ResultCode.java index f60ca6c..9599693 100644 --- a/src/main/java/com/sirius/spurt/common/meta/ResultCode.java +++ b/src/main/java/com/sirius/spurt/common/meta/ResultCode.java @@ -2,14 +2,17 @@ public enum ResultCode { SUCCESS(0, "정상 처리 되었습니다"), - SYSTEM_ERROR(1000, "알 수 없는 애러가 발생했습니다."), + SYSTEM_ERROR(1000, "알 수 없는 에러가 발생했습니다."), AUTHENTICATION_FAILED(2000, "인증에 실패했습니다."), NOT_QUESTION_OWNER(3000, "질문이 존재하지 않거나 작성자가 아닙니다."), NOT_EXIST_USER(3001, "존재하지 않는 유저입니다."), NOT_EXPERIENCE_OWNER(3002, "경험이 존재하지 않거나 작성자가 아닙니다."), TIME_FORMAT_ERROR(4000, "시간 형식이 맞지 않습니다."), NOT_ALL_CATEGORY(5000, "ALL category는 저장할 수 없습니다."), - NO_CONTENT(6000, "조회 결과가 없습니다."); + MISSING_CATEGORY(5001, "category는 최소 1개 이상 입력하셔야 합니다."), + NO_CONTENT(6000, "조회 결과가 없습니다."), + QUESTION_THREE_SECONDS(7000, "질문은 3초에 1번만 작성이 가능합니다."), + EXPERIENCE_THREE_SECONDS(7001, "경험은 3초에 1번만 작성이 가능합니다."); private Integer code; private String message; diff --git a/src/main/java/com/sirius/spurt/common/resolver/LoginUserResolver.java b/src/main/java/com/sirius/spurt/common/resolver/LoginUserResolver.java index 2478a78..3f8f0de 100644 --- a/src/main/java/com/sirius/spurt/common/resolver/LoginUserResolver.java +++ b/src/main/java/com/sirius/spurt/common/resolver/LoginUserResolver.java @@ -1,14 +1,12 @@ package com.sirius.spurt.common.resolver; import com.sirius.spurt.common.exception.GlobalException; -import com.sirius.spurt.common.meta.JobGroup; import com.sirius.spurt.common.meta.ResultCode; import com.sirius.spurt.common.resolver.user.LoginUser; import com.sirius.spurt.store.provider.auth.AuthProvider; import com.sirius.spurt.store.provider.auth.vo.AuthVo; import com.sirius.spurt.store.provider.jobgroup.JobGroupProvider; import com.sirius.spurt.store.provider.user.UserProvider; -import com.sirius.spurt.store.provider.user.vo.UserVo; import jakarta.servlet.http.HttpServletRequest; import lombok.RequiredArgsConstructor; import org.springframework.core.MethodParameter; @@ -54,11 +52,11 @@ public Object resolveArgument( String accessToken = accessHeader.replace(TOKEN_TYPE, ""); userInfo = authProvider.getUserId(accessToken); // 로그인 이후 직군 미선택 유저 추가 체크 -// UserVo userVo = userProvider.getUserInfo(userInfo.getUserId()); -// if (userVo == null) { -// jobGroupProvider.saveJobGroup( -// userInfo.getUserId(), userInfo.getEmail(), JobGroup.DEVELOPER); -// } + // UserVo userVo = userProvider.getUserInfo(userInfo.getUserId()); + // if (userVo == null) { + // jobGroupProvider.saveJobGroup( + // userInfo.getUserId(), userInfo.getEmail(), JobGroup.DEVELOPER); + // } } return new LoginUser(userInfo.getUserId(), userInfo.getEmail()); } diff --git a/src/main/java/com/sirius/spurt/common/validator/ExperienceValidator.java b/src/main/java/com/sirius/spurt/common/validator/ExperienceValidator.java new file mode 100644 index 0000000..593477c --- /dev/null +++ b/src/main/java/com/sirius/spurt/common/validator/ExperienceValidator.java @@ -0,0 +1,52 @@ +package com.sirius.spurt.common.validator; + +import static com.sirius.spurt.common.meta.ResultCode.EXPERIENCE_THREE_SECONDS; +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXPERIENCE_OWNER; +import static com.sirius.spurt.common.meta.ResultCode.NO_CONTENT; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.repository.database.entity.ExperienceEntity; +import java.sql.Timestamp; +import java.util.List; +import org.springframework.util.CollectionUtils; + +public class ExperienceValidator { + private static long EXPERIENCE_DUPLICATE_TIME = 3000L; + + public static void validateNoContent(ExperienceEntity experienceEntity) { + if (!isExistExperience(experienceEntity)) { + throw new GlobalException(NO_CONTENT); + } + } + + public static void validateNoContents(List experienceEntities) { + if (CollectionUtils.isEmpty(experienceEntities)) { + throw new GlobalException(NO_CONTENT); + } + } + + public static void validate(ExperienceEntity experienceEntity) { + if (!isExistExperience(experienceEntity)) { + throw new GlobalException(NOT_EXPERIENCE_OWNER); + } + } + + public static void validateTimestamp(ExperienceEntity experienceEntity) { + if (!isExistExperience(experienceEntity)) { + return; + } + + if (isWithin3SecondsDifference(experienceEntity.getCreateTimestamp())) { + throw new GlobalException(EXPERIENCE_THREE_SECONDS); + } + } + + private static boolean isExistExperience(ExperienceEntity experienceEntity) { + return experienceEntity != null; + } + + private static boolean isWithin3SecondsDifference(Timestamp timestamp) { + return new Timestamp(System.currentTimeMillis()).getTime() - timestamp.getTime() + < EXPERIENCE_DUPLICATE_TIME; + } +} diff --git a/src/main/java/com/sirius/spurt/common/validator/QuestionValidator.java b/src/main/java/com/sirius/spurt/common/validator/QuestionValidator.java new file mode 100644 index 0000000..69a0ad4 --- /dev/null +++ b/src/main/java/com/sirius/spurt/common/validator/QuestionValidator.java @@ -0,0 +1,72 @@ +package com.sirius.spurt.common.validator; + +import static com.sirius.spurt.common.meta.ResultCode.MISSING_CATEGORY; +import static com.sirius.spurt.common.meta.ResultCode.NOT_ALL_CATEGORY; +import static com.sirius.spurt.common.meta.ResultCode.NOT_QUESTION_OWNER; +import static com.sirius.spurt.common.meta.ResultCode.QUESTION_THREE_SECONDS; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.common.meta.Category; +import com.sirius.spurt.service.business.question.PutQuestionBusiness; +import com.sirius.spurt.service.business.question.SaveQuestionBusiness; +import com.sirius.spurt.store.repository.database.entity.QuestionEntity; +import java.sql.Timestamp; +import java.util.List; +import org.springframework.util.CollectionUtils; + +public class QuestionValidator { + private static long BOARD_DUPLICATE_TIME = 3000L; + + public static void validate(QuestionEntity questionEntity) { + if (!isExistQuestion(questionEntity)) { + throw new GlobalException(NOT_QUESTION_OWNER); + } + } + + public static void validateTimestamp(QuestionEntity questionEntity) { + if (!isExistQuestion(questionEntity)) { + return; + } + + if (isWithin3SecondsDifference(questionEntity.getCreateTimestamp())) { + throw new GlobalException(QUESTION_THREE_SECONDS); + } + } + + public static void validate(SaveQuestionBusiness.Dto dto) { + if (isEmptyCategory(dto.getCategoryList())) { + throw new GlobalException(MISSING_CATEGORY); + } + + if (isContainsAllCategory(dto.getCategoryList())) { + throw new GlobalException(NOT_ALL_CATEGORY); + } + } + + public static void validate(PutQuestionBusiness.Dto dto) { + if (isEmptyCategory(dto.getCategoryList())) { + throw new GlobalException(MISSING_CATEGORY); + } + + if (isContainsAllCategory(dto.getCategoryList())) { + throw new GlobalException(NOT_ALL_CATEGORY); + } + } + + private static boolean isExistQuestion(QuestionEntity questionEntity) { + return questionEntity != null; + } + + private static boolean isWithin3SecondsDifference(Timestamp timestamp) { + return new Timestamp(System.currentTimeMillis()).getTime() - timestamp.getTime() + < BOARD_DUPLICATE_TIME; + } + + private static boolean isContainsAllCategory(List categoryList) { + return categoryList.contains(Category.ALL); + } + + private static boolean isEmptyCategory(List categoryList) { + return CollectionUtils.isEmpty(categoryList); + } +} diff --git a/src/main/java/com/sirius/spurt/common/validator/UserValidator.java b/src/main/java/com/sirius/spurt/common/validator/UserValidator.java new file mode 100644 index 0000000..4812e82 --- /dev/null +++ b/src/main/java/com/sirius/spurt/common/validator/UserValidator.java @@ -0,0 +1,18 @@ +package com.sirius.spurt.common.validator; + +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.repository.database.entity.UserEntity; + +public class UserValidator { + public static void validator(UserEntity userEntity) { + if (!isExistUser(userEntity)) { + throw new GlobalException(NOT_EXIST_USER); + } + } + + private static boolean isExistUser(UserEntity userEntity) { + return userEntity != null; + } +} diff --git a/src/main/java/com/sirius/spurt/service/business/category/GetAllCategoryBusiness.java b/src/main/java/com/sirius/spurt/service/business/category/GetAllCategoryBusiness.java index 1323850..2d300f1 100644 --- a/src/main/java/com/sirius/spurt/service/business/category/GetAllCategoryBusiness.java +++ b/src/main/java/com/sirius/spurt/service/business/category/GetAllCategoryBusiness.java @@ -34,15 +34,15 @@ public static class Dto implements Business.Dto, Serializable {} @JsonIgnoreProperties @Builder public static class Result implements Business.Result, Serializable { - List categoryList; + private List categoryList; @Data @JsonIgnoreProperties @Builder @AllArgsConstructor - static class ResultCategory { - String value; - String description; + public static class ResultCategory { + private String value; + private String description; } } diff --git a/src/main/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusiness.java b/src/main/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusiness.java index 0bf6021..c39c363 100644 --- a/src/main/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusiness.java +++ b/src/main/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusiness.java @@ -24,7 +24,7 @@ public class SaveJobGroupBusiness implements Business { @Override public Result execute(Dto input) { jobGroupProvider.saveJobGroup( - input.loginUser.getUserId(), input.loginUser.getEmail(), input.getJobGroup()); + input.getLoginUser().getUserId(), input.getLoginUser().getEmail(), input.getJobGroup()); return new SaveJobGroupBusiness.Result(); } diff --git a/src/main/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusiness.java b/src/main/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusiness.java index ff329da..22d4386 100644 --- a/src/main/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusiness.java +++ b/src/main/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusiness.java @@ -24,7 +24,7 @@ public class UpdateJobGroupBusiness implements Business { @Override public Result execute(Dto input) { jobGroupProvider.updateJobGroup( - input.loginUser.getUserId(), input.loginUser.getEmail(), input.getJobGroup()); + input.getLoginUser().getUserId(), input.getLoginUser().getEmail(), input.getJobGroup()); return new UpdateJobGroupBusiness.Result(); } diff --git a/src/main/java/com/sirius/spurt/service/business/question/GetQuestionBusiness.java b/src/main/java/com/sirius/spurt/service/business/question/GetQuestionBusiness.java index 8cdf1f8..44063dd 100644 --- a/src/main/java/com/sirius/spurt/service/business/question/GetQuestionBusiness.java +++ b/src/main/java/com/sirius/spurt/service/business/question/GetQuestionBusiness.java @@ -35,14 +35,14 @@ public class GetQuestionBusiness implements Business { @Override public Result execute(Dto input) { - Result result = - GetQuestionBusinessMapper.INSTANCE.toResult( - questionProvider.getQuestion(input.getQuestionId())); - - String experienceTitle = - experienceProvider.getQuestionExperienceTitle(result.getExperienceId(), input.getUserId()); - result.setExperienceTitle(experienceTitle); - return result; + QuestionVo questionVo = questionProvider.getQuestion(input.getQuestionId()); + String experienceTitle = null; + if (questionVo.getExperienceId() != null) { + experienceTitle = + experienceProvider.getQuestionExperienceTitle( + questionVo.getExperienceId(), input.getUserId()); + } + return GetQuestionBusinessMapper.INSTANCE.toResult(questionVo, experienceTitle); } @JsonIgnoreProperties @@ -98,6 +98,6 @@ default List toKeyword(List keyWordList) { return keyWordList.stream().map(KeyWordVo -> KeyWordVo.getKeyWord()).toList(); } - Result toResult(QuestionVo vo); + Result toResult(QuestionVo vo, String experienceTitle); } } diff --git a/src/main/java/com/sirius/spurt/service/business/question/PutQuestionBusiness.java b/src/main/java/com/sirius/spurt/service/business/question/PutQuestionBusiness.java index dccb5cc..ff05bf4 100644 --- a/src/main/java/com/sirius/spurt/service/business/question/PutQuestionBusiness.java +++ b/src/main/java/com/sirius/spurt/service/business/question/PutQuestionBusiness.java @@ -1,11 +1,9 @@ package com.sirius.spurt.service.business.question; -import static com.sirius.spurt.common.meta.ResultCode.NOT_ALL_CATEGORY; - import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.sirius.spurt.common.exception.GlobalException; import com.sirius.spurt.common.meta.Category; import com.sirius.spurt.common.template.Business; +import com.sirius.spurt.common.validator.QuestionValidator; import com.sirius.spurt.service.business.question.PutQuestionBusiness.Dto; import com.sirius.spurt.service.business.question.PutQuestionBusiness.Result; import com.sirius.spurt.store.provider.question.QuestionProvider; @@ -33,10 +31,7 @@ public class PutQuestionBusiness implements Business { @Override public Result execute(Dto input) { - - if (input.getCategoryList() != null && input.getCategoryList().contains(Category.ALL)) { - throw new GlobalException(NOT_ALL_CATEGORY); - } + QuestionValidator.validate(input); questionProvider.putQuestion( input.getQuestionId(), input.getSubject(), diff --git a/src/main/java/com/sirius/spurt/service/business/question/SaveQuestionBusiness.java b/src/main/java/com/sirius/spurt/service/business/question/SaveQuestionBusiness.java index 05e200c..0854ec4 100644 --- a/src/main/java/com/sirius/spurt/service/business/question/SaveQuestionBusiness.java +++ b/src/main/java/com/sirius/spurt/service/business/question/SaveQuestionBusiness.java @@ -1,11 +1,9 @@ package com.sirius.spurt.service.business.question; -import static com.sirius.spurt.common.meta.ResultCode.NOT_ALL_CATEGORY; - import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -import com.sirius.spurt.common.exception.GlobalException; import com.sirius.spurt.common.meta.Category; import com.sirius.spurt.common.template.Business; +import com.sirius.spurt.common.validator.QuestionValidator; import com.sirius.spurt.service.business.question.SaveQuestionBusiness.Dto; import com.sirius.spurt.service.business.question.SaveQuestionBusiness.Result; import com.sirius.spurt.store.provider.question.QuestionProvider; @@ -36,10 +34,7 @@ public class SaveQuestionBusiness implements Business { @Override public Result execute(Dto input) { - - if (input.getCategoryList().contains(Category.ALL)) { - throw new GlobalException(NOT_ALL_CATEGORY); - } + QuestionValidator.validate(input); return SaveQuestionBusinessMapper.INSTANCE.toResult( questionProvider.saveQuestion( diff --git a/src/main/java/com/sirius/spurt/service/business/user/DeleteUserBusiness.java b/src/main/java/com/sirius/spurt/service/business/user/DeleteUserBusiness.java new file mode 100644 index 0000000..c7fb45a --- /dev/null +++ b/src/main/java/com/sirius/spurt/service/business/user/DeleteUserBusiness.java @@ -0,0 +1,46 @@ +package com.sirius.spurt.service.business.user; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.sirius.spurt.common.template.Business; +import com.sirius.spurt.service.business.user.DeleteUserBusiness.Dto; +import com.sirius.spurt.service.business.user.DeleteUserBusiness.Result; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.store.provider.user.UserProvider; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.RequiredArgsConstructor; +import lombok.Setter; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +@Component +@RequiredArgsConstructor +public class DeleteUserBusiness implements Business { + private final UserProvider userProvider; + private final ExperienceProvider experienceProvider; + private final QuestionProvider questionProvider; + + @Override + @Transactional + public Result execute(Dto input) { + experienceProvider.deleteExperienceByUser(input.getUserId()); + questionProvider.deleteQuestionByUser(input.getUserId()); + userProvider.deleteUser(input.getUserId()); + return new Result(); + } + + @Setter + @Getter + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class Dto implements Business.Dto { + private String userId; + } + + @JsonIgnoreProperties + public static class Result implements Business.Result {} +} diff --git a/src/main/java/com/sirius/spurt/service/controller/category/CategoryController.java b/src/main/java/com/sirius/spurt/service/controller/category/CategoryController.java index 950ca52..3e22d77 100644 --- a/src/main/java/com/sirius/spurt/service/controller/category/CategoryController.java +++ b/src/main/java/com/sirius/spurt/service/controller/category/CategoryController.java @@ -19,6 +19,6 @@ public class CategoryController { */ @GetMapping public RestResponse getAllCategory() { - return RestResponse.success(getAllCategoryBusiness.execute(null)); + return RestResponse.success(getAllCategoryBusiness.execute(new GetAllCategoryBusiness.Dto())); } } diff --git a/src/main/java/com/sirius/spurt/service/controller/experience/ExperienceController.java b/src/main/java/com/sirius/spurt/service/controller/experience/ExperienceController.java index 6ce1156..3851892 100644 --- a/src/main/java/com/sirius/spurt/service/controller/experience/ExperienceController.java +++ b/src/main/java/com/sirius/spurt/service/controller/experience/ExperienceController.java @@ -70,7 +70,8 @@ public RestResponse deleteExperience( */ @GetMapping public RestResponse getAllExperience(LoginUser loginUser) { - GetAllExperienceBusiness.Dto dto = new GetAllExperienceBusiness.Dto(loginUser.getUserId()); + GetAllExperienceBusiness.Dto dto = + GetAllExperienceBusiness.Dto.builder().userId(loginUser.getUserId()).build(); return RestResponse.success(getAllExperienceBusiness.execute(dto)); } @@ -83,7 +84,10 @@ public RestResponse getAllExperience(LoginUser public RestResponse getExperience( LoginUser loginUser, @PathVariable("experienceId") Long experienceId) { GetExperienceBusiness.Dto dto = - new GetExperienceBusiness.Dto(loginUser.getUserId(), experienceId); + GetExperienceBusiness.Dto.builder() + .userId(loginUser.getUserId()) + .experienceId(experienceId) + .build(); return RestResponse.success(getExperienceBusiness.execute(dto)); } } diff --git a/src/main/java/com/sirius/spurt/service/controller/user/UserController.java b/src/main/java/com/sirius/spurt/service/controller/user/UserController.java index bd399bb..bbcd828 100644 --- a/src/main/java/com/sirius/spurt/service/controller/user/UserController.java +++ b/src/main/java/com/sirius/spurt/service/controller/user/UserController.java @@ -4,10 +4,12 @@ import com.sirius.spurt.service.business.user.CheckUserExistsBusiness; import com.sirius.spurt.service.business.user.CheckUserHasPinedBusiness; import com.sirius.spurt.service.business.user.CheckUserHasPostedBusiness; +import com.sirius.spurt.service.business.user.DeleteUserBusiness; import com.sirius.spurt.service.business.user.UserInfoBusiness; import com.sirius.spurt.service.controller.RestResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -21,6 +23,7 @@ public class UserController { private final CheckUserHasPinedBusiness checkUserHasPinedBusiness; private final CheckUserHasPostedBusiness checkUserHasPostedBusiness; private final UserInfoBusiness userInfoBusiness; + private final DeleteUserBusiness deleteUserBusiness; /** * @return @@ -65,4 +68,15 @@ public RestResponse getUserInfo(LoginUser loginUser) { userInfoBusiness.execute( UserInfoBusiness.Dto.builder().userId(loginUser.getUserId()).build())); } + + /** + * @return + * @title 유저 삭제 + */ + @DeleteMapping + public RestResponse deleteUser(LoginUser loginUser) { + return RestResponse.success( + deleteUserBusiness.execute( + DeleteUserBusiness.Dto.builder().userId(loginUser.getUserId()).build())); + } } diff --git a/src/main/java/com/sirius/spurt/store/provider/auth/vo/AuthVo.java b/src/main/java/com/sirius/spurt/store/provider/auth/vo/AuthVo.java index 30ad346..8afb161 100644 --- a/src/main/java/com/sirius/spurt/store/provider/auth/vo/AuthVo.java +++ b/src/main/java/com/sirius/spurt/store/provider/auth/vo/AuthVo.java @@ -10,6 +10,6 @@ @NoArgsConstructor @AllArgsConstructor public class AuthVo { - String userId; - String email; + private String userId; + private String email; } diff --git a/src/main/java/com/sirius/spurt/store/provider/experience/ExperienceProvider.java b/src/main/java/com/sirius/spurt/store/provider/experience/ExperienceProvider.java index 68e7c49..5acefca 100644 --- a/src/main/java/com/sirius/spurt/store/provider/experience/ExperienceProvider.java +++ b/src/main/java/com/sirius/spurt/store/provider/experience/ExperienceProvider.java @@ -28,4 +28,6 @@ void updateExperience( ExperienceVo getExperience(final Long experienceId, final String userId); String getQuestionExperienceTitle(final Long experienceId, final String userId); + + void deleteExperienceByUser(final String userId); } diff --git a/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java b/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java index 47f0bd8..9615e45 100644 --- a/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java +++ b/src/main/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImpl.java @@ -1,11 +1,10 @@ package com.sirius.spurt.store.provider.experience.impl; -import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; -import static com.sirius.spurt.common.meta.ResultCode.NOT_EXPERIENCE_OWNER; -import static com.sirius.spurt.common.meta.ResultCode.NO_CONTENT; import static com.sirius.spurt.common.meta.ResultCode.TIME_FORMAT_ERROR; import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.common.validator.ExperienceValidator; +import com.sirius.spurt.common.validator.UserValidator; import com.sirius.spurt.store.provider.experience.ExperienceProvider; import com.sirius.spurt.store.provider.experience.vo.CategoryVo; import com.sirius.spurt.store.provider.experience.vo.ExperienceVo; @@ -51,10 +50,11 @@ public ExperienceVo saveExperience( final String link, final String userId) { UserEntity userEntity = userRepository.findByUserId(userId); + UserValidator.validator(userEntity); - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } + ExperienceEntity prevExperience = + experienceRepository.findTopByUserEntityOrderByCreateTimestampDesc(userEntity); + ExperienceValidator.validateTimestamp(prevExperience); ExperienceEntity experienceEntity = ExperienceEntity.builder() @@ -82,10 +82,7 @@ public void updateExperience( final String userId) { ExperienceEntity previous = experienceRepository.findByExperienceIdAndUserEntityUserId(experienceId, userId); - - if (previous == null) { - throw new GlobalException(NOT_EXPERIENCE_OWNER); - } + ExperienceValidator.validate(previous); ExperienceEntity experienceEntity = ExperienceEntity.builder() @@ -107,11 +104,7 @@ public void updateExperience( public void deleteExperience(final Long experienceId, final String userId) { ExperienceEntity previous = experienceRepository.findByExperienceIdAndUserEntityUserId(experienceId, userId); - - if (previous == null) { - throw new GlobalException(NOT_EXPERIENCE_OWNER); - } - + ExperienceValidator.validate(previous); experienceRepository.deleteById(experienceId); } @@ -119,10 +112,7 @@ public void deleteExperience(final Long experienceId, final String userId) { public ExperienceVoList getAllExperience(final String userId) { List experienceEntityList = experienceRepository.findByUserEntityUserId(userId); - - if (CollectionUtils.isEmpty(experienceEntityList)) { - throw new GlobalException(NO_CONTENT); - } + ExperienceValidator.validateNoContents(experienceEntityList); List experienceVoList = ExperienceProviderImplMapper.INSTANCE.toExperienceVoList(experienceEntityList); @@ -137,11 +127,7 @@ public ExperienceVoList getAllExperience(final String userId) { public ExperienceVo getExperience(final Long experienceId, final String userId) { ExperienceEntity experienceEntity = experienceRepository.findByExperienceIdAndUserEntityUserId(experienceId, userId); - - if (experienceEntity == null) { - throw new GlobalException(NO_CONTENT); - } - + ExperienceValidator.validateNoContent(experienceEntity); return ExperienceProviderImplMapper.INSTANCE.toExperienceVo(experienceEntity); } @@ -149,8 +135,18 @@ public ExperienceVo getExperience(final Long experienceId, final String userId) public String getQuestionExperienceTitle(final Long experienceId, final String userId) { ExperienceEntity experienceEntity = experienceRepository.findByExperienceIdAndUserEntityUserId(experienceId, userId); + if (experienceEntity == null) { + return null; + } - return experienceEntity != null ? experienceEntity.getTitle() : null; + return experienceEntity.getTitle(); + } + + @Override + public void deleteExperienceByUser(final String userId) { + UserEntity userEntity = userRepository.findByUserId(userId); + UserValidator.validator(userEntity); + experienceRepository.deleteByUserEntity(userEntity); } @Mapper @@ -173,7 +169,9 @@ default String toStringTime(Timestamp timestamp) { default QuestionVoList toQuestionVoList(List questionEntityList) { List unorderedQuestionList = new ArrayList<>(); + int totalCount = 0; if (!CollectionUtils.isEmpty(questionEntityList)) { + totalCount = questionEntityList.size(); unorderedQuestionList = new java.util.ArrayList<>(questionEntityList.stream().map(this::toQuestionVo).toList()); @@ -191,7 +189,7 @@ default QuestionVoList toQuestionVoList(List questionEntityList) return QuestionVoList.builder() .questionVoList(unorderedQuestionList) - .totalCount(questionEntityList == null ? 0 : questionEntityList.size()) + .totalCount(totalCount) .build(); } diff --git a/src/main/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImpl.java b/src/main/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImpl.java index 5b9f152..636dc19 100644 --- a/src/main/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImpl.java +++ b/src/main/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImpl.java @@ -1,9 +1,7 @@ package com.sirius.spurt.store.provider.jobgroup.impl; -import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; - -import com.sirius.spurt.common.exception.GlobalException; import com.sirius.spurt.common.meta.JobGroup; +import com.sirius.spurt.common.validator.UserValidator; import com.sirius.spurt.store.provider.jobgroup.JobGroupProvider; import com.sirius.spurt.store.repository.database.entity.UserEntity; import com.sirius.spurt.store.repository.database.repository.UserRepository; @@ -33,10 +31,7 @@ public void saveJobGroup(final String userId, final String email, final JobGroup @Transactional public void updateJobGroup(final String userId, final String email, final JobGroup jobGroup) { UserEntity userEntity = userRepository.findByUserId(userId); - - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } + UserValidator.validator(userEntity); userRepository.save( UserEntity.builder() diff --git a/src/main/java/com/sirius/spurt/store/provider/question/QuestionProvider.java b/src/main/java/com/sirius/spurt/store/provider/question/QuestionProvider.java index 9ea8f71..4f7a627 100644 --- a/src/main/java/com/sirius/spurt/store/provider/question/QuestionProvider.java +++ b/src/main/java/com/sirius/spurt/store/provider/question/QuestionProvider.java @@ -42,4 +42,6 @@ QuestionVoList searchQuestion( final Boolean myQuestionIndicator, final String userId, final PageRequest pageRequest); + + void deleteQuestionByUser(final String userId); } diff --git a/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java b/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java index dd72f54..4e72d4e 100644 --- a/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java +++ b/src/main/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImpl.java @@ -1,11 +1,9 @@ package com.sirius.spurt.store.provider.question.impl; -import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; -import static com.sirius.spurt.common.meta.ResultCode.NOT_QUESTION_OWNER; - -import com.sirius.spurt.common.exception.GlobalException; import com.sirius.spurt.common.meta.Category; import com.sirius.spurt.common.meta.JobGroup; +import com.sirius.spurt.common.validator.QuestionValidator; +import com.sirius.spurt.common.validator.UserValidator; import com.sirius.spurt.store.provider.question.QuestionProvider; import com.sirius.spurt.store.provider.question.vo.QuestionVo; import com.sirius.spurt.store.provider.question.vo.QuestionVoList; @@ -13,10 +11,8 @@ import com.sirius.spurt.store.repository.database.entity.KeyWordEntity; import com.sirius.spurt.store.repository.database.entity.QuestionEntity; import com.sirius.spurt.store.repository.database.entity.UserEntity; -import com.sirius.spurt.store.repository.database.repository.ExperienceRepository; import com.sirius.spurt.store.repository.database.repository.QuestionRepository; import com.sirius.spurt.store.repository.database.repository.UserRepository; -import jakarta.transaction.Transactional; import java.sql.Timestamp; import java.time.LocalDateTime; import java.util.List; @@ -27,12 +23,12 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor public class QuestionProviderImpl implements QuestionProvider { private final QuestionRepository questionRepository; - private final ExperienceRepository experienceRepository; private final UserRepository userRepository; @Override @@ -40,17 +36,11 @@ public class QuestionProviderImpl implements QuestionProvider { public void putPinQuestion( final String questionId, final String userId, final Boolean pinIndicator) { UserEntity userEntity = userRepository.findByUserId(userId); - - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } + UserValidator.validator(userEntity); QuestionEntity previous = questionRepository.findByQuestionIdAndUserId(Long.valueOf(questionId), userId); - - if (previous == null) { - throw new GlobalException(NOT_QUESTION_OWNER); - } + QuestionValidator.validate(previous); if (!userEntity.getHasPined()) { userRepository.save( @@ -86,17 +76,16 @@ public QuestionVoList randomQuestion( return QuestionVoList.builder() .questions( QuestionProviderImplMapper.INSTANCE.toQuestionVos( - questionRepository.RandomQuestion(jobGroup, userId, count, category))) + questionRepository.randomQuestion(jobGroup, userId, count, category))) .build(); } @Override public void deleteQuestion(final String userId, final Long questionId) { - QuestionEntity entity = questionRepository.findByQuestionIdAndUserId(questionId, userId); - if (entity == null) { - throw new GlobalException(NOT_QUESTION_OWNER); - } - questionRepository.delete(entity); + QuestionEntity questionEntity = + questionRepository.findByQuestionIdAndUserId(questionId, userId); + QuestionValidator.validate(questionEntity); + questionRepository.delete(questionEntity); } @Override @@ -135,17 +124,11 @@ public void putQuestion( final List categoryList, final String userId) { UserEntity userEntity = userRepository.findByUserId(userId); - - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } + UserValidator.validator(userEntity); QuestionEntity previous = questionRepository.findByQuestionIdAndUserId(Long.valueOf(questionId), userId); - - if (previous == null) { - throw new GlobalException(NOT_QUESTION_OWNER); - } + QuestionValidator.validate(previous); List categoryEntityList = categoryList.stream() @@ -184,10 +167,11 @@ public QuestionVo saveQuestion( final Long experienceId, final String userId) { UserEntity userEntity = userRepository.findByUserId(userId); + UserValidator.validator(userEntity); - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } + QuestionEntity prevQuestion = + questionRepository.findTopByUserIdOrderByCreateTimestampDesc(userId); + QuestionValidator.validateTimestamp(prevQuestion); List categoryEntityList = categoryList.stream() @@ -227,6 +211,11 @@ public QuestionVo saveQuestion( questionRepository.save(questionEntity)); } + @Override + public void deleteQuestionByUser(final String userId) { + questionRepository.deleteByUserId(userId); + } + @Mapper public interface QuestionProviderImplMapper { QuestionProviderImpl.QuestionProviderImplMapper INSTANCE = diff --git a/src/main/java/com/sirius/spurt/store/provider/question/vo/CategoryVo.java b/src/main/java/com/sirius/spurt/store/provider/question/vo/CategoryVo.java index cd3901e..c88f6d1 100644 --- a/src/main/java/com/sirius/spurt/store/provider/question/vo/CategoryVo.java +++ b/src/main/java/com/sirius/spurt/store/provider/question/vo/CategoryVo.java @@ -1,9 +1,15 @@ package com.sirius.spurt.store.provider.question.vo; import com.sirius.spurt.common.meta.Category; -import lombok.Data; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; -@Data +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class CategoryVo { private Long categoryId; diff --git a/src/main/java/com/sirius/spurt/store/provider/question/vo/KeyWordVo.java b/src/main/java/com/sirius/spurt/store/provider/question/vo/KeyWordVo.java index aa41cc6..a614f0a 100644 --- a/src/main/java/com/sirius/spurt/store/provider/question/vo/KeyWordVo.java +++ b/src/main/java/com/sirius/spurt/store/provider/question/vo/KeyWordVo.java @@ -1,8 +1,14 @@ package com.sirius.spurt.store.provider.question.vo; -import lombok.Data; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; -@Data +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor public class KeyWordVo { private Long keyWordId; diff --git a/src/main/java/com/sirius/spurt/store/provider/question/vo/QuestionVo.java b/src/main/java/com/sirius/spurt/store/provider/question/vo/QuestionVo.java index 722801d..fe6ec39 100644 --- a/src/main/java/com/sirius/spurt/store/provider/question/vo/QuestionVo.java +++ b/src/main/java/com/sirius/spurt/store/provider/question/vo/QuestionVo.java @@ -26,7 +26,6 @@ public class QuestionVo { private JobGroup jobGroup; private Boolean pinIndicator; - private String createTime; private Long experienceId; diff --git a/src/main/java/com/sirius/spurt/store/provider/user/UserProvider.java b/src/main/java/com/sirius/spurt/store/provider/user/UserProvider.java index 4ed4b9d..ad729a4 100644 --- a/src/main/java/com/sirius/spurt/store/provider/user/UserProvider.java +++ b/src/main/java/com/sirius/spurt/store/provider/user/UserProvider.java @@ -3,11 +3,13 @@ import com.sirius.spurt.store.provider.user.vo.UserVo; public interface UserProvider { - boolean checkUserExists(String userId); + boolean checkUserExists(final String userId); - UserVo getUserInfo(String userId); + UserVo getUserInfo(final String userId); - boolean checkHasPined(String userId); + boolean checkHasPined(final String userId); - boolean checkHasPosted(String userId); + boolean checkHasPosted(final String userId); + + void deleteUser(final String userId); } diff --git a/src/main/java/com/sirius/spurt/store/provider/user/impl/UserProviderImpl.java b/src/main/java/com/sirius/spurt/store/provider/user/impl/UserProviderImpl.java index b1946c8..7ecef23 100644 --- a/src/main/java/com/sirius/spurt/store/provider/user/impl/UserProviderImpl.java +++ b/src/main/java/com/sirius/spurt/store/provider/user/impl/UserProviderImpl.java @@ -1,8 +1,6 @@ package com.sirius.spurt.store.provider.user.impl; -import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; - -import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.common.validator.UserValidator; import com.sirius.spurt.store.provider.user.UserProvider; import com.sirius.spurt.store.provider.user.vo.UserVo; import com.sirius.spurt.store.repository.database.entity.UserEntity; @@ -18,37 +16,36 @@ public class UserProviderImpl implements UserProvider { private final UserRepository userRepository; @Override - public UserVo getUserInfo(String userId) { + public UserVo getUserInfo(final String userId) { return UserProviderImplMapper.INSTANCE.toUserVo(userRepository.findByUserId(userId)); } @Override - public boolean checkUserExists(String userId) { + public boolean checkUserExists(final String userId) { return userRepository.existsByUserId(userId); } @Override - public boolean checkHasPined(String userId) { + public boolean checkHasPined(final String userId) { UserEntity userEntity = userRepository.findByUserId(userId); - - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } - + UserValidator.validator(userEntity); return userEntity.getHasPined(); } @Override - public boolean checkHasPosted(String userId) { + public boolean checkHasPosted(final String userId) { UserEntity userEntity = userRepository.findByUserId(userId); - - if (userEntity == null) { - throw new GlobalException(NOT_EXIST_USER); - } - + UserValidator.validator(userEntity); return userEntity.getHasPosted(); } + @Override + public void deleteUser(final String userId) { + UserEntity userEntity = userRepository.findByUserId(userId); + UserValidator.validator(userEntity); + userRepository.delete(userEntity); + } + @Mapper public interface UserProviderImplMapper { UserProviderImpl.UserProviderImplMapper INSTANCE = diff --git a/src/main/java/com/sirius/spurt/store/provider/user/vo/UserVo.java b/src/main/java/com/sirius/spurt/store/provider/user/vo/UserVo.java index 9cc92e5..aae227b 100644 --- a/src/main/java/com/sirius/spurt/store/provider/user/vo/UserVo.java +++ b/src/main/java/com/sirius/spurt/store/provider/user/vo/UserVo.java @@ -1,9 +1,11 @@ package com.sirius.spurt.store.provider.user.vo; import com.sirius.spurt.common.meta.JobGroup; +import lombok.Builder; import lombok.Data; @Data +@Builder public class UserVo { private String userId; diff --git a/src/main/java/com/sirius/spurt/store/repository/database/custom/QuestionRepositoryCustom.java b/src/main/java/com/sirius/spurt/store/repository/database/custom/QuestionRepositoryCustom.java index 171d6f8..c7ef3b0 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/custom/QuestionRepositoryCustom.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/custom/QuestionRepositoryCustom.java @@ -18,6 +18,6 @@ Page searchQuestion( final String userId, final PageRequest pageRequest); - List RandomQuestion( + List randomQuestion( final JobGroup jobGroup, final String userId, final Integer count, final Category category); } diff --git a/src/main/java/com/sirius/spurt/store/repository/database/custom/impl/QuestionRepositoryImpl.java b/src/main/java/com/sirius/spurt/store/repository/database/custom/impl/QuestionRepositoryImpl.java index 67acf27..7c9cb42 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/custom/impl/QuestionRepositoryImpl.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/custom/impl/QuestionRepositoryImpl.java @@ -23,7 +23,7 @@ public class QuestionRepositoryImpl implements QuestionRepositoryCustom { private final JPAQueryFactory jpaQueryFactory; @Override - public List RandomQuestion( + public List randomQuestion( final JobGroup jobGroup, final String userId, final Integer count, final Category category) { return jpaQueryFactory .selectFrom(QQuestionEntity.questionEntity) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/entity/CategoryEntity.java b/src/main/java/com/sirius/spurt/store/repository/database/entity/CategoryEntity.java index 2494e77..c5bcf5a 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/entity/CategoryEntity.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/entity/CategoryEntity.java @@ -19,7 +19,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -@Table(name = "CATEGORY") +@Table(name = "tb_category") public class CategoryEntity extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/entity/ExperienceEntity.java b/src/main/java/com/sirius/spurt/store/repository/database/entity/ExperienceEntity.java index d59b175..bd1819f 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/entity/ExperienceEntity.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/entity/ExperienceEntity.java @@ -22,7 +22,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -@Table(name = "EXPERIENCE") +@Table(name = "tb_experience") public class ExperienceEntity extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/entity/KeyWordEntity.java b/src/main/java/com/sirius/spurt/store/repository/database/entity/KeyWordEntity.java index 02d20a3..3c5d74d 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/entity/KeyWordEntity.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/entity/KeyWordEntity.java @@ -16,7 +16,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -@Table(name = "KEYWORD") +@Table(name = "tb_keyword") public class KeyWordEntity extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/entity/QuestionEntity.java b/src/main/java/com/sirius/spurt/store/repository/database/entity/QuestionEntity.java index d8fc5c6..74f0cf6 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/entity/QuestionEntity.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/entity/QuestionEntity.java @@ -24,7 +24,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -@Table(name = "QUESTION") +@Table(name = "tb_question") public class QuestionEntity extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -46,8 +46,7 @@ public class QuestionEntity extends BaseEntity { @Column private Boolean pinIndicator; @Column private Timestamp pinUpdatedTime; - @Column(nullable = false) - private Long experienceId; + @Column private Long experienceId; // 키워드 테이블 조인 @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/entity/SampleEntity.java b/src/main/java/com/sirius/spurt/store/repository/database/entity/SampleEntity.java index 487ad26..6a2dac3 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/entity/SampleEntity.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/entity/SampleEntity.java @@ -16,7 +16,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -@Table(name = "SAMPLE") +@Table(name = "tb_sample") public class SampleEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/sirius/spurt/store/repository/database/entity/UserEntity.java b/src/main/java/com/sirius/spurt/store/repository/database/entity/UserEntity.java index 2124742..a0ed68f 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/entity/UserEntity.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/entity/UserEntity.java @@ -17,7 +17,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -@Table(name = "USER") +@Table(name = "tb_user") public class UserEntity extends BaseEntity { @Id private String userId; diff --git a/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java b/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java index c79a33c..51ecb0e 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepository.java @@ -1,6 +1,7 @@ package com.sirius.spurt.store.repository.database.repository; import com.sirius.spurt.store.repository.database.entity.ExperienceEntity; +import com.sirius.spurt.store.repository.database.entity.UserEntity; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; @@ -9,4 +10,8 @@ ExperienceEntity findByExperienceIdAndUserEntityUserId( final Long experienceId, final String userId); List findByUserEntityUserId(final String userId); + + ExperienceEntity findTopByUserEntityOrderByCreateTimestampDesc(UserEntity userEntity); + + void deleteByUserEntity(UserEntity userEntity); } diff --git a/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java b/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java index 130ff78..793706a 100644 --- a/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java +++ b/src/main/java/com/sirius/spurt/store/repository/database/repository/QuestionRepository.java @@ -11,4 +11,8 @@ public interface QuestionRepository QuestionEntity findByQuestionId(final Long id); QuestionEntity findByQuestionIdAndUserId(final Long id, final String userId); + + QuestionEntity findTopByUserIdOrderByCreateTimestampDesc(String userId); + + void deleteByUserId(String userId); } diff --git a/src/main/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthImpl.java b/src/main/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthRepositoryImpl.java similarity index 90% rename from src/main/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthImpl.java rename to src/main/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthRepositoryImpl.java index 16768e6..caeaa0b 100644 --- a/src/main/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthImpl.java +++ b/src/main/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthRepositoryImpl.java @@ -4,6 +4,7 @@ import com.sirius.spurt.common.meta.ResultCode; import com.sirius.spurt.store.repository.resttemplate.auth.AuthRepository; import com.sirius.spurt.store.repository.resttemplate.auth.playload.UserInfoPayload; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; @@ -16,7 +17,10 @@ @Service @Slf4j -public class AuthImpl implements AuthRepository { +@RequiredArgsConstructor +public class AuthRepositoryImpl implements AuthRepository { + private final RestTemplate restTemplate; + @Value("${user-info-endpoint}") private String userInfoEndpoint; @@ -24,10 +28,8 @@ public class AuthImpl implements AuthRepository { public UserInfoPayload getUserInfo(String accessToken) { try { MultiValueMap httpBody = new LinkedMultiValueMap<>(); - HttpEntity> req = new HttpEntity<>(httpBody); - RestTemplate restTemplate = new RestTemplate(); ResponseEntity res = restTemplate.exchange( userInfoEndpoint + "?access_token=" + accessToken, diff --git a/src/main/resources/application.yml b/src/main/resources/application-test.yml similarity index 57% rename from src/main/resources/application.yml rename to src/main/resources/application-test.yml index 0d31050..eb9fcd8 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application-test.yml @@ -1,13 +1,9 @@ -server: - port: 8080 - spring: datasource: - driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://27.96.135.5:3306/spurt?useSSL=false&serverTimezone=Asia/Seoul&characterEncoding=UTF-8 - username: root - password: root - + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:testdb;MODE=MySQL; + username: sa + password: jpa: open-in-view: true @@ -25,9 +21,3 @@ spring: init: mode: always encoding: UTF-8 - -logging: - level: - org.hibernate.SQL: debug - -user-info-endpoint: https://www.googleapis.com/oauth2/v1/userinfo diff --git a/src/test/java/com/sirius/spurt/BaseMvcTest.java b/src/test/java/com/sirius/spurt/BaseMvcTest.java index e84108e..b20d46d 100644 --- a/src/test/java/com/sirius/spurt/BaseMvcTest.java +++ b/src/test/java/com/sirius/spurt/BaseMvcTest.java @@ -46,7 +46,7 @@ public void setUp(RestDocumentationContextProvider restDocumentation) throws Exc Mockito.when(authProvider.getUserId(anyString())).thenReturn(authVo); var mockMvcRequestBuilders = - MockMvcRequestBuilders.get("http://example.com") + MockMvcRequestBuilders.get("https://api.spurtapp.com") .header("Authorization", "Bearer <<전달받은토큰값>>"); this.mockMvc = MockMvcBuilders.webAppContextSetup(context) @@ -58,14 +58,14 @@ public void setUp(RestDocumentationContextProvider restDocumentation) throws Exc Preprocessors.preprocessRequest(), Preprocessors.preprocessResponse( ResponseModifyingPreprocessors.replaceBinaryContent(), - ResponseModifyingPreprocessors.limitJsonArrayLength(objectMapper), + // ResponseModifyingPreprocessors.limitJsonArrayLength(objectMapper), Preprocessors.prettyPrint()))) .apply( MockMvcRestDocumentation.documentationConfiguration(restDocumentation) .uris() - .withScheme("http") - .withHost("localhost") - .withPort(8080) + .withScheme("https") + .withHost("api.spurtapp.com") + .withPort(443) .and() .snippets() .withDefaults( diff --git a/src/test/java/com/sirius/spurt/CategoryControllerTest.java b/src/test/java/com/sirius/spurt/CategoryControllerTest.java index df48437..64da7a0 100644 --- a/src/test/java/com/sirius/spurt/CategoryControllerTest.java +++ b/src/test/java/com/sirius/spurt/CategoryControllerTest.java @@ -1,5 +1,6 @@ package com.sirius.spurt; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; @@ -26,7 +27,7 @@ public class CategoryControllerTest extends BaseMvcTest { GetAllCategoryBusinessMapper.INSTANCE.toResultCategoryList( List.of(Category.values()))) .build(); - when(getAllCategoryBusiness.execute(null)).thenReturn(result); + when(getAllCategoryBusiness.execute(any())).thenReturn(result); this.mockMvc.perform(get("/v1/category")).andExpect(status().isOk()).andDo(print()); } } diff --git a/src/test/java/com/sirius/spurt/ExperienceControllerTest.java b/src/test/java/com/sirius/spurt/ExperienceControllerTest.java index 1013925..2d60673 100644 --- a/src/test/java/com/sirius/spurt/ExperienceControllerTest.java +++ b/src/test/java/com/sirius/spurt/ExperienceControllerTest.java @@ -10,26 +10,36 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import com.sirius.spurt.common.meta.Category; +import com.sirius.spurt.common.resolver.user.LoginUser; import com.sirius.spurt.service.business.experience.DeleteExperienceBusiness; import com.sirius.spurt.service.business.experience.GetAllExperienceBusiness; import com.sirius.spurt.service.business.experience.GetExperienceBusiness; import com.sirius.spurt.service.business.experience.SaveExperienceBusiness; import com.sirius.spurt.service.business.experience.UpdateExperienceBusiness; import com.sirius.spurt.service.controller.experience.ExperienceController; +import com.sirius.spurt.test.ExperienceTest; import java.util.List; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.MediaType; @WebMvcTest(controllers = {ExperienceController.class}) -public class ExperienceControllerTest extends BaseMvcTest { +public class ExperienceControllerTest extends BaseMvcTest implements ExperienceTest { @MockBean private SaveExperienceBusiness saveExperienceBusiness; @MockBean private UpdateExperienceBusiness updateExperienceBusiness; @MockBean private DeleteExperienceBusiness deleteExperienceBusiness; @MockBean private GetAllExperienceBusiness getAllExperienceBusiness; @MockBean private GetExperienceBusiness getExperienceBusiness; + private LoginUser loginUser; + + @BeforeEach + void setUp() { + loginUser = LoginUser.builder().userId(TEST_USER_ID).email(TEST_EMAIL).build(); + } + @Test void 본인_경험_저장() throws Exception { SaveExperienceBusiness.Dto dto = @@ -39,14 +49,20 @@ public class ExperienceControllerTest extends BaseMvcTest { .startDate("2023-07") .endDate("2023-08") .link("link") - .userId("admin") .build(); - when(saveExperienceBusiness.execute(any())).thenReturn(new SaveExperienceBusiness.Result()); + SaveExperienceBusiness.Result result = + SaveExperienceBusiness.Result.builder().experienceId(TEST_EXPERIENCE_ID).build(); + when(saveExperienceBusiness.execute(any())).thenReturn(result); this.mockMvc .perform( post("/v1/experience") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(dto))) + .content(objectMapper.writeValueAsString(dto)) + .with( + request -> { + request.setAttribute("loginUser", loginUser); + return request; + })) .andExpect(status().isOk()) .andDo(print()); } @@ -61,14 +77,18 @@ public class ExperienceControllerTest extends BaseMvcTest { .startDate("2023-07") .endDate("2023-08") .link("link") - .userId("admin") .build(); when(updateExperienceBusiness.execute(any())).thenReturn(new UpdateExperienceBusiness.Result()); this.mockMvc .perform( put("/v1/experience") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(dto))) + .content(objectMapper.writeValueAsString(dto)) + .with( + request -> { + request.setAttribute("loginUser", loginUser); + return request; + })) .andExpect(status().isOk()) .andDo(print()); } @@ -76,28 +96,32 @@ public class ExperienceControllerTest extends BaseMvcTest { @Test void 본인_경험_삭제() throws Exception { DeleteExperienceBusiness.Dto dto = - DeleteExperienceBusiness.Dto.builder().experienceId(1L).userId("admin").build(); + DeleteExperienceBusiness.Dto.builder().experienceId(1L).build(); when(deleteExperienceBusiness.execute(any())).thenReturn(new DeleteExperienceBusiness.Result()); this.mockMvc .perform( delete("/v1/experience") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(dto))) + .content(objectMapper.writeValueAsString(dto)) + .with( + request -> { + request.setAttribute("loginUser", loginUser); + return request; + })) .andExpect(status().isOk()) .andDo(print()); } @Test void 본인_경험_전체_조회() throws Exception { - GetAllExperienceBusiness.Dto dto = - GetAllExperienceBusiness.Dto.builder().userId("admin").build(); GetAllExperienceBusiness.Result.Experience.QuestionList.Question question = GetAllExperienceBusiness.Result.Experience.QuestionList.Question.builder() .questionId(1L) .subject("질문제목1") .mainText("질문내용1") - .pinIndicator(false) + .pinIndicator(Boolean.FALSE) .categoryList(List.of(Category.PRACTICAL)) + .keyWordList(List.of("keyWord1", "keyWord2")) .build(); GetAllExperienceBusiness.Result.Experience.QuestionList questionList = GetAllExperienceBusiness.Result.Experience.QuestionList.builder() @@ -115,22 +139,33 @@ public class ExperienceControllerTest extends BaseMvcTest { .questionList(questionList) .build(); GetAllExperienceBusiness.Result result = - GetAllExperienceBusiness.Result.builder().experienceList(List.of(experience)).build(); + GetAllExperienceBusiness.Result.builder() + .experienceList(List.of(experience)) + .totalCount(1) + .build(); when(getAllExperienceBusiness.execute(any())).thenReturn(result); - this.mockMvc.perform(get("/v1/experience")).andExpect(status().isOk()).andDo(print()); + this.mockMvc + .perform( + get("/v1/experience") + .with( + request -> { + request.setAttribute("loginUser", loginUser); + return request; + })) + .andExpect(status().isOk()) + .andDo(print()); } @Test void 본인_경험_단건_조회() throws Exception { - GetExperienceBusiness.Dto dto = - GetExperienceBusiness.Dto.builder().experienceId(1L).userId("admin").build(); GetExperienceBusiness.Result.QuestionList.Question question = GetExperienceBusiness.Result.QuestionList.Question.builder() .questionId(1L) .subject("질문제목1") .mainText("질문내용1") - .pinIndicator(false) + .pinIndicator(Boolean.FALSE) .categoryList(List.of(Category.PRACTICAL)) + .keyWordList(List.of("keyWord1", "keyWord2")) .build(); GetExperienceBusiness.Result.QuestionList questionList = GetExperienceBusiness.Result.QuestionList.builder() @@ -148,6 +183,15 @@ public class ExperienceControllerTest extends BaseMvcTest { .questionList(questionList) .build(); when(getExperienceBusiness.execute(any())).thenReturn(result); - this.mockMvc.perform(get("/v1/experience/1")).andExpect(status().isOk()).andDo(print()); + this.mockMvc + .perform( + get("/v1/experience/1") + .with( + request -> { + request.setAttribute("loginUser", loginUser); + return request; + })) + .andExpect(status().isOk()) + .andDo(print()); } } diff --git a/src/test/java/com/sirius/spurt/JobGroupControllerTest.java b/src/test/java/com/sirius/spurt/JobGroupControllerTest.java index 37342d7..e05b569 100644 --- a/src/test/java/com/sirius/spurt/JobGroupControllerTest.java +++ b/src/test/java/com/sirius/spurt/JobGroupControllerTest.java @@ -1,5 +1,6 @@ package com.sirius.spurt; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; @@ -29,7 +30,7 @@ public class JobGroupControllerTest extends BaseMvcTest { .loginUser(loginUser) .jobGroup(JobGroup.DEVELOPER) .build(); - when(saveJobGroupBusiness.execute(dto)).thenReturn(new SaveJobGroupBusiness.Result()); + when(saveJobGroupBusiness.execute(any())).thenReturn(new SaveJobGroupBusiness.Result()); this.mockMvc .perform( post("/v1/jobgroup") @@ -48,7 +49,7 @@ public class JobGroupControllerTest extends BaseMvcTest { .loginUser(loginUser) .jobGroup(JobGroup.DEVELOPER) .build(); - when(updateJobGroupBusiness.execute(dto)).thenReturn(new UpdateJobGroupBusiness.Result()); + when(updateJobGroupBusiness.execute(any())).thenReturn(new UpdateJobGroupBusiness.Result()); this.mockMvc .perform( put("/v1/jobgroup") diff --git a/src/test/java/com/sirius/spurt/QuestionControllerTest.java b/src/test/java/com/sirius/spurt/QuestionControllerTest.java index 25990cf..d21c32d 100644 --- a/src/test/java/com/sirius/spurt/QuestionControllerTest.java +++ b/src/test/java/com/sirius/spurt/QuestionControllerTest.java @@ -19,6 +19,9 @@ import com.sirius.spurt.service.business.question.RetrieveQuestionBusiness; import com.sirius.spurt.service.business.question.SaveQuestionBusiness; import com.sirius.spurt.service.controller.question.QuestionController; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.KeyWordTest; +import com.sirius.spurt.test.QuestionTest; import java.util.List; import org.junit.jupiter.api.Test; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; @@ -26,7 +29,8 @@ import org.springframework.http.MediaType; @WebMvcTest(controllers = {QuestionController.class}) -public class QuestionControllerTest extends BaseMvcTest { +public class QuestionControllerTest extends BaseMvcTest + implements QuestionTest, KeyWordTest, CategoryTest { @MockBean private RetrieveQuestionBusiness retrieveQuestionBusiness; @MockBean private SaveQuestionBusiness saveQuestionBusiness; @MockBean private PutQuestionBusiness putQuestionBusiness; @@ -89,10 +93,13 @@ public class QuestionControllerTest extends BaseMvcTest { GetQuestionBusiness.Dto.builder().questionId(Long.parseLong("1")).build(); GetQuestionBusiness.Result result = GetQuestionBusiness.Result.builder() + .questionId(TEST_QUESTION_ID) .subject("제목") .jobGroup(JobGroup.DEVELOPER) .mainText("본문") .createTime("2023-08-13 01:39:21") + .userId(TEST_USER_ID) + .pinIndicator(Boolean.FALSE) .categoryList(List.of(Category.CONFLICT)) .keyWordList(List.of("키워드")) .build(); @@ -129,7 +136,9 @@ public class QuestionControllerTest extends BaseMvcTest { .categoryList(List.of(Category.CONFLICT)) .keyWordList(List.of("testKeyword")) .build(); - when(saveQuestionBusiness.execute(any())).thenReturn(new SaveQuestionBusiness.Result()); + SaveQuestionBusiness.Result result = + SaveQuestionBusiness.Result.builder().questionId(TEST_QUESTION_ID).build(); + when(saveQuestionBusiness.execute(any())).thenReturn(result); this.mockMvc .perform( post("/v1/question") @@ -147,11 +156,14 @@ public class QuestionControllerTest extends BaseMvcTest { .questions( List.of( RetrieveQuestionBusiness.Result.Question.builder() - .subject("제목") - .categoryList(List.of(Category.CONFLICT)) - .jobGroup(JobGroup.DEVELOPER) - .mainText("본문") - .createTime("2023-08-13 01:39:21") + .questionId(TEST_QUESTION_ID) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .pinIndicator(Boolean.TRUE) + .keyWordList(List.of(TEST_KEY_WORD_VALUE)) + .categoryList(List.of(TEST_CATEGORY)) + .jobGroup(TEST_JOB_GROUP) + .createTime(TEST_CREATE_TIME) .build())) .meta( RetrieveQuestionBusiness.Result.MetaData.builder() @@ -164,10 +176,8 @@ public class QuestionControllerTest extends BaseMvcTest { this.mockMvc .perform( get("/v1/question") - .param("subject", "제목") - .param("pinIndicator", "false") - .param("jobGroup", JobGroup.DEVELOPER.name()) - .param("category", Category.CONFLICT.name())) + .param("jobGroup", TEST_JOB_GROUP.name()) + .param("category", TEST_CATEGORY.name())) .andExpect(status().isOk()) .andDo(print()); } diff --git a/src/test/java/com/sirius/spurt/UserControllerTest.java b/src/test/java/com/sirius/spurt/UserControllerTest.java index 9ccbee9..c31f886 100644 --- a/src/test/java/com/sirius/spurt/UserControllerTest.java +++ b/src/test/java/com/sirius/spurt/UserControllerTest.java @@ -2,6 +2,7 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -10,6 +11,7 @@ import com.sirius.spurt.service.business.user.CheckUserExistsBusiness; import com.sirius.spurt.service.business.user.CheckUserHasPinedBusiness; import com.sirius.spurt.service.business.user.CheckUserHasPostedBusiness; +import com.sirius.spurt.service.business.user.DeleteUserBusiness; import com.sirius.spurt.service.business.user.UserInfoBusiness; import com.sirius.spurt.service.controller.user.UserController; import org.junit.jupiter.api.Test; @@ -22,6 +24,7 @@ public class UserControllerTest extends BaseMvcTest { @MockBean private CheckUserHasPinedBusiness checkUserHasPinedBusiness; @MockBean private CheckUserHasPostedBusiness checkUserHasPostedBusiness; @MockBean private UserInfoBusiness userInfoBusiness; + @MockBean private DeleteUserBusiness deleteUserBusiness; @Test void 유저_정보_조회() throws Exception { @@ -60,4 +63,12 @@ public class UserControllerTest extends BaseMvcTest { when(checkUserHasPostedBusiness.execute(any())).thenReturn(result); this.mockMvc.perform(get("/v1/user/posting")).andExpect(status().isOk()).andDo(print()); } + + @Test + void 유저_삭제_확인() throws Exception { + DeleteUserBusiness.Dto dto = DeleteUserBusiness.Dto.builder().userId("userId").build(); + DeleteUserBusiness.Result result = new DeleteUserBusiness.Result(); + when(deleteUserBusiness.execute(any())).thenReturn(result); + this.mockMvc.perform(delete("/v1/user")).andExpect(status().isOk()).andDo(print()); + } } diff --git a/src/test/java/com/sirius/spurt/common/config/QuerydslConfigTest.java b/src/test/java/com/sirius/spurt/common/config/QuerydslConfigTest.java new file mode 100644 index 0000000..38c67b7 --- /dev/null +++ b/src/test/java/com/sirius/spurt/common/config/QuerydslConfigTest.java @@ -0,0 +1,23 @@ +package com.sirius.spurt.common.config; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import com.sirius.spurt.store.repository.database.custom.impl.QuestionRepositoryImpl; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +@TestConfiguration +public class QuerydslConfigTest { + @PersistenceContext private EntityManager entityManager; + + @Bean + public JPAQueryFactory jpaQueryFactory() { + return new JPAQueryFactory(entityManager); + } + + @Bean + public QuestionRepositoryImpl questionRepositoryImpl() { + return new QuestionRepositoryImpl(jpaQueryFactory()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/category/GetAllCategoryBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/category/GetAllCategoryBusinessTest.java new file mode 100644 index 0000000..a800343 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/category/GetAllCategoryBusinessTest.java @@ -0,0 +1,34 @@ +package com.sirius.spurt.service.business.category; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.sirius.spurt.common.meta.Category; +import com.sirius.spurt.service.business.category.GetAllCategoryBusiness.Dto; +import com.sirius.spurt.service.business.category.GetAllCategoryBusiness.Result; +import com.sirius.spurt.service.business.category.GetAllCategoryBusiness.Result.ResultCategory; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class GetAllCategoryBusinessTest { + @InjectMocks private GetAllCategoryBusiness getAllCategoryBusiness; + + @Test + void 카테고리_전체_조회_테스트() { + // given + + // when + Result result = getAllCategoryBusiness.execute(new Dto()); + + // then + List categories = + result.getCategoryList().stream().map(ResultCategory::getValue).toList(); + List expectedCategories = + Arrays.stream(Category.values()).map(Category::getValue).toList(); + assertThat(categories).containsAll(expectedCategories); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/experience/DeleteExperienceBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/experience/DeleteExperienceBusinessTest.java new file mode 100644 index 0000000..86a314b --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/experience/DeleteExperienceBusinessTest.java @@ -0,0 +1,32 @@ +package com.sirius.spurt.service.business.experience; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.service.business.experience.DeleteExperienceBusiness.Dto; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.test.ExperienceTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class DeleteExperienceBusinessTest implements ExperienceTest { + @InjectMocks private DeleteExperienceBusiness deleteExperienceBusiness; + + @Mock private ExperienceProvider experienceProvider; + + @Test + void 경험_삭제_테스트() { + // given + Dto dto = Dto.builder().experienceId(TEST_EXPERIENCE_ID).userId(TEST_USER_ID).build(); + + // when + deleteExperienceBusiness.execute(dto); + + // then + verify(experienceProvider).deleteExperience(any(), any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/experience/GetAllExperienceBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/experience/GetAllExperienceBusinessTest.java new file mode 100644 index 0000000..9673fe0 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/experience/GetAllExperienceBusinessTest.java @@ -0,0 +1,75 @@ +package com.sirius.spurt.service.business.experience; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.experience.GetAllExperienceBusiness.Dto; +import com.sirius.spurt.service.business.experience.GetAllExperienceBusiness.Result; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.store.provider.experience.vo.ExperienceVo; +import com.sirius.spurt.store.provider.experience.vo.ExperienceVoList; +import com.sirius.spurt.test.ExperienceTest; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class GetAllExperienceBusinessTest implements ExperienceTest { + @InjectMocks private GetAllExperienceBusiness getAllExperienceBusiness; + + @Mock private ExperienceProvider experienceProvider; + + @Test + void 경험_전체_조회_테스트() { + // given + ExperienceVo experienceVo = + ExperienceVo.builder() + .experienceId(TEST_EXPERIENCE_ID) + .title(TEST_EXPERIENCE_TITLE) + .content(TEST_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE_STRING) + .endDate(TEST_EXPERIENCE_END_DATE_STRING) + .link(TEST_EXPERIENCE_LINK) + .build(); + ExperienceVo anotherExperienceVo = + ExperienceVo.builder() + .experienceId(TEST_ANOTHER_EXPERIENCE_ID) + .title(TEST_ANOTHER_EXPERIENCE_TITLE) + .content(TEST_ANOTHER_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE_STRING) + .endDate(TEST_EXPERIENCE_END_DATE_STRING) + .link(TEST_ANOTHER_EXPERIENCE_LINK) + .build(); + ExperienceVoList experienceVoList = + ExperienceVoList.builder() + .experienceVoList(List.of(experienceVo, anotherExperienceVo)) + .totalCount(2) + .build(); + Dto dto = Dto.builder().userId(TEST_USER_ID).build(); + when(experienceProvider.getAllExperience(any())).thenReturn(experienceVoList); + + // when + Result result = getAllExperienceBusiness.execute(dto); + + // then + verify(experienceProvider).getAllExperience(any()); + assertThat(result.getTotalCount()).isEqualTo(2); + assertThat(result.getExperienceList().size()).isEqualTo(2); + assertThat(result.getExperienceList().get(0).getExperienceId()).isEqualTo(TEST_EXPERIENCE_ID); + assertThat(result.getExperienceList().get(0).getTitle()).isEqualTo(TEST_EXPERIENCE_TITLE); + assertThat(result.getExperienceList().get(0).getContent()).isEqualTo(TEST_EXPERIENCE_CONTENT); + assertThat(result.getExperienceList().get(0).getLink()).isEqualTo(TEST_EXPERIENCE_LINK); + assertThat(result.getExperienceList().get(1).getExperienceId()) + .isEqualTo(TEST_ANOTHER_EXPERIENCE_ID); + assertThat(result.getExperienceList().get(1).getTitle()) + .isEqualTo(TEST_ANOTHER_EXPERIENCE_TITLE); + assertThat(result.getExperienceList().get(1).getContent()) + .isEqualTo(TEST_ANOTHER_EXPERIENCE_CONTENT); + assertThat(result.getExperienceList().get(1).getLink()).isEqualTo(TEST_ANOTHER_EXPERIENCE_LINK); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/experience/GetExperienceBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/experience/GetExperienceBusinessTest.java new file mode 100644 index 0000000..47a1628 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/experience/GetExperienceBusinessTest.java @@ -0,0 +1,52 @@ +package com.sirius.spurt.service.business.experience; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.experience.GetExperienceBusiness.Dto; +import com.sirius.spurt.service.business.experience.GetExperienceBusiness.Result; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.store.provider.experience.vo.ExperienceVo; +import com.sirius.spurt.test.ExperienceTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class GetExperienceBusinessTest implements ExperienceTest { + @InjectMocks private GetExperienceBusiness getExperienceBusiness; + + @Mock private ExperienceProvider experienceProvider; + + @Test + void 경험_단건_조회_테스트() { + // given + ExperienceVo experienceVo = + ExperienceVo.builder() + .experienceId(TEST_EXPERIENCE_ID) + .title(TEST_EXPERIENCE_TITLE) + .content(TEST_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE_STRING) + .endDate(TEST_EXPERIENCE_END_DATE_STRING) + .link(TEST_EXPERIENCE_LINK) + .build(); + Dto dto = Dto.builder().userId(TEST_USER_ID).experienceId(TEST_EXPERIENCE_ID).build(); + when(experienceProvider.getExperience(any(), any())).thenReturn(experienceVo); + + // when + Result result = getExperienceBusiness.execute(dto); + + // then + verify(experienceProvider).getExperience(any(), any()); + assertThat(result.getExperienceId()).isEqualTo(TEST_EXPERIENCE_ID); + assertThat(result.getTitle()).isEqualTo(TEST_EXPERIENCE_TITLE); + assertThat(result.getContent()).isEqualTo(TEST_EXPERIENCE_CONTENT); + assertThat(result.getStartDate()).isEqualTo(TEST_EXPERIENCE_START_DATE_STRING); + assertThat(result.getEndDate()).isEqualTo(TEST_EXPERIENCE_END_DATE_STRING); + assertThat(result.getLink()).isEqualTo(TEST_EXPERIENCE_LINK); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/experience/SaveExperienceBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/experience/SaveExperienceBusinessTest.java new file mode 100644 index 0000000..f2f1b33 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/experience/SaveExperienceBusinessTest.java @@ -0,0 +1,56 @@ +package com.sirius.spurt.service.business.experience; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.experience.SaveExperienceBusiness.Dto; +import com.sirius.spurt.service.business.experience.SaveExperienceBusiness.Result; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.store.provider.experience.vo.ExperienceVo; +import com.sirius.spurt.test.ExperienceTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class SaveExperienceBusinessTest implements ExperienceTest { + @InjectMocks private SaveExperienceBusiness saveExperienceBusiness; + + @Mock private ExperienceProvider experienceProvider; + + @Test + void 경험_저장_테스트() { + // given + ExperienceVo experienceVo = + ExperienceVo.builder() + .experienceId(TEST_EXPERIENCE_ID) + .title(TEST_EXPERIENCE_TITLE) + .content(TEST_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE_STRING) + .endDate(TEST_EXPERIENCE_END_DATE_STRING) + .link(TEST_EXPERIENCE_LINK) + .build(); + Dto dto = + Dto.builder() + .title(TEST_EXPERIENCE_TITLE) + .content(TEST_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE_STRING) + .endDate(TEST_EXPERIENCE_END_DATE_STRING) + .link(TEST_EXPERIENCE_LINK) + .userId(TEST_USER_ID) + .build(); + when(experienceProvider.saveExperience(any(), any(), any(), any(), any(), any())) + .thenReturn(experienceVo); + + // when + Result result = saveExperienceBusiness.execute(dto); + + // then + verify(experienceProvider).saveExperience(any(), any(), any(), any(), any(), any()); + assertThat(result.getExperienceId()).isEqualTo(TEST_EXPERIENCE_ID); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/experience/UpdateExperienceBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/experience/UpdateExperienceBusinessTest.java new file mode 100644 index 0000000..fd15b8e --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/experience/UpdateExperienceBusinessTest.java @@ -0,0 +1,41 @@ +package com.sirius.spurt.service.business.experience; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.service.business.experience.UpdateExperienceBusiness.Dto; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.test.ExperienceTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class UpdateExperienceBusinessTest implements ExperienceTest { + @InjectMocks private UpdateExperienceBusiness updateExperienceBusiness; + + @Mock private ExperienceProvider experienceProvider; + + @Test + void 경험_수정_테스트() { + // given + Dto dto = + Dto.builder() + .experienceId(TEST_EXPERIENCE_ID) + .title(TEST_EXPERIENCE_TITLE) + .content(TEST_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE_STRING) + .endDate(TEST_EXPERIENCE_END_DATE_STRING) + .link(TEST_EXPERIENCE_LINK) + .userId(TEST_USER_ID) + .build(); + + // when + updateExperienceBusiness.execute(dto); + + // then + verify(experienceProvider).updateExperience(any(), any(), any(), any(), any(), any(), any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusinessTest.java new file mode 100644 index 0000000..d07f90f --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/jobgroup/SaveJobGroupBusinessTest.java @@ -0,0 +1,37 @@ +package com.sirius.spurt.service.business.jobgroup; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.common.resolver.user.LoginUser; +import com.sirius.spurt.service.business.jobgroup.SaveJobGroupBusiness.Dto; +import com.sirius.spurt.store.provider.jobgroup.JobGroupProvider; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class SaveJobGroupBusinessTest implements UserTest { + @InjectMocks private SaveJobGroupBusiness saveJobGroupBusiness; + + @Mock private JobGroupProvider jobGroupProvider; + + @Test + void 직군_저장_테스트() { + // given + Dto dto = + Dto.builder() + .loginUser(LoginUser.builder().userId(TEST_USER_ID).email(TEST_EMAIL).build()) + .jobGroup(TEST_JOB_GROUP) + .build(); + + // when + saveJobGroupBusiness.execute(dto); + + // then + verify(jobGroupProvider).saveJobGroup(any(), any(), any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusinessTest.java new file mode 100644 index 0000000..f4bcd2e --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/jobgroup/UpdateJobGroupBusinessTest.java @@ -0,0 +1,38 @@ +package com.sirius.spurt.service.business.jobgroup; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.common.resolver.user.LoginUser; +import com.sirius.spurt.service.business.jobgroup.UpdateJobGroupBusiness.Dto; +import com.sirius.spurt.store.provider.jobgroup.JobGroupProvider; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class UpdateJobGroupBusinessTest implements UserTest { + @InjectMocks private UpdateJobGroupBusiness updateJobGroupBusiness; + + @Mock private JobGroupProvider jobGroupProvider; + + @Test + void 직군_수정_테스트() { + // given + Dto dto = + Dto.builder() + .loginUser(LoginUser.builder().userId(TEST_USER_ID).email(TEST_EMAIL).build()) + .jobGroup(TEST_JOB_GROUP) + .build(); + + // when + updateJobGroupBusiness.execute(dto); + + // then + verify(jobGroupProvider).updateJobGroup(any(), any(), any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/DeleteQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/DeleteQuestionBusinessTest.java new file mode 100644 index 0000000..d7d99af --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/DeleteQuestionBusinessTest.java @@ -0,0 +1,33 @@ +package com.sirius.spurt.service.business.question; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.service.business.question.DeleteQuestionBusiness.Dto; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.test.QuestionTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class DeleteQuestionBusinessTest implements QuestionTest { + @InjectMocks private DeleteQuestionBusiness deleteQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + + @Test + void 질문_삭제_테스트() { + // given + Dto dto = + Dto.builder().questionId(String.valueOf(TEST_QUESTION_ID)).userId(TEST_USER_ID).build(); + + // when + deleteQuestionBusiness.execute(dto); + + // then + verify(questionProvider).deleteQuestion(any(), any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/GetQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/GetQuestionBusinessTest.java new file mode 100644 index 0000000..0f5665d --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/GetQuestionBusinessTest.java @@ -0,0 +1,119 @@ +package com.sirius.spurt.service.business.question; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.question.GetQuestionBusiness.Dto; +import com.sirius.spurt.service.business.question.GetQuestionBusiness.Result; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.store.provider.question.vo.CategoryVo; +import com.sirius.spurt.store.provider.question.vo.KeyWordVo; +import com.sirius.spurt.store.provider.question.vo.QuestionVo; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.KeyWordTest; +import com.sirius.spurt.test.QuestionTest; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class GetQuestionBusinessTest implements QuestionTest, KeyWordTest, CategoryTest { + @InjectMocks private GetQuestionBusiness getQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + @Mock private ExperienceProvider experienceProvider; + + private KeyWordVo keyWordVo; + private CategoryVo categoryVo; + + @BeforeEach + void init() { + keyWordVo = + KeyWordVo.builder() + .keyWordId(TEST_KEY_WORD_ID) + .questionId(TEST_QUESTION_ID) + .keyWord(TEST_KEY_WORD_VALUE) + .build(); + categoryVo = + CategoryVo.builder() + .categoryId(TEST_CATEGORY_ID) + .questionId(TEST_QUESTION_ID) + .category(TEST_CATEGORY) + .build(); + } + + @Nested + class 질문_단건_조회 { + @Test + void 질문_단건_조회_성공_테스트() { + // given + Dto dto = Dto.builder().questionId(TEST_QUESTION_ID).userId(TEST_USER_ID).build(); + QuestionVo questionVo = + QuestionVo.builder() + .questionId(TEST_QUESTION_ID) + .userId(TEST_USER_ID) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .experienceId(TEST_EXPERIENCE_ID) + .createTimestamp(Timestamp.valueOf(LocalDateTime.now())) + .keyWordList(List.of(keyWordVo)) + .categoryList(List.of(categoryVo)) + .build(); + when(questionProvider.getQuestion(any())).thenReturn(questionVo); + when(experienceProvider.getQuestionExperienceTitle(any(), any())) + .thenReturn(TEST_EXPERIENCE_TITLE); + + // when + Result result = getQuestionBusiness.execute(dto); + + // then + verify(questionProvider).getQuestion(any()); + verify(experienceProvider).getQuestionExperienceTitle(any(), any()); + assertThat(result.getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(result.getMainText()).isEqualTo(TEST_QUESTION_MAIN_TEXT); + assertThat(result.getExperienceId()).isEqualTo(TEST_EXPERIENCE_ID); + assertThat(result.getKeyWordList()).contains(TEST_KEY_WORD_VALUE); + assertThat(result.getCategoryList()).contains(TEST_CATEGORY); + } + + @Test + void 질문_단건_조회_성공_테스트_경험없음() { + // given + Dto dto = Dto.builder().questionId(TEST_QUESTION_ID).userId(TEST_USER_ID).build(); + QuestionVo questionVo = + QuestionVo.builder() + .questionId(TEST_QUESTION_ID) + .userId(TEST_USER_ID) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .createTimestamp(Timestamp.valueOf(LocalDateTime.now())) + .keyWordList(List.of(keyWordVo)) + .categoryList(List.of(categoryVo)) + .build(); + when(questionProvider.getQuestion(any())).thenReturn(questionVo); + + // when + Result result = getQuestionBusiness.execute(dto); + + // then + verify(questionProvider).getQuestion(any()); + verify(experienceProvider, times(0)).getQuestionExperienceTitle(any(), any()); + assertThat(result.getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(result.getMainText()).isEqualTo(TEST_QUESTION_MAIN_TEXT); + assertThat(result.getExperienceId()).isEqualTo(null); + assertThat(result.getKeyWordList()).contains(TEST_KEY_WORD_VALUE); + assertThat(result.getCategoryList()).contains(TEST_CATEGORY); + } + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/PutPinQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/PutPinQuestionBusinessTest.java new file mode 100644 index 0000000..0eca19b --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/PutPinQuestionBusinessTest.java @@ -0,0 +1,37 @@ +package com.sirius.spurt.service.business.question; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.service.business.question.PutPinQuestionBusiness.Dto; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.test.QuestionTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class PutPinQuestionBusinessTest implements QuestionTest { + @InjectMocks private PutPinQuestionBusiness putPinQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + + @Test + void 질문_핀_수정_테스트() { + // given + Dto dto = + Dto.builder() + .questionId(String.valueOf(TEST_QUESTION_ID)) + .userId(TEST_USER_ID) + .pinIndicator(Boolean.TRUE) + .build(); + + // when + putPinQuestionBusiness.execute(dto); + + // then + verify(questionProvider).putPinQuestion(any(), any(), any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/PutQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/PutQuestionBusinessTest.java new file mode 100644 index 0000000..0aa13d7 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/PutQuestionBusinessTest.java @@ -0,0 +1,78 @@ +package com.sirius.spurt.service.business.question; + +import static com.sirius.spurt.common.meta.ResultCode.NOT_ALL_CATEGORY; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.service.business.question.PutQuestionBusiness.Dto; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.KeyWordTest; +import com.sirius.spurt.test.QuestionTest; +import java.util.List; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class PutQuestionBusinessTest implements QuestionTest, KeyWordTest, CategoryTest { + @InjectMocks private PutQuestionBusiness putQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + + @Nested + class 질문_수정 { + @Test + void 질문_수정_성공_테스트() { + // given + Dto dto = + Dto.builder() + .questionId(String.valueOf(TEST_QUESTION_ID)) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .keyWordList(List.of(TEST_KEY_WORD_VALUE, TEST_ANOTHER_KEY_WORD_VALUE)) + .categoryList(List.of(TEST_CATEGORY)) + .userId(TEST_USER_ID) + .build(); + + // when + putQuestionBusiness.execute(dto); + + // then + verify(questionProvider).putQuestion(any(), any(), any(), any(), any(), any()); + } + + @Test + void 질문_수정_실패_테스트() { + // given + Dto dto = + Dto.builder() + .questionId(String.valueOf(TEST_QUESTION_ID)) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .keyWordList(List.of(TEST_KEY_WORD_VALUE, TEST_ANOTHER_KEY_WORD_VALUE)) + .categoryList(List.of(TEST_ANOTHER_CATEGORY)) + .userId(TEST_USER_ID) + .build(); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + putQuestionBusiness.execute(dto); + }); + + // then + verify(questionProvider, times(0)).putQuestion(any(), any(), any(), any(), any(), any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_ALL_CATEGORY); + } + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/RandomQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/RandomQuestionBusinessTest.java new file mode 100644 index 0000000..5ba9077 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/RandomQuestionBusinessTest.java @@ -0,0 +1,97 @@ +package com.sirius.spurt.service.business.question; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.isNotNull; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.question.RandomQuestionBusiness.Dto; +import com.sirius.spurt.service.business.question.RandomQuestionBusiness.Result; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.store.provider.question.vo.QuestionVo; +import com.sirius.spurt.store.provider.question.vo.QuestionVoList; +import com.sirius.spurt.store.provider.user.UserProvider; +import com.sirius.spurt.store.provider.user.vo.UserVo; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.QuestionTest; +import java.util.List; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class RandomQuestionBusinessTest implements QuestionTest, CategoryTest { + @InjectMocks private RandomQuestionBusiness randomQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + @Mock private UserProvider userProvider; + + @Nested + class 질문_랜덤_조회 { + @Test + void 질문_랜덤_조회_성공_테스트_로그인() { + // given + Dto dto = Dto.builder().userId(TEST_USER_ID).count(1).category(TEST_CATEGORY).build(); + UserVo userVo = UserVo.builder().userId(TEST_USER_ID).jobGroup(TEST_JOB_GROUP).build(); + QuestionVo questionVo = + QuestionVo.builder() + .questionId(TEST_QUESTION_ID) + .userId(TEST_USER_ID) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .jobGroup(TEST_JOB_GROUP) + .build(); + QuestionVoList questionVoList = + QuestionVoList.builder().questions(List.of(questionVo)).build(); + when(userProvider.getUserInfo(any())).thenReturn(userVo); + when(questionProvider.randomQuestion(isNotNull(), isNotNull(), any(), any())) + .thenReturn(questionVoList); + + // when + Result result = randomQuestionBusiness.execute(dto); + + // then + verify(questionProvider, times(0)).randomQuestion(isNull(), isNull(), any(), any()); + verify(userProvider).getUserInfo(any()); + verify(questionProvider).randomQuestion(isNotNull(), isNotNull(), any(), any()); + assertThat(result.getQuestions().size()).isEqualTo(1); + assertThat(result.getQuestions().get(0).getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(result.getQuestions().get(0).getJobGroup()).isEqualTo(TEST_JOB_GROUP); + } + + @Test + void 질문_랜덤_조회_성공_테스트_비로그인() { + // given + Dto dto = Dto.builder().userId(null).count(1).category(TEST_CATEGORY).build(); + QuestionVo questionVo = + QuestionVo.builder() + .questionId(TEST_ANOTHER_QUESTION_ID) + .userId(TEST_ANOTHER_USER_ID) + .subject(TEST_ANOTHER_QUESTION_SUBJECT) + .mainText(TEST_ANOTHER_QUESTION_MAIN_TEXT) + .jobGroup(TEST_JOB_GROUP) + .build(); + QuestionVoList questionVoList = + QuestionVoList.builder().questions(List.of(questionVo)).build(); + when(questionProvider.randomQuestion(isNull(), isNull(), any(), any())) + .thenReturn(questionVoList); + + // when + Result result = randomQuestionBusiness.execute(dto); + + // then + verify(questionProvider).randomQuestion(isNull(), isNull(), any(), any()); + verify(userProvider, times(0)).getUserInfo(any()); + verify(questionProvider, times(0)).randomQuestion(isNotNull(), isNotNull(), any(), any()); + assertThat(result.getQuestions().size()).isEqualTo(1); + assertThat(result.getQuestions().get(0).getSubject()) + .isEqualTo(TEST_ANOTHER_QUESTION_SUBJECT); + } + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/RetrieveQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/RetrieveQuestionBusinessTest.java new file mode 100644 index 0000000..01641c9 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/RetrieveQuestionBusinessTest.java @@ -0,0 +1,93 @@ +package com.sirius.spurt.service.business.question; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.question.RetrieveQuestionBusiness.Dto; +import com.sirius.spurt.service.business.question.RetrieveQuestionBusiness.Result; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.store.provider.question.vo.CategoryVo; +import com.sirius.spurt.store.provider.question.vo.KeyWordVo; +import com.sirius.spurt.store.provider.question.vo.QuestionVo; +import com.sirius.spurt.store.provider.question.vo.QuestionVoList; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.KeyWordTest; +import com.sirius.spurt.test.QuestionTest; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.PageRequest; + +@ExtendWith(MockitoExtension.class) +class RetrieveQuestionBusinessTest implements QuestionTest, CategoryTest, KeyWordTest { + @InjectMocks private RetrieveQuestionBusiness retrieveQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + + @Test + void 질문_조회_테스트() { + // given + Dto dto = + Dto.builder() + .userId(TEST_USER_ID) + .subject(TEST_QUESTION_SUBJECT) + .jobGroup(TEST_JOB_GROUP) + .category(TEST_CATEGORY) + .pinIndicator(null) + .myQuestionIndicator(Boolean.TRUE) + .size(String.valueOf(10)) + .offset(0) + .build(); + KeyWordVo keyWordVo = + KeyWordVo.builder() + .keyWordId(TEST_KEY_WORD_ID) + .questionId(TEST_QUESTION_ID) + .keyWord(TEST_KEY_WORD_VALUE) + .build(); + CategoryVo categoryVo = + CategoryVo.builder() + .categoryId(TEST_CATEGORY_ID) + .questionId(TEST_QUESTION_ID) + .category(TEST_CATEGORY) + .build(); + PageRequest pageRequest = PageRequest.of(0, 10); + QuestionVo questionVo = + QuestionVo.builder() + .questionId(TEST_QUESTION_ID) + .userId(TEST_USER_ID) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .jobGroup(TEST_JOB_GROUP) + .createTimestamp(Timestamp.valueOf(LocalDateTime.now())) + .keyWordList(List.of(keyWordVo)) + .categoryList(List.of(categoryVo)) + .build(); + QuestionVoList questionVoList = + QuestionVoList.builder() + .questions(List.of(questionVo)) + .pageable(pageRequest) + .totalPage(1) + .totalCount(1L) + .build(); + when(questionProvider.searchQuestion(any(), any(), any(), any(), any(), any(), any())) + .thenReturn(questionVoList); + + // when + Result result = retrieveQuestionBusiness.execute(dto); + + // then + verify(questionProvider).searchQuestion(any(), any(), any(), any(), any(), any(), any()); + assertThat(result.getQuestions().size()).isEqualTo(1); + assertThat(result.getQuestions().get(0).getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(result.getQuestions().get(0).getMainText()).isEqualTo(TEST_QUESTION_MAIN_TEXT); + assertThat(result.getMeta().getTotalCount()).isEqualTo(1L); + assertThat(result.getMeta().getTotalPage()).isEqualTo(1); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/question/SaveQuestionBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/question/SaveQuestionBusinessTest.java new file mode 100644 index 0000000..3b27eea --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/question/SaveQuestionBusinessTest.java @@ -0,0 +1,83 @@ +package com.sirius.spurt.service.business.question; + +import static com.sirius.spurt.common.meta.ResultCode.NOT_ALL_CATEGORY; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.service.business.question.SaveQuestionBusiness.Dto; +import com.sirius.spurt.service.business.question.SaveQuestionBusiness.Result; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.store.provider.question.vo.QuestionVo; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.KeyWordTest; +import com.sirius.spurt.test.QuestionTest; +import java.util.List; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class SaveQuestionBusinessTest implements QuestionTest, CategoryTest, KeyWordTest { + @InjectMocks private SaveQuestionBusiness saveQuestionBusiness; + + @Mock private QuestionProvider questionProvider; + + @Nested + class 질문_저장 { + @Test + void 질문_저장_성공_테스트() { + // given + Dto dto = + Dto.builder() + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .keyWordList(List.of(TEST_KEY_WORD_VALUE, TEST_ANOTHER_KEY_WORD_VALUE)) + .categoryList(List.of(TEST_CATEGORY)) + .userId(TEST_USER_ID) + .build(); + QuestionVo questionVo = QuestionVo.builder().questionId(TEST_QUESTION_ID).build(); + when(questionProvider.saveQuestion(any(), any(), any(), any(), any(), any())) + .thenReturn(questionVo); + + // when + Result result = saveQuestionBusiness.execute(dto); + + // then + verify(questionProvider).saveQuestion(any(), any(), any(), any(), any(), any()); + assertThat(result.getQuestionId()).isEqualTo(TEST_QUESTION_ID); + } + + @Test + void 질문_저장_실패_테스트() { + // given + Dto dto = + Dto.builder() + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .keyWordList(List.of(TEST_KEY_WORD_VALUE, TEST_ANOTHER_KEY_WORD_VALUE)) + .categoryList(List.of(TEST_ANOTHER_CATEGORY)) + .userId(TEST_USER_ID) + .build(); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + saveQuestionBusiness.execute(dto); + }); + + // then + verify(questionProvider, times(0)).saveQuestion(any(), any(), any(), any(), any(), any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_ALL_CATEGORY); + } + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/user/CheckUserExistsBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/user/CheckUserExistsBusinessTest.java new file mode 100644 index 0000000..185f0fd --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/user/CheckUserExistsBusinessTest.java @@ -0,0 +1,37 @@ +package com.sirius.spurt.service.business.user; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.user.CheckUserExistsBusiness.Dto; +import com.sirius.spurt.service.business.user.CheckUserExistsBusiness.Result; +import com.sirius.spurt.store.provider.user.UserProvider; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CheckUserExistsBusinessTest implements UserTest { + @InjectMocks private CheckUserExistsBusiness checkUserExistsBusiness; + + @Mock private UserProvider userProvider; + + @Test + void 유저_존재_확인_테스트() { + // given + Dto dto = Dto.builder().userId(TEST_USER_ID).build(); + when(userProvider.checkUserExists(any())).thenReturn(true); + + // when + Result result = checkUserExistsBusiness.execute(dto); + + // then + verify(userProvider).checkUserExists(any()); + assertThat(result.isUserExists()).isEqualTo(true); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/user/CheckUserHasPinedBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/user/CheckUserHasPinedBusinessTest.java new file mode 100644 index 0000000..af17bbf --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/user/CheckUserHasPinedBusinessTest.java @@ -0,0 +1,37 @@ +package com.sirius.spurt.service.business.user; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.user.CheckUserHasPinedBusiness.Dto; +import com.sirius.spurt.service.business.user.CheckUserHasPinedBusiness.Result; +import com.sirius.spurt.store.provider.user.UserProvider; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CheckUserHasPinedBusinessTest implements UserTest { + @InjectMocks private CheckUserHasPinedBusiness checkUserHasPinedBusiness; + + @Mock private UserProvider userProvider; + + @Test + void 유저_첫핀_확인_테스트() { + // given + Dto dto = Dto.builder().userId(TEST_USER_ID).build(); + when(userProvider.checkHasPined(any())).thenReturn(true); + + // when + Result result = checkUserHasPinedBusiness.execute(dto); + + // then + verify(userProvider).checkHasPined(any()); + assertThat(result.isHasPined()).isEqualTo(true); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/user/CheckUserHasPostedBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/user/CheckUserHasPostedBusinessTest.java new file mode 100644 index 0000000..e59923a --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/user/CheckUserHasPostedBusinessTest.java @@ -0,0 +1,37 @@ +package com.sirius.spurt.service.business.user; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.user.CheckUserHasPostedBusiness.Dto; +import com.sirius.spurt.service.business.user.CheckUserHasPostedBusiness.Result; +import com.sirius.spurt.store.provider.user.UserProvider; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class CheckUserHasPostedBusinessTest implements UserTest { + @InjectMocks private CheckUserHasPostedBusiness checkUserHasPostedBusiness; + + @Mock private UserProvider userProvider; + + @Test + void 유저_첫질문_확인_테스트() { + // given + Dto dto = Dto.builder().userId(TEST_USER_ID).build(); + when(userProvider.checkHasPosted(any())).thenReturn(true); + + // when + Result result = checkUserHasPostedBusiness.execute(dto); + + // then + verify(userProvider).checkHasPosted(any()); + assertThat(result.isHasPosted()).isEqualTo(true); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/user/DeleteUserBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/user/DeleteUserBusinessTest.java new file mode 100644 index 0000000..dd8dc7e --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/user/DeleteUserBusinessTest.java @@ -0,0 +1,38 @@ +package com.sirius.spurt.service.business.user; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; + +import com.sirius.spurt.service.business.user.DeleteUserBusiness.Dto; +import com.sirius.spurt.store.provider.experience.ExperienceProvider; +import com.sirius.spurt.store.provider.question.QuestionProvider; +import com.sirius.spurt.store.provider.user.UserProvider; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class DeleteUserBusinessTest implements UserTest { + @InjectMocks private DeleteUserBusiness deleteUserBusiness; + + @Mock private UserProvider userProvider; + @Mock private ExperienceProvider experienceProvider; + @Mock private QuestionProvider questionProvider; + + @Test + void 유저_삭제_테스트() { + // given + Dto dto = Dto.builder().userId(TEST_USER_ID).build(); + + // when + deleteUserBusiness.execute(dto); + + // then + verify(experienceProvider).deleteExperienceByUser(any()); + verify(questionProvider).deleteQuestionByUser(any()); + verify(userProvider).deleteUser(any()); + } +} diff --git a/src/test/java/com/sirius/spurt/service/business/user/UserInfoBusinessTest.java b/src/test/java/com/sirius/spurt/service/business/user/UserInfoBusinessTest.java new file mode 100644 index 0000000..6866c04 --- /dev/null +++ b/src/test/java/com/sirius/spurt/service/business/user/UserInfoBusinessTest.java @@ -0,0 +1,40 @@ +package com.sirius.spurt.service.business.user; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.service.business.user.UserInfoBusiness.Dto; +import com.sirius.spurt.service.business.user.UserInfoBusiness.Result; +import com.sirius.spurt.store.provider.user.UserProvider; +import com.sirius.spurt.store.provider.user.vo.UserVo; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class UserInfoBusinessTest implements UserTest { + @InjectMocks private UserInfoBusiness userInfoBusiness; + + @Mock private UserProvider userProvider; + + @Test + void 유저_정보_조회_테스트() { + // given + Dto dto = Dto.builder().userId(TEST_USER_ID).build(); + UserVo userVo = UserVo.builder().userId(TEST_USER_ID).jobGroup(TEST_JOB_GROUP).build(); + when(userProvider.getUserInfo(any())).thenReturn(userVo); + + // when + Result result = userInfoBusiness.execute(dto); + + // then + verify(userProvider).getUserInfo(any()); + assertThat(result.getUserId()).isEqualTo(TEST_USER_ID); + assertThat(result.getJobGroup()).isEqualTo(TEST_JOB_GROUP); + } +} diff --git a/src/test/java/com/sirius/spurt/store/provider/auth/impl/AuthProviderImplTest.java b/src/test/java/com/sirius/spurt/store/provider/auth/impl/AuthProviderImplTest.java new file mode 100644 index 0000000..06b130d --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/provider/auth/impl/AuthProviderImplTest.java @@ -0,0 +1,39 @@ +package com.sirius.spurt.store.provider.auth.impl; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.store.provider.auth.vo.AuthVo; +import com.sirius.spurt.store.repository.resttemplate.auth.AuthRepository; +import com.sirius.spurt.store.repository.resttemplate.auth.playload.UserInfoPayload; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class AuthProviderImplTest implements UserTest { + @InjectMocks private AuthProviderImpl authProvider; + + @Mock private AuthRepository authRepository; + + @Test + void 유저_정보_조회_테스트() { + // given + UserInfoPayload userInfo = + UserInfoPayload.builder().userId(TEST_USER_ID).email(TEST_EMAIL).build(); + when(authRepository.getUserInfo(any())).thenReturn(userInfo); + + // when + AuthVo authVo = authProvider.getUserId("accessToken"); + + // then + verify(authRepository).getUserInfo(any()); + assertThat(authVo.getUserId()).isEqualTo(userInfo.getUserId()); + assertThat(authVo.getEmail()).isEqualTo(userInfo.getEmail()); + } +} diff --git a/src/test/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImplTest.java b/src/test/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImplTest.java new file mode 100644 index 0000000..0255ec9 --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/provider/experience/impl/ExperienceProviderImplTest.java @@ -0,0 +1,363 @@ +package com.sirius.spurt.store.provider.experience.impl; + +import static com.sirius.spurt.common.meta.ResultCode.EXPERIENCE_THREE_SECONDS; +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXPERIENCE_OWNER; +import static com.sirius.spurt.common.meta.ResultCode.NO_CONTENT; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.provider.experience.vo.ExperienceVo; +import com.sirius.spurt.store.provider.experience.vo.ExperienceVoList; +import com.sirius.spurt.store.repository.database.entity.BaseEntity; +import com.sirius.spurt.store.repository.database.repository.ExperienceRepository; +import com.sirius.spurt.store.repository.database.repository.UserRepository; +import com.sirius.spurt.test.ExperienceTest; +import java.lang.reflect.Field; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ExperienceProviderImplTest implements ExperienceTest { + @InjectMocks private ExperienceProviderImpl experienceProvider; + + @Mock private ExperienceRepository experienceRepository; + @Mock private UserRepository userRepository; + + @Nested + class 경험_저장 { + @Test + void 경험_저장_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(experienceRepository.findTopByUserEntityOrderByCreateTimestampDesc(any())) + .thenReturn(null); + when(experienceRepository.save(any())).thenReturn(TEST_EXPERIENCE); + + // when + ExperienceVo experienceVo = + experienceProvider.saveExperience( + TEST_EXPERIENCE_TITLE, + TEST_EXPERIENCE_CONTENT, + TEST_EXPERIENCE_START_DATE_STRING, + TEST_EXPERIENCE_END_DATE_STRING, + TEST_EXPERIENCE_LINK, + TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + verify(experienceRepository).findTopByUserEntityOrderByCreateTimestampDesc(any()); + verify(experienceRepository).save(any()); + assertThat(experienceVo.getTitle()).isEqualTo(TEST_EXPERIENCE_TITLE); + assertThat(experienceVo.getContent()).isEqualTo(TEST_EXPERIENCE_CONTENT); + assertThat(experienceVo.getStartDate()).isEqualTo(TEST_EXPERIENCE_START_DATE_STRING); + assertThat(experienceVo.getEndDate()).isEqualTo(TEST_EXPERIENCE_END_DATE_STRING); + assertThat(experienceVo.getLink()).isEqualTo(TEST_EXPERIENCE_LINK); + } + + @Test + void 경험_저장_실패_테스트_유저없음() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.saveExperience( + TEST_EXPERIENCE_TITLE, + TEST_EXPERIENCE_CONTENT, + TEST_EXPERIENCE_START_DATE_STRING, + TEST_EXPERIENCE_END_DATE_STRING, + TEST_EXPERIENCE_LINK, + TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(experienceRepository, times(0)).findTopByUserEntityOrderByCreateTimestampDesc(any()); + verify(experienceRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + + @Test + void 경험_저장_실패_테스트_3초등록() { + // given + try { + Field field = BaseEntity.class.getDeclaredField("createTimestamp"); + field.setAccessible(true); + field.set(TEST_EXPERIENCE, Timestamp.valueOf(LocalDateTime.now())); + } catch (Exception e) { + e.printStackTrace(); + } + + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(experienceRepository.findTopByUserEntityOrderByCreateTimestampDesc(any())) + .thenReturn(TEST_EXPERIENCE); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.saveExperience( + TEST_EXPERIENCE_TITLE, + TEST_EXPERIENCE_CONTENT, + TEST_EXPERIENCE_START_DATE_STRING, + TEST_EXPERIENCE_END_DATE_STRING, + TEST_EXPERIENCE_LINK, + TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(experienceRepository).findTopByUserEntityOrderByCreateTimestampDesc(any()); + verify(experienceRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(EXPERIENCE_THREE_SECONDS); + } + } + + @Nested + class 경험_수정 { + @Test + void 경험_수정_성공_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(TEST_EXPERIENCE); + + // when + experienceProvider.updateExperience( + TEST_EXPERIENCE_ID, + TEST_EXPERIENCE_TITLE, + TEST_EXPERIENCE_CONTENT, + TEST_EXPERIENCE_START_DATE_STRING, + TEST_EXPERIENCE_END_DATE_STRING, + TEST_EXPERIENCE_LINK, + TEST_USER_ID); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + verify(experienceRepository).save(any()); + } + + @Test + void 경험_수정_실패_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.updateExperience( + TEST_EXPERIENCE_ID, + TEST_EXPERIENCE_TITLE, + TEST_EXPERIENCE_CONTENT, + TEST_EXPERIENCE_START_DATE_STRING, + TEST_EXPERIENCE_END_DATE_STRING, + TEST_EXPERIENCE_LINK, + TEST_USER_ID); + }); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + verify(experienceRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXPERIENCE_OWNER); + } + } + + @Nested + class 경험_삭제 { + @Test + void 경험_삭제_성공_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(TEST_EXPERIENCE); + + // when + experienceProvider.deleteExperience(TEST_EXPERIENCE_ID, TEST_USER_ID); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + verify(experienceRepository).deleteById(any()); + } + + @Test + void 경험_삭제_실패_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.deleteExperience(TEST_EXPERIENCE_ID, TEST_USER_ID); + }); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + verify(experienceRepository, times(0)).deleteById(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXPERIENCE_OWNER); + } + } + + @Nested + class 경험_전체_조회 { + @Test + void 경험_전체_조회_성공_테스트() { + // given + when(experienceRepository.findByUserEntityUserId(any())) + .thenReturn(List.of(TEST_EXPERIENCE, TEST_ANOTHER_EXPERIENCE)); + + // when + ExperienceVoList experienceVoList = experienceProvider.getAllExperience(TEST_USER_ID); + + // then + verify(experienceRepository).findByUserEntityUserId(any()); + assertThat(experienceVoList.getExperienceVoList().size()).isEqualTo(2); + assertThat(experienceVoList.getExperienceVoList().get(0).getTitle()) + .isEqualTo(TEST_EXPERIENCE_TITLE); + assertThat(experienceVoList.getExperienceVoList().get(0).getContent()) + .isEqualTo(TEST_EXPERIENCE_CONTENT); + assertThat(experienceVoList.getExperienceVoList().get(0).getStartDate()) + .isEqualTo(TEST_EXPERIENCE_START_DATE_STRING); + assertThat(experienceVoList.getExperienceVoList().get(0).getEndDate()) + .isEqualTo(TEST_EXPERIENCE_END_DATE_STRING); + assertThat(experienceVoList.getExperienceVoList().get(0).getLink()) + .isEqualTo(TEST_EXPERIENCE_LINK); + assertThat(experienceVoList.getExperienceVoList().get(1).getTitle()) + .isEqualTo(TEST_ANOTHER_EXPERIENCE_TITLE); + assertThat(experienceVoList.getExperienceVoList().get(1).getContent()) + .isEqualTo(TEST_ANOTHER_EXPERIENCE_CONTENT); + assertThat(experienceVoList.getExperienceVoList().get(1).getLink()) + .isEqualTo(TEST_ANOTHER_EXPERIENCE_LINK); + assertThat(experienceVoList.getTotalCount()).isEqualTo(2); + } + + @Test + void 경험_전체_조회_실패_테스트() { + // given + when(experienceRepository.findByUserEntityUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.getAllExperience(TEST_USER_ID); + }); + + // then + verify(experienceRepository).findByUserEntityUserId(any()); + assertThat(exception.getResultCode()).isEqualTo(NO_CONTENT); + } + } + + @Nested + class 경험_조회 { + @Test + void 경험_조회_성공_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(TEST_EXPERIENCE); + + // when + ExperienceVo experienceVo = + experienceProvider.getExperience(TEST_EXPERIENCE_ID, TEST_USER_ID); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + assertThat(experienceVo.getTitle()).isEqualTo(TEST_EXPERIENCE_TITLE); + assertThat(experienceVo.getContent()).isEqualTo(TEST_EXPERIENCE_CONTENT); + assertThat(experienceVo.getStartDate()).isEqualTo(TEST_EXPERIENCE_START_DATE_STRING); + assertThat(experienceVo.getEndDate()).isEqualTo(TEST_EXPERIENCE_END_DATE_STRING); + assertThat(experienceVo.getLink()).isEqualTo(TEST_EXPERIENCE_LINK); + } + + @Test + void 경험_조회_실패_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.getExperience(TEST_EXPERIENCE_ID, TEST_USER_ID); + }); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + assertThat(exception.getResultCode()).isEqualTo(NO_CONTENT); + } + } + + @Test + void 경험_제목_조회_테스트() { + // given + when(experienceRepository.findByExperienceIdAndUserEntityUserId(any(), any())) + .thenReturn(TEST_EXPERIENCE); + + // when + String experienceTitle = + experienceProvider.getQuestionExperienceTitle(TEST_EXPERIENCE_ID, TEST_USER_ID); + + // then + verify(experienceRepository).findByExperienceIdAndUserEntityUserId(any(), any()); + assertThat(experienceTitle).isEqualTo(TEST_EXPERIENCE_TITLE); + } + + @Nested + class 경험_삭제_user { + @Test + void 경험_삭제_user_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + + // when + experienceProvider.deleteExperienceByUser(TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + verify(experienceRepository).deleteByUserEntity(any()); + } + + @Test + void 경험_삭제_user_실패_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + experienceProvider.deleteExperienceByUser(TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(experienceRepository, times(0)).deleteByUserEntity(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + } +} diff --git a/src/test/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImplTest.java b/src/test/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImplTest.java new file mode 100644 index 0000000..e23ee22 --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/provider/jobgroup/impl/JobGroupProviderImplTest.java @@ -0,0 +1,70 @@ +package com.sirius.spurt.store.provider.jobgroup.impl; + +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.repository.database.repository.UserRepository; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class JobGroupProviderImplTest implements UserTest { + @InjectMocks private JobGroupProviderImpl jobGroupProvider; + + @Mock private UserRepository userRepository; + + @Test + void 직군_저장_테스트() { + // given + + // when + jobGroupProvider.saveJobGroup(TEST_USER_ID, TEST_EMAIL, TEST_JOB_GROUP); + + // then + verify(userRepository).save(any()); + } + + @Nested + class 직군_수정 { + @Test + void 직군_수정_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + + // when + jobGroupProvider.updateJobGroup(TEST_USER_ID, TEST_EMAIL, TEST_JOB_GROUP); + + // then + verify(userRepository).findByUserId(any()); + verify(userRepository).save(any()); + } + + @Test + void 직군_수정_실패_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + jobGroupProvider.updateJobGroup(TEST_USER_ID, TEST_EMAIL, TEST_JOB_GROUP); + }); + + // then + verify(userRepository).findByUserId(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + } +} diff --git a/src/test/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImplTest.java b/src/test/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImplTest.java new file mode 100644 index 0000000..9a50b5d --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/provider/question/impl/QuestionProviderImplTest.java @@ -0,0 +1,433 @@ +package com.sirius.spurt.store.provider.question.impl; + +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; +import static com.sirius.spurt.common.meta.ResultCode.NOT_QUESTION_OWNER; +import static com.sirius.spurt.common.meta.ResultCode.QUESTION_THREE_SECONDS; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.provider.question.vo.QuestionVo; +import com.sirius.spurt.store.provider.question.vo.QuestionVoList; +import com.sirius.spurt.store.repository.database.entity.BaseEntity; +import com.sirius.spurt.store.repository.database.entity.QuestionEntity; +import com.sirius.spurt.store.repository.database.repository.QuestionRepository; +import com.sirius.spurt.store.repository.database.repository.UserRepository; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.KeyWordTest; +import com.sirius.spurt.test.QuestionTest; +import java.lang.reflect.Field; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.util.List; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; + +@ExtendWith(MockitoExtension.class) +class QuestionProviderImplTest implements QuestionTest, CategoryTest, KeyWordTest { + @InjectMocks private QuestionProviderImpl questionProvider; + + @Mock private QuestionRepository questionRepository; + @Mock private UserRepository userRepository; + + @Nested + class 질문_핀_수정 { + @Test + void 질문_핀_수정_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(TEST_QUESTION); + + // when + questionProvider.putPinQuestion( + String.valueOf(TEST_QUESTION_ID), TEST_USER_ID, Boolean.FALSE); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(userRepository, times(0)).save(any()); + verify(questionRepository).save(any()); + } + + @Test + void 질문_핀_수정_성공_테스트_첫핀() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_ANOTHER_USER); + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(TEST_QUESTION); + + // when + questionProvider.putPinQuestion( + String.valueOf(TEST_QUESTION_ID), TEST_ANOTHER_USER_ID, Boolean.FALSE); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(userRepository).save(any()); + verify(questionRepository).save(any()); + } + + @Test + void 질문_핀_수정_실패_테스트_유저없음() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.putPinQuestion( + String.valueOf(TEST_QUESTION_ID), TEST_USER_ID, Boolean.FALSE); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository, times(0)).findByQuestionIdAndUserId(any(), any()); + verify(userRepository, times(0)).save(any()); + verify(questionRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + + @Test + void 질문_핀_수정_실패_테스트_질문없음() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.putPinQuestion( + String.valueOf(TEST_QUESTION_ID), TEST_USER_ID, Boolean.FALSE); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(userRepository, times(0)).save(any()); + verify(questionRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_QUESTION_OWNER); + } + } + + @Test + void 랜덤_질문_조회_테스트() { + // given + when(questionRepository.randomQuestion(any(), any(), any(), any())) + .thenReturn(List.of(TEST_QUESTION, TEST_ANOTHER_QUESTION)); + + // when + QuestionVoList questionVoList = questionProvider.randomQuestion(null, null, 2, null); + + // then + verify(questionRepository).randomQuestion(any(), any(), any(), any()); + assertThat(questionVoList.getQuestions().size()).isEqualTo(2); + assertThat(questionVoList.getQuestions().get(0).getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(questionVoList.getQuestions().get(0).getMainText()) + .isEqualTo(TEST_QUESTION_MAIN_TEXT); + assertThat(questionVoList.getQuestions().get(1).getSubject()) + .isEqualTo(TEST_ANOTHER_QUESTION_SUBJECT); + assertThat(questionVoList.getQuestions().get(1).getMainText()) + .isEqualTo(TEST_ANOTHER_QUESTION_MAIN_TEXT); + } + + @Nested + class 질문_삭제 { + @Test + void 질문_삭제_성공_테스트() { + // given + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(TEST_QUESTION); + + // when + questionProvider.deleteQuestion(TEST_USER_ID, TEST_QUESTION_ID); + + // then + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(questionRepository).delete(any()); + } + + @Test + void 질문_삭제_실패_테스트() { + // given + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.deleteQuestion(TEST_USER_ID, TEST_QUESTION_ID); + }); + + // then + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(questionRepository, times(0)).deleteByUserId(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_QUESTION_OWNER); + } + } + + @Test + void 질문_페이지_조회_테스트() { + // given + PageRequest pageRequest = PageRequest.of(0, 1); + Page pages = new PageImpl<>(List.of(TEST_QUESTION), pageRequest, 1); + when(questionRepository.searchQuestion(any(), any(), any(), any(), any(), any(), any())) + .thenReturn(pages); + + // when + QuestionVoList questionVoList = + questionProvider.searchQuestion( + TEST_QUESTION_SUBJECT, + TEST_QUESTION_JOB_GROUP, + TEST_CATEGORY, + TEST_PIN_INDICATOR, + Boolean.TRUE, + TEST_USER_ID, + pageRequest); + + // then + verify(questionRepository).searchQuestion(any(), any(), any(), any(), any(), any(), any()); + assertThat(questionVoList.getPageable()).isEqualTo(pages.getPageable()); + assertThat(questionVoList.getTotalPage()).isEqualTo(pages.getTotalPages()); + assertThat(questionVoList.getTotalCount()).isEqualTo(pages.getTotalElements()); + assertThat(questionVoList.getQuestions().size()).isEqualTo(pages.getContent().size()); + assertThat(questionVoList.getQuestions().get(0).getSubject()) + .isEqualTo(pages.getContent().get(0).getSubject()); + assertThat(questionVoList.getQuestions().get(0).getMainText()) + .isEqualTo(pages.getContent().get(0).getMainText()); + } + + @Test + void 질문_단건_조회_테스트() { + // given + when(questionRepository.findByQuestionId(any())).thenReturn(TEST_QUESTION); + + // when + QuestionVo questionVo = questionProvider.getQuestion(TEST_QUESTION_ID); + + // then + verify(questionRepository).findByQuestionId(any()); + assertThat(questionVo.getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(questionVo.getMainText()).isEqualTo(TEST_QUESTION_MAIN_TEXT); + } + + @Nested + class 질문_수정 { + @Test + void 질문_수정_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(TEST_QUESTION); + + // when + questionProvider.putQuestion( + String.valueOf(TEST_QUESTION_ID), + TEST_QUESTION_SUBJECT, + TEST_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(questionRepository).save(any()); + } + + @Test + void 질문_수정_실패_테스트_유저없음() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.putQuestion( + String.valueOf(TEST_QUESTION_ID), + TEST_QUESTION_SUBJECT, + TEST_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository, times(0)).findByQuestionIdAndUserId(any(), any()); + verify(questionRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + + @Test + void 질문_수정_실패_테스트_질문없음() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(questionRepository.findByQuestionIdAndUserId(any(), any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.putQuestion( + String.valueOf(TEST_QUESTION_ID), + TEST_QUESTION_SUBJECT, + TEST_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findByQuestionIdAndUserId(any(), any()); + verify(questionRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_QUESTION_OWNER); + } + } + + @Nested + class 질문_저장 { + @Test + void 질문_저장_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(questionRepository.findTopByUserIdOrderByCreateTimestampDesc(any())).thenReturn(null); + when(questionRepository.save(any())).thenReturn(TEST_QUESTION); + + // when + QuestionVo questionVo = + questionProvider.saveQuestion( + TEST_QUESTION_SUBJECT, + TEST_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_EXPERIENCE_ID, + TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findTopByUserIdOrderByCreateTimestampDesc(any()); + verify(userRepository, times(0)).save(any()); + verify(questionRepository).save(any()); + assertThat(questionVo.getSubject()).isEqualTo(TEST_QUESTION_SUBJECT); + assertThat(questionVo.getMainText()).isEqualTo(TEST_QUESTION_MAIN_TEXT); + } + + @Test + void 질문_저장_성공_테스트_첫질문() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_ANOTHER_USER); + when(questionRepository.findTopByUserIdOrderByCreateTimestampDesc(any())).thenReturn(null); + when(questionRepository.save(any())).thenReturn(TEST_ANOTHER_QUESTION); + + // when + QuestionVo questionVo = + questionProvider.saveQuestion( + TEST_ANOTHER_QUESTION_SUBJECT, + TEST_ANOTHER_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_ANOTHER_EXPERIENCE_ID, + TEST_ANOTHER_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findTopByUserIdOrderByCreateTimestampDesc(any()); + verify(userRepository).save(any()); + verify(questionRepository).save(any()); + assertThat(questionVo.getSubject()).isEqualTo(TEST_ANOTHER_QUESTION_SUBJECT); + assertThat(questionVo.getMainText()).isEqualTo(TEST_ANOTHER_QUESTION_MAIN_TEXT); + } + + @Test + void 질문_저장_실패_테스트_유저없음() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.saveQuestion( + TEST_QUESTION_SUBJECT, + TEST_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_EXPERIENCE_ID, + TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository, times(0)).findTopByUserIdOrderByCreateTimestampDesc(any()); + verify(userRepository, times(0)).save(any()); + verify(questionRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + + @Test + void 질문_저장_실패_테스트_3초등록() { + // given + try { + Field field = BaseEntity.class.getDeclaredField("createTimestamp"); + field.setAccessible(true); + field.set(TEST_QUESTION, Timestamp.valueOf(LocalDateTime.now())); + } catch (Exception e) { + e.printStackTrace(); + } + + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + when(questionRepository.findTopByUserIdOrderByCreateTimestampDesc(any())) + .thenReturn(TEST_QUESTION); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + questionProvider.saveQuestion( + TEST_QUESTION_SUBJECT, + TEST_QUESTION_MAIN_TEXT, + List.of(TEST_KEY_WORD_VALUE), + List.of(TEST_CATEGORY), + TEST_EXPERIENCE_ID, + TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + verify(questionRepository).findTopByUserIdOrderByCreateTimestampDesc(any()); + verify(userRepository, times(0)).save(any()); + verify(questionRepository, times(0)).save(any()); + assertThat(exception.getResultCode()).isEqualTo(QUESTION_THREE_SECONDS); + } + } + + @Test + void 질문_삭제_user_테스트() { + // given + + // when + questionProvider.deleteQuestionByUser(TEST_USER_ID); + + // then + verify(questionRepository).deleteByUserId(any()); + } +} diff --git a/src/test/java/com/sirius/spurt/store/provider/user/impl/UserProviderImplTest.java b/src/test/java/com/sirius/spurt/store/provider/user/impl/UserProviderImplTest.java new file mode 100644 index 0000000..c39fa4a --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/provider/user/impl/UserProviderImplTest.java @@ -0,0 +1,154 @@ +package com.sirius.spurt.store.provider.user.impl; + +import static com.sirius.spurt.common.meta.ResultCode.NOT_EXIST_USER; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.provider.user.vo.UserVo; +import com.sirius.spurt.store.repository.database.repository.UserRepository; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class UserProviderImplTest implements UserTest { + @InjectMocks private UserProviderImpl userProvider; + + @Mock private UserRepository userRepository; + + @Test + void 유저_정보_조회_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + + // when + UserVo userVo = userProvider.getUserInfo(TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + assertThat(userVo.getUserId()).isEqualTo(TEST_USER_ID); + assertThat(userVo.getJobGroup()).isEqualTo(TEST_JOB_GROUP); + } + + @Test + void 유저_존재_확인_테스트() { + // given + when(userRepository.existsByUserId(any())).thenReturn(true); + + // when + boolean isExistsUser = userProvider.checkUserExists(TEST_USER_ID); + + // then + verify(userRepository).existsByUserId(any()); + assertThat(isExistsUser).isEqualTo(true); + } + + @Nested + class 유저_핀_등록_여부 { + @Test + void 핀_등록_여부_확인_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + + // when + boolean hasPined = userProvider.checkHasPined(TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + assertThat(hasPined).isEqualTo(true); + } + + @Test + void 핀_등록_여부_확인_실패_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + userProvider.checkHasPined(TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + } + + @Nested + class 유저_게시글_작성_여부 { + @Test + void 게시글_작성_여부_확인_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + + // when + boolean hasPosted = userProvider.checkHasPosted(TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + assertThat(hasPosted).isEqualTo(true); + } + + @Test + void 게시글_작성_여부_확인_실패_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + userProvider.checkHasPosted(TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + } + + @Nested + class 유저_삭제 { + @Test + void 유저_삭제_성공_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(TEST_USER); + + // when + userProvider.deleteUser(TEST_USER_ID); + + // then + verify(userRepository).findByUserId(any()); + } + + @Test + void 유저_삭제_실패_테스트() { + // given + when(userRepository.findByUserId(any())).thenReturn(null); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + userProvider.deleteUser(TEST_USER_ID); + }); + + // then + verify(userRepository).findByUserId(any()); + assertThat(exception.getResultCode()).isEqualTo(NOT_EXIST_USER); + } + } +} diff --git a/src/test/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepositoryTest.java b/src/test/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepositoryTest.java new file mode 100644 index 0000000..3894199 --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/repository/database/repository/ExperienceRepositoryTest.java @@ -0,0 +1,97 @@ +package com.sirius.spurt.store.repository.database.repository; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.sirius.spurt.common.config.QuerydslConfigTest; +import com.sirius.spurt.store.repository.database.entity.ExperienceEntity; +import com.sirius.spurt.test.ExperienceTest; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +@DataJpaTest +@ActiveProfiles("test") +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Import(QuerydslConfigTest.class) +class ExperienceRepositoryTest implements ExperienceTest { + @Autowired private ExperienceRepository experienceRepository; + @Autowired private UserRepository userRepository; + + @Test + void 경험_조회_experienceId_userId_테스트() { + // given + userRepository.save(TEST_USER); + ExperienceEntity savedExperience = experienceRepository.save(TEST_EXPERIENCE); + + // when + ExperienceEntity experienceEntity = + experienceRepository.findByExperienceIdAndUserEntityUserId( + savedExperience.getExperienceId(), TEST_USER_ID); + + // then + assertThat(experienceEntity.getTitle()).isEqualTo(savedExperience.getTitle()); + assertThat(experienceEntity.getContent()).isEqualTo(savedExperience.getContent()); + assertThat(experienceEntity.getStartDate()).isEqualTo(savedExperience.getStartDate()); + assertThat(experienceEntity.getEndDate()).isEqualTo(savedExperience.getEndDate()); + assertThat(experienceEntity.getLink()).isEqualTo(savedExperience.getLink()); + assertThat(experienceEntity.getUserEntity()).isEqualTo(savedExperience.getUserEntity()); + } + + @Test + void 경험_조회_userId_테스트() { + // given + userRepository.save(TEST_USER); + ExperienceEntity savedExperience = experienceRepository.save(TEST_EXPERIENCE); + + // when + List experienceEntities = + experienceRepository.findByUserEntityUserId(TEST_USER_ID); + + // then + assertThat(experienceEntities.get(0).getTitle()).isEqualTo(savedExperience.getTitle()); + assertThat(experienceEntities.get(0).getContent()).isEqualTo(savedExperience.getContent()); + assertThat(experienceEntities.get(0).getStartDate()).isEqualTo(savedExperience.getStartDate()); + assertThat(experienceEntities.get(0).getEndDate()).isEqualTo(savedExperience.getEndDate()); + assertThat(experienceEntities.get(0).getLink()).isEqualTo(savedExperience.getLink()); + assertThat(experienceEntities.get(0).getUserEntity()) + .isEqualTo(savedExperience.getUserEntity()); + } + + @Test + void 최근_등록한_경험_조회_테스트() { + // given + userRepository.save(TEST_USER); + ExperienceEntity savedExperience = experienceRepository.save(TEST_EXPERIENCE); + + // when + ExperienceEntity experienceEntity = + experienceRepository.findTopByUserEntityOrderByCreateTimestampDesc(TEST_USER); + + // then + assertThat(experienceEntity.getTitle()).isEqualTo(savedExperience.getTitle()); + assertThat(experienceEntity.getContent()).isEqualTo(savedExperience.getContent()); + assertThat(experienceEntity.getStartDate()).isEqualTo(savedExperience.getStartDate()); + assertThat(experienceEntity.getEndDate()).isEqualTo(savedExperience.getEndDate()); + assertThat(experienceEntity.getLink()).isEqualTo(savedExperience.getLink()); + assertThat(experienceEntity.getUserEntity()).isEqualTo(savedExperience.getUserEntity()); + } + + @Test + void 경험_삭제_user_테스트() { + // given + userRepository.save(TEST_USER); + experienceRepository.save(TEST_EXPERIENCE); + experienceRepository.deleteByUserEntity(TEST_USER); + + // when + ExperienceEntity experienceEntity = + experienceRepository.findTopByUserEntityOrderByCreateTimestampDesc(TEST_USER); + + // then + assertThat(experienceEntity).isNull(); + } +} diff --git a/src/test/java/com/sirius/spurt/store/repository/database/repository/QuestionRepositoryTest.java b/src/test/java/com/sirius/spurt/store/repository/database/repository/QuestionRepositoryTest.java new file mode 100644 index 0000000..a7c6269 --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/repository/database/repository/QuestionRepositoryTest.java @@ -0,0 +1,183 @@ +package com.sirius.spurt.store.repository.database.repository; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.sirius.spurt.common.config.QuerydslConfigTest; +import com.sirius.spurt.store.repository.database.entity.CategoryEntity; +import com.sirius.spurt.store.repository.database.entity.QuestionEntity; +import com.sirius.spurt.store.repository.database.entity.UserEntity; +import com.sirius.spurt.test.CategoryTest; +import com.sirius.spurt.test.QuestionTest; +import com.sirius.spurt.test.UserTest; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.test.context.ActiveProfiles; + +@DataJpaTest +@ActiveProfiles("test") +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Import(QuerydslConfigTest.class) +class QuestionRepositoryTest implements QuestionTest, UserTest, CategoryTest { + @Autowired private QuestionRepository questionRepository; + @Autowired private UserRepository userRepository; + + private UserEntity savedUser; + private CategoryEntity savedCategory; + private QuestionEntity savedQuestion; + + @BeforeEach + void init() { + savedUser = userRepository.save(TEST_USER); + savedCategory = CategoryEntity.builder().category(TEST_CATEGORY).build(); + savedQuestion = + questionRepository.save( + QuestionEntity.builder() + .questionId(TEST_QUESTION_ID) + .userId(savedUser.getUserId()) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .jobGroup(TEST_QUESTION_JOB_GROUP) + .pinIndicator(TEST_PIN_INDICATOR) + .pinUpdatedTime(TEST_PIN_UPDATED_TIME) + .experienceId(null) + .categoryEntityList(List.of(savedCategory)) + .build()); + } + + @Test + void 질문_조회_테스트() { + // given + + // when + QuestionEntity questionEntity = + questionRepository.findByQuestionId(savedQuestion.getQuestionId()); + + // then + assertThat(questionEntity.getUserId()).isEqualTo(savedQuestion.getUserId()); + assertThat(questionEntity.getSubject()).isEqualTo(savedQuestion.getSubject()); + assertThat(questionEntity.getMainText()).isEqualTo(savedQuestion.getMainText()); + assertThat(questionEntity.getJobGroup()).isEqualTo(savedQuestion.getJobGroup()); + assertThat(questionEntity.getPinIndicator()).isEqualTo(savedQuestion.getPinIndicator()); + assertThat(questionEntity.getPinUpdatedTime()).isEqualTo(savedQuestion.getPinUpdatedTime()); + assertThat(questionEntity.getExperienceId()).isEqualTo(savedQuestion.getExperienceId()); + } + + @Test + void 질문_조회_question_user_테스트() { + // given + + // when + QuestionEntity questionEntity = + questionRepository.findByQuestionIdAndUserId( + savedQuestion.getQuestionId(), savedUser.getUserId()); + + // then + assertThat(questionEntity.getUserId()).isEqualTo(savedQuestion.getUserId()); + assertThat(questionEntity.getSubject()).isEqualTo(savedQuestion.getSubject()); + assertThat(questionEntity.getMainText()).isEqualTo(savedQuestion.getMainText()); + assertThat(questionEntity.getJobGroup()).isEqualTo(savedQuestion.getJobGroup()); + assertThat(questionEntity.getPinIndicator()).isEqualTo(savedQuestion.getPinIndicator()); + assertThat(questionEntity.getPinUpdatedTime()).isEqualTo(savedQuestion.getPinUpdatedTime()); + assertThat(questionEntity.getExperienceId()).isEqualTo(savedQuestion.getExperienceId()); + } + + @Test + void 최근_등록한_질문_조회_테스트() { + // given + + // when + QuestionEntity questionEntity = + questionRepository.findTopByUserIdOrderByCreateTimestampDesc(savedUser.getUserId()); + + // then + assertThat(questionEntity.getUserId()).isEqualTo(savedQuestion.getUserId()); + assertThat(questionEntity.getSubject()).isEqualTo(savedQuestion.getSubject()); + assertThat(questionEntity.getMainText()).isEqualTo(savedQuestion.getMainText()); + assertThat(questionEntity.getJobGroup()).isEqualTo(savedQuestion.getJobGroup()); + assertThat(questionEntity.getPinIndicator()).isEqualTo(savedQuestion.getPinIndicator()); + assertThat(questionEntity.getPinUpdatedTime()).isEqualTo(savedQuestion.getPinUpdatedTime()); + assertThat(questionEntity.getExperienceId()).isEqualTo(savedQuestion.getExperienceId()); + } + + @Test + void 질문_삭제_user_테스트() { + // given + questionRepository.deleteByUserId(savedUser.getUserId()); + + // when + QuestionEntity questionEntity = + questionRepository.findTopByUserIdOrderByCreateTimestampDesc(savedUser.getUserId()); + + // then + assertThat(questionEntity).isNull(); + } + + @Test + void 질문_페이지_조회_테스트() { + // given + questionRepository.save( + QuestionEntity.builder() + .questionId(TEST_QUESTION_ID) + .userId(savedUser.getUserId()) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .jobGroup(TEST_QUESTION_JOB_GROUP) + .pinIndicator(TEST_PIN_INDICATOR) + .pinUpdatedTime(TEST_PIN_UPDATED_TIME) + .experienceId(null) + .build()); + PageRequest pageRequest = PageRequest.of(0, 1); + + // when + Page pages = + questionRepository.searchQuestion( + null, null, null, null, Boolean.TRUE, savedUser.getUserId(), pageRequest); + + // then + assertThat(pages.getTotalPages()).isEqualTo(2); + assertThat(pages.getTotalElements()).isEqualTo(2); + assertThat(pages.getContent().get(0)).isEqualTo(savedQuestion); + } + + @Test + void 랜덤_질문_조회_테스트() { + // given + CategoryEntity savedCategory2 = CategoryEntity.builder().category(TEST_CATEGORY).build(); + questionRepository.save( + QuestionEntity.builder() + .questionId(TEST_ANOTHER_QUESTION_ID) + .userId(savedUser.getUserId()) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .jobGroup(TEST_QUESTION_JOB_GROUP) + .pinIndicator(TEST_PIN_INDICATOR) + .pinUpdatedTime(TEST_PIN_UPDATED_TIME) + .experienceId(null) + .categoryEntityList(List.of(savedCategory2)) + .build()); + + // when + List questionEntity1 = + questionRepository.randomQuestion( + TEST_QUESTION_JOB_GROUP, TEST_ANOTHER_USER_ID, 1, TEST_CATEGORY); + + List questionEntity2 = null; + int cnt = 100; + do { + questionEntity2 = + questionRepository.randomQuestion( + TEST_QUESTION_JOB_GROUP, TEST_ANOTHER_USER_ID, 1, TEST_CATEGORY); + cnt--; + } while (questionEntity1.get(0).equals(questionEntity2.get(0)) && cnt > 0); + + // then + assertThat(questionEntity1.get(0)).isNotEqualTo(questionEntity2.get(0)); + } +} diff --git a/src/test/java/com/sirius/spurt/store/repository/database/repository/UserRepositoryTest.java b/src/test/java/com/sirius/spurt/store/repository/database/repository/UserRepositoryTest.java new file mode 100644 index 0000000..1da1770 --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/repository/database/repository/UserRepositoryTest.java @@ -0,0 +1,45 @@ +package com.sirius.spurt.store.repository.database.repository; + +import static org.assertj.core.api.Assertions.assertThat; + +import com.sirius.spurt.common.config.QuerydslConfigTest; +import com.sirius.spurt.store.repository.database.entity.UserEntity; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +@DataJpaTest +@ActiveProfiles("test") +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Import(QuerydslConfigTest.class) +class UserRepositoryTest implements UserTest { + @Autowired private UserRepository userRepository; + + @Test + void 유저_존재_확인_테스트() { + // given + userRepository.save(TEST_USER); + + // when + boolean isExistsUser = userRepository.existsByUserId(TEST_USER_ID); + + // then + assertThat(isExistsUser).isEqualTo(true); + } + + @Test + void 유저_조회_테스트() { + // given + UserEntity savedUser = userRepository.save(TEST_USER); + + // when + UserEntity userEntity = userRepository.findByUserId(TEST_USER_ID); + + // then + assertThat(userEntity).isEqualTo(savedUser); + } +} diff --git a/src/test/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthRepositoryImplTest.java b/src/test/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthRepositoryImplTest.java new file mode 100644 index 0000000..2e8b033 --- /dev/null +++ b/src/test/java/com/sirius/spurt/store/repository/resttemplate/auth/impl/AuthRepositoryImplTest.java @@ -0,0 +1,78 @@ +package com.sirius.spurt.store.repository.resttemplate.auth.impl; + +import static com.sirius.spurt.common.meta.ResultCode.AUTHENTICATION_FAILED; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.sirius.spurt.common.exception.GlobalException; +import com.sirius.spurt.store.repository.resttemplate.auth.playload.UserInfoPayload; +import com.sirius.spurt.test.UserTest; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.RestTemplate; + +@ExtendWith(MockitoExtension.class) +class AuthRepositoryImplTest implements UserTest { + @InjectMocks private AuthRepositoryImpl authRepository; + + @Mock private RestTemplate restTemplate; + + @Nested + class 유저_정보_조회 { + @Test + void 유저_정보_조회_테스트() { + // given + UserInfoPayload userInfo = + UserInfoPayload.builder().userId(TEST_USER_ID).email(TEST_EMAIL).build(); + when(restTemplate.exchange( + anyString(), any(HttpMethod.class), any(HttpEntity.class), eq(UserInfoPayload.class))) + .thenReturn(ResponseEntity.ok(userInfo)); + + // when + UserInfoPayload res = authRepository.getUserInfo("accessToken"); + + // then + verify(restTemplate) + .exchange( + anyString(), any(HttpMethod.class), any(HttpEntity.class), eq(UserInfoPayload.class)); + assertThat(res.getUserId()).isEqualTo(userInfo.getUserId()); + assertThat(res.getEmail()).isEqualTo(userInfo.getEmail()); + } + + @Test + void 유저_정보_조회_실패_테스트() { + // given + when(restTemplate.exchange( + anyString(), any(HttpMethod.class), any(HttpEntity.class), eq(UserInfoPayload.class))) + .thenThrow(new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR)); + + // when + GlobalException exception = + assertThrows( + GlobalException.class, + () -> { + authRepository.getUserInfo("accessToken"); + }); + + // then + verify(restTemplate) + .exchange( + anyString(), any(HttpMethod.class), any(HttpEntity.class), eq(UserInfoPayload.class)); + assertThat(exception.getResultCode()).isEqualTo(AUTHENTICATION_FAILED); + } + } +} diff --git a/src/test/java/com/sirius/spurt/test/CategoryTest.java b/src/test/java/com/sirius/spurt/test/CategoryTest.java new file mode 100644 index 0000000..1a7e455 --- /dev/null +++ b/src/test/java/com/sirius/spurt/test/CategoryTest.java @@ -0,0 +1,25 @@ +package com.sirius.spurt.test; + +import com.sirius.spurt.common.meta.Category; +import com.sirius.spurt.store.repository.database.entity.CategoryEntity; + +public interface CategoryTest extends QuestionTest { + Long TEST_CATEGORY_ID = 1L; + Long TEST_ANOTHER_CATEGORY_ID = 2L; + Category TEST_CATEGORY = Category.MAJOR; + Category TEST_ANOTHER_CATEGORY = Category.ALL; + + CategoryEntity TEST_CATEGORY_ENTITY = + CategoryEntity.builder() + .categoryId(TEST_CATEGORY_ID) + .questionId(TEST_QUESTION_ID) + .category(TEST_CATEGORY) + .build(); + + CategoryEntity TEST_ANOTHER_CATEGORY_ENTITY = + CategoryEntity.builder() + .categoryId(TEST_ANOTHER_CATEGORY_ID) + .questionId(TEST_ANOTHER_QUESTION_ID) + .category(TEST_CATEGORY) + .build(); +} diff --git a/src/test/java/com/sirius/spurt/test/ExperienceTest.java b/src/test/java/com/sirius/spurt/test/ExperienceTest.java new file mode 100644 index 0000000..4befd4d --- /dev/null +++ b/src/test/java/com/sirius/spurt/test/ExperienceTest.java @@ -0,0 +1,45 @@ +package com.sirius.spurt.test; + +import com.sirius.spurt.store.repository.database.entity.ExperienceEntity; +import java.sql.Timestamp; +import java.text.SimpleDateFormat; + +public interface ExperienceTest extends UserTest { + Long TEST_EXPERIENCE_ID = 1L; + String TEST_EXPERIENCE_TITLE = "title"; + String TEST_EXPERIENCE_CONTENT = "content"; + Timestamp TEST_EXPERIENCE_START_DATE = Timestamp.valueOf("2023-07-01 00:00:00"); + String TEST_EXPERIENCE_START_DATE_STRING = + new SimpleDateFormat("yyyy-MM").format(TEST_EXPERIENCE_START_DATE); + Timestamp TEST_EXPERIENCE_END_DATE = Timestamp.valueOf("2023-08-19 00:00:00"); + String TEST_EXPERIENCE_END_DATE_STRING = + new SimpleDateFormat("yyyy-MM").format(TEST_EXPERIENCE_END_DATE); + String TEST_EXPERIENCE_LINK = "link"; + + Long TEST_ANOTHER_EXPERIENCE_ID = 2L; + String TEST_ANOTHER_EXPERIENCE_TITLE = "anotherTitle"; + String TEST_ANOTHER_EXPERIENCE_CONTENT = "anotherContent"; + String TEST_ANOTHER_EXPERIENCE_LINK = "anotherLink"; + + ExperienceEntity TEST_EXPERIENCE = + ExperienceEntity.builder() + .experienceId(TEST_EXPERIENCE_ID) + .title(TEST_EXPERIENCE_TITLE) + .content(TEST_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE) + .endDate(TEST_EXPERIENCE_END_DATE) + .link(TEST_EXPERIENCE_LINK) + .userEntity(TEST_USER) + .build(); + + ExperienceEntity TEST_ANOTHER_EXPERIENCE = + ExperienceEntity.builder() + .experienceId(TEST_ANOTHER_EXPERIENCE_ID) + .title(TEST_ANOTHER_EXPERIENCE_TITLE) + .content(TEST_ANOTHER_EXPERIENCE_CONTENT) + .startDate(TEST_EXPERIENCE_START_DATE) + .endDate(TEST_EXPERIENCE_END_DATE) + .link(TEST_ANOTHER_EXPERIENCE_LINK) + .userEntity(TEST_USER) + .build(); +} diff --git a/src/test/java/com/sirius/spurt/test/KeyWordTest.java b/src/test/java/com/sirius/spurt/test/KeyWordTest.java new file mode 100644 index 0000000..185a363 --- /dev/null +++ b/src/test/java/com/sirius/spurt/test/KeyWordTest.java @@ -0,0 +1,25 @@ +package com.sirius.spurt.test; + +import com.sirius.spurt.store.repository.database.entity.KeyWordEntity; + +public interface KeyWordTest extends QuestionTest { + Long TEST_KEY_WORD_ID = 1L; + String TEST_KEY_WORD_VALUE = "keyWord"; + + Long TEST_ANOTHER_KEY_WORD_ID = 2L; + String TEST_ANOTHER_KEY_WORD_VALUE = "anotherKeyWord"; + + KeyWordEntity TEST_KEY_WORD = + KeyWordEntity.builder() + .keyWordId(TEST_KEY_WORD_ID) + .questionId(TEST_QUESTION_ID) + .keyWord(TEST_KEY_WORD_VALUE) + .build(); + + KeyWordEntity TEST_ANOTHER_KEY_WORD = + KeyWordEntity.builder() + .keyWordId(TEST_ANOTHER_KEY_WORD_ID) + .questionId(TEST_QUESTION_ID) + .keyWord(TEST_ANOTHER_KEY_WORD_VALUE) + .build(); +} diff --git a/src/test/java/com/sirius/spurt/test/QuestionTest.java b/src/test/java/com/sirius/spurt/test/QuestionTest.java new file mode 100644 index 0000000..64e65dc --- /dev/null +++ b/src/test/java/com/sirius/spurt/test/QuestionTest.java @@ -0,0 +1,44 @@ +package com.sirius.spurt.test; + +import com.sirius.spurt.common.meta.JobGroup; +import com.sirius.spurt.store.repository.database.entity.QuestionEntity; +import java.sql.Timestamp; + +public interface QuestionTest extends UserTest, ExperienceTest { + Long TEST_QUESTION_ID = 1L; + String TEST_QUESTION_SUBJECT = "subject"; + String TEST_QUESTION_MAIN_TEXT = "mainText"; + JobGroup TEST_QUESTION_JOB_GROUP = JobGroup.DEVELOPER; + Boolean TEST_PIN_INDICATOR = Boolean.TRUE; + Timestamp TEST_PIN_UPDATED_TIME = Timestamp.valueOf("2024-06-21 00:00:00"); + + Long TEST_ANOTHER_QUESTION_ID = 2L; + String TEST_ANOTHER_QUESTION_SUBJECT = "anotherSubject"; + String TEST_ANOTHER_QUESTION_MAIN_TEXT = "anotherMainText"; + + String TEST_CREATE_TIME = "2023-08-13 01:39:21"; + + QuestionEntity TEST_QUESTION = + QuestionEntity.builder() + .questionId(TEST_QUESTION_ID) + .userId(TEST_USER_ID) + .subject(TEST_QUESTION_SUBJECT) + .mainText(TEST_QUESTION_MAIN_TEXT) + .jobGroup(TEST_QUESTION_JOB_GROUP) + .pinIndicator(TEST_PIN_INDICATOR) + .pinUpdatedTime(TEST_PIN_UPDATED_TIME) + .experienceId(TEST_EXPERIENCE_ID) + .build(); + + QuestionEntity TEST_ANOTHER_QUESTION = + QuestionEntity.builder() + .questionId(TEST_QUESTION_ID) + .userId(TEST_ANOTHER_USER_ID) + .subject(TEST_ANOTHER_QUESTION_SUBJECT) + .mainText(TEST_ANOTHER_QUESTION_MAIN_TEXT) + .jobGroup(TEST_QUESTION_JOB_GROUP) + .pinIndicator(TEST_PIN_INDICATOR) + .pinUpdatedTime(TEST_PIN_UPDATED_TIME) + .experienceId(TEST_ANOTHER_EXPERIENCE_ID) + .build(); +} diff --git a/src/test/java/com/sirius/spurt/test/UserTest.java b/src/test/java/com/sirius/spurt/test/UserTest.java new file mode 100644 index 0000000..f2195d9 --- /dev/null +++ b/src/test/java/com/sirius/spurt/test/UserTest.java @@ -0,0 +1,35 @@ +package com.sirius.spurt.test; + +import com.sirius.spurt.common.meta.JobGroup; +import com.sirius.spurt.store.repository.database.entity.UserEntity; + +public interface UserTest { + String TEST_USER_ID = "userId"; + JobGroup TEST_JOB_GROUP = JobGroup.DEVELOPER; + String TEST_EMAIL = "email@gmail.com"; + Boolean TEST_HAS_PINED = Boolean.TRUE; + Boolean TEST_HAS_POSTED = Boolean.TRUE; + + String TEST_ANOTHER_USER_ID = "anotherUserId"; + String TEST_ANOTHER_EMAIL = "another@gmail.com"; + Boolean TEST_ANOTHER_HAS_PINED = Boolean.FALSE; + Boolean TEST_ANOTHER_HAS_POSTED = Boolean.FALSE; + + UserEntity TEST_USER = + UserEntity.builder() + .userId(TEST_USER_ID) + .jobGroup(TEST_JOB_GROUP) + .email(TEST_EMAIL) + .hasPined(TEST_HAS_PINED) + .hasPosted(TEST_HAS_POSTED) + .build(); + + UserEntity TEST_ANOTHER_USER = + UserEntity.builder() + .userId(TEST_ANOTHER_USER_ID) + .jobGroup(TEST_JOB_GROUP) + .email(TEST_ANOTHER_EMAIL) + .hasPined(TEST_ANOTHER_HAS_PINED) + .hasPosted(TEST_ANOTHER_HAS_POSTED) + .build(); +}