From 61802131c9e5d7d8a398097eb19431d3551ba0bc Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Thu, 17 Oct 2024 23:02:06 +0900 Subject: [PATCH 1/7] :sparkles: feat(api): add post recommendation sentence API --- .../common/error/dto/CommonErrorCode.java | 3 +- .../controller/PostRecommendController.java | 29 +++++++++++++++++ .../PostRecommendSentenceResponseDto.java | 6 ++++ .../PostRecommendSentenceRepository.java | 7 ++++ .../service/PostRecommendService.java | 31 ++++++++++++++++++ .../recommend/post/PostRecommendSentence.java | 32 +++++++++++++++++++ ...017__add_post_recommend_sentence_table.sql | 5 +++ 7 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/PostRecommendSentenceRepository.java create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java create mode 100644 backend/streetdrop-domain/src/main/java/com/depromeet/recommend/post/PostRecommendSentence.java create mode 100644 backend/streetdrop-domain/src/main/resources/db/migration/V20241017__add_post_recommend_sentence_table.sql diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/CommonErrorCode.java b/backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/CommonErrorCode.java index 9bac9f43..3643aab5 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/CommonErrorCode.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/common/error/dto/CommonErrorCode.java @@ -30,7 +30,8 @@ public enum CommonErrorCode implements ErrorCodeInterface { */ INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON_INTERNAL_SERVER_ERROR", "Internal Server Error", "An unexpected error occurred"), NOT_IMPLEMENTED(HttpStatus.NOT_IMPLEMENTED, "COMMON_NOT_IMPLEMENTED", "Not Implemented", "The server does not support the functionality required to fulfill the request."), - UNSUPPORTED_TYPE(HttpStatus.BAD_REQUEST, "COMMON_UNSUPPORTED_TYPE", "Unsupported Type", "The type specified is not supported."); + UNSUPPORTED_TYPE(HttpStatus.BAD_REQUEST, "COMMON_UNSUPPORTED_TYPE", "Unsupported Type", "The type specified is not supported."), + SENTENCES_NOT_FOUND(HttpStatus.NOT_FOUND, "COMMON_SENTENCES_NOT_FOUND", "Sentences Not Found", "No sentences available in the database"); private final HttpStatus status; private final String errorResponseCode; diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java new file mode 100644 index 00000000..e69c9d66 --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java @@ -0,0 +1,29 @@ +package com.depromeet.domains.recommend.controller; + +import com.depromeet.common.dto.ResponseDto; +import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; +import com.depromeet.domains.recommend.service.PostRecommendService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping +@RequiredArgsConstructor +@Tag(name = "๐Ÿ’โ€โ™€๏ธPost Recommend", description = "Post Recommend API") +public class PostRecommendController { + + private final PostRecommendService postRecommendService; + + @Operation(summary = "ํ™ˆ ํ™”๋ฉด ๋“œ๋ž ์œ ๋„ - ๋ฌด์ž‘์œ„ ๋ฌธ์žฅ ์ถ”์ฒœ") + @GetMapping("/post-recommend/random-sentence") + public ResponseEntity getRandomPhrase() { + var response = postRecommendService.getOneRandomSentence(); + return ResponseDto.ok(response); + } + +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java new file mode 100644 index 00000000..fbc5de19 --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java @@ -0,0 +1,6 @@ +package com.depromeet.domains.recommend.dto.response; + +public record PostRecommendSentenceResponseDto( + String sentence +) { +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/PostRecommendSentenceRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/PostRecommendSentenceRepository.java new file mode 100644 index 00000000..19d75e6a --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/PostRecommendSentenceRepository.java @@ -0,0 +1,7 @@ +package com.depromeet.domains.recommend.repository; + +import com.depromeet.recommend.post.PostRecommendSentence; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostRecommendSentenceRepository extends JpaRepository { +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java new file mode 100644 index 00000000..f4b51f76 --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java @@ -0,0 +1,31 @@ +package com.depromeet.domains.recommend.service; + +import com.depromeet.common.error.dto.CommonErrorCode; +import com.depromeet.common.error.exception.internal.NotFoundException; +import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; +import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; +import com.depromeet.recommend.post.PostRecommendSentence; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Random; + +@Service +@RequiredArgsConstructor +public class PostRecommendService { + + private final PostRecommendSentenceRepository postRecommendSentenceRepository; + private final Random random = new Random(); + + public PostRecommendSentenceResponseDto getOneRandomSentence() { + var all = postRecommendSentenceRepository.findAll(); + if (all.isEmpty()) { + throw new NotFoundException(CommonErrorCode.SENTENCES_NOT_FOUND); + } + + PostRecommendSentence randomPostRecommendSentence = all.get(random.nextInt(all.size())); + String randomSentence = randomPostRecommendSentence.getSentence(); + return new PostRecommendSentenceResponseDto(randomSentence); + } + +} diff --git a/backend/streetdrop-domain/src/main/java/com/depromeet/recommend/post/PostRecommendSentence.java b/backend/streetdrop-domain/src/main/java/com/depromeet/recommend/post/PostRecommendSentence.java new file mode 100644 index 00000000..74880669 --- /dev/null +++ b/backend/streetdrop-domain/src/main/java/com/depromeet/recommend/post/PostRecommendSentence.java @@ -0,0 +1,32 @@ +package com.depromeet.recommend.post; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import static jakarta.persistence.GenerationType.IDENTITY; +import static lombok.AccessLevel.PROTECTED; + +@Getter +@NoArgsConstructor(access = PROTECTED) +@Entity +public class PostRecommendSentence { + + @Id + @GeneratedValue(strategy = IDENTITY) + @Column(name = "post_recommend_sentence_id") + private Long id; + + @Column(nullable = false) + private String sentence; + + @Builder + public PostRecommendSentence(String sentence) { + this.sentence = sentence; + } + +} diff --git a/backend/streetdrop-domain/src/main/resources/db/migration/V20241017__add_post_recommend_sentence_table.sql b/backend/streetdrop-domain/src/main/resources/db/migration/V20241017__add_post_recommend_sentence_table.sql new file mode 100644 index 00000000..ea092cd2 --- /dev/null +++ b/backend/streetdrop-domain/src/main/resources/db/migration/V20241017__add_post_recommend_sentence_table.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS post_recommend_sentence ( + post_recommend_sentence_id BIGINT NOT NULL AUTO_INCREMENT, + sentence VARCHAR(255) NOT NULL, + PRIMARY KEY (post_recommend_sentence_id) +) \ No newline at end of file From 4e0bb030da41ae8e0c5e3da9a48f7ad74da31921 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Thu, 17 Oct 2024 23:05:08 +0900 Subject: [PATCH 2/7] :white_check_mark: test(api): add post recommendation sentence --- .../PostRecommendControllerTest.java | 54 ++++++++++++++ .../service/PostRecommendServiceTest.java | 74 +++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java create mode 100644 backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java diff --git a/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java b/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java new file mode 100644 index 00000000..64768bf0 --- /dev/null +++ b/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java @@ -0,0 +1,54 @@ +package unit.domains.recommend.controller; + +import com.depromeet.common.error.dto.CommonErrorCode; +import com.depromeet.common.error.exception.internal.NotFoundException; +import com.depromeet.domains.recommend.controller.PostRecommendController; +import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; +import com.depromeet.domains.recommend.service.PostRecommendService; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.web.servlet.MockMvc; + +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@ContextConfiguration(classes = PostRecommendController.class) +@WebMvcTest(controllers = {PostRecommendController.class}, excludeAutoConfiguration = {SecurityAutoConfiguration.class}) +@Import(PostRecommendController.class) +@DisplayName("[API][Controller] PostRecommendController ํ…Œ์ŠคํŠธ") +public class PostRecommendControllerTest { + + @Autowired + MockMvc mvc; + + @MockBean + PostRecommendService postRecommendService; + + @DisplayName("[GET] ํ™ˆ ํ™”๋ฉด ๋“œ๋ž ์œ ๋„ - ๋ฌด์ž‘์œ„ ๋ฌธ์žฅ ์ถ”์ฒœ") + @Nested + class GetRandomSentenceTest { + @Nested + @DisplayName("์„ฑ๊ณต") + class Success { + @DisplayName("๋ฌด์ž‘์œ„ ์ถ”์ฒœ ๋ฌธ์žฅ 1๊ฐœ ์กฐํšŒ") + @Test + void getOneRandomSentenceSuccess() throws Exception { + var randomSentence = new PostRecommendSentenceResponseDto("random sentence"); + when(postRecommendService.getOneRandomSentence()).thenReturn(randomSentence); + + var response = mvc.perform(get("/post-recommend/random-sentence")); + response.andExpect(status().isOk()) + .andExpect(jsonPath("$.sentence").value("random sentence")); + } + } + } +} diff --git a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java new file mode 100644 index 00000000..cfa97e19 --- /dev/null +++ b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java @@ -0,0 +1,74 @@ +package unit.domains.recommend.service; + +import com.depromeet.common.error.exception.internal.NotFoundException; +import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; +import com.depromeet.domains.recommend.service.PostRecommendService; +import com.depromeet.recommend.post.PostRecommendSentence; +import org.junit.jupiter.api.DisplayName; +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 java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; +import static org.mockito.BDDMockito.given; + +@ExtendWith(MockitoExtension.class) +@DisplayName("[Service] PostRecommendService ํ…Œ์ŠคํŠธ") +public class PostRecommendServiceTest { + + @InjectMocks + private PostRecommendService postRecommendService; + + @Mock + private PostRecommendSentenceRepository postRecommendSentenceRepository; + + @DisplayName("๋ฌด์ž‘์œ„ ๋ฌธ์žฅ ์ถ”์ฒœ") + @Nested + class GetOneRandomSentenceTest { + @Nested + @DisplayName("์„ฑ๊ณต") + class Success { + @DisplayName("๋ฌด์ž‘์œ„ ์ถ”์ฒœ ๋ฌธ์žฅ 1๊ฐœ ์กฐํšŒ") + @Test + void getOneRandomSentenceSuccess() { + List sentences = List.of( + new PostRecommendSentence("First sentence"), + new PostRecommendSentence("Second sentence"), + new PostRecommendSentence("Third sentence") + ); + + given(postRecommendSentenceRepository.findAll()) + .willReturn(sentences); + var result = postRecommendService.getOneRandomSentence(); + + assertThat(result).isNotNull(); + assertThat(result.sentence()).isIn( + "First sentence", + "Second sentence", + "Third sentence" + ); + } + } + + @Nested + @DisplayName("์‹คํŒจ") + class Fail { + @DisplayName("์ €์žฅ์†Œ์— ์ถ”์ฒœ ๋ฌธ์žฅ์ด ์—†๋Š” ๊ฒฝ์šฐ") + @Test + void getOneRandomSentenceFail() { + given(postRecommendSentenceRepository.findAll()) + .willReturn(List.of()); + + assertThatThrownBy(() -> postRecommendService.getOneRandomSentence()) + .isInstanceOf(NotFoundException.class); + } + } + } + +} From daa73f45d51420c4aaeef6b2677600b6c76913a4 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Sun, 20 Oct 2024 21:10:48 +0900 Subject: [PATCH 3/7] :sparkles: feat(api): get once a day --- .../controller/PostRecommendController.java | 8 ++++-- .../PostRecommendSentenceResponseDto.java | 3 ++ .../recommend/provider/RandomProvider.java | 20 +++++++++++++ .../MemoryUserTimestampRepository.java | 28 +++++++++++++++++++ .../repository/UserTimestampRepository.java | 6 ++++ .../service/PostRecommendService.java | 28 ++++++++++++------- .../security/config/WebSecurityConfig.java | 1 + 7 files changed, 82 insertions(+), 12 deletions(-) create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/provider/RandomProvider.java create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java create mode 100644 backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java index e69c9d66..da23781c 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/controller/PostRecommendController.java @@ -3,6 +3,8 @@ import com.depromeet.common.dto.ResponseDto; import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; import com.depromeet.domains.recommend.service.PostRecommendService; +import com.depromeet.security.annotation.ReqUser; +import com.depromeet.user.User; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; @@ -21,8 +23,10 @@ public class PostRecommendController { @Operation(summary = "ํ™ˆ ํ™”๋ฉด ๋“œ๋ž ์œ ๋„ - ๋ฌด์ž‘์œ„ ๋ฌธ์žฅ ์ถ”์ฒœ") @GetMapping("/post-recommend/random-sentence") - public ResponseEntity getRandomPhrase() { - var response = postRecommendService.getOneRandomSentence(); + public ResponseEntity getRandomPhrase( + @ReqUser User user + ) { + var response = postRecommendService.getOneRandomSentence(user); return ResponseDto.ok(response); } diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java index fbc5de19..c8a97b21 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/dto/response/PostRecommendSentenceResponseDto.java @@ -3,4 +3,7 @@ public record PostRecommendSentenceResponseDto( String sentence ) { + public static PostRecommendSentenceResponseDto empty() { + return new PostRecommendSentenceResponseDto(null); + } } diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/provider/RandomProvider.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/provider/RandomProvider.java new file mode 100644 index 00000000..e87c1ef5 --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/provider/RandomProvider.java @@ -0,0 +1,20 @@ +package com.depromeet.domains.recommend.provider; + +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Random; + +@Component +public final class RandomProvider { + + private static final Random RANDOM = new Random(); + + public static T getRandomElement(List list) { + return list.get(RANDOM.nextInt(list.size())); + } + + private RandomProvider() { + } + +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java new file mode 100644 index 00000000..c58999ab --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java @@ -0,0 +1,28 @@ +package com.depromeet.domains.recommend.repository; + +import org.springframework.stereotype.Repository; + +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; + +@Repository +public class MemoryUserTimestampRepository implements UserTimestampRepository{ + + private static Map store = new HashMap<>(); + + @Override + public void save(Long userId) { + store.put(userId, LocalDateTime.now()); + } + + @Override + public Boolean isSent(Long userId) { + System.out.println(">> " + store); + return Optional.ofNullable(store.get(userId)) + .map(readTime -> !readTime.isBefore(LocalDateTime.now().minusDays(1))) + .orElse(false); + } + +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java new file mode 100644 index 00000000..feb52247 --- /dev/null +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java @@ -0,0 +1,6 @@ +package com.depromeet.domains.recommend.repository; + +public interface UserTimestampRepository { + void save(Long userId); + Boolean isSent(Long userId); +} diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java index f4b51f76..f0b09195 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java @@ -3,29 +3,37 @@ import com.depromeet.common.error.dto.CommonErrorCode; import com.depromeet.common.error.exception.internal.NotFoundException; import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; +import com.depromeet.domains.recommend.provider.RandomProvider; import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; +import com.depromeet.domains.recommend.repository.UserTimestampRepository; import com.depromeet.recommend.post.PostRecommendSentence; +import com.depromeet.user.User; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.Random; - @Service @RequiredArgsConstructor public class PostRecommendService { private final PostRecommendSentenceRepository postRecommendSentenceRepository; - private final Random random = new Random(); + private final UserTimestampRepository userTimestampRepository; - public PostRecommendSentenceResponseDto getOneRandomSentence() { - var all = postRecommendSentenceRepository.findAll(); - if (all.isEmpty()) { - throw new NotFoundException(CommonErrorCode.SENTENCES_NOT_FOUND); + public PostRecommendSentenceResponseDto getOneRandomSentence(User user) { + if (userTimestampRepository.isSent(user.getId())) { + return PostRecommendSentenceResponseDto.empty(); } - - PostRecommendSentence randomPostRecommendSentence = all.get(random.nextInt(all.size())); - String randomSentence = randomPostRecommendSentence.getSentence(); + String randomSentence = getRandomSentence(); + userTimestampRepository.save(user.getId()); return new PostRecommendSentenceResponseDto(randomSentence); } + private String getRandomSentence() { + var postRecommendSentences = postRecommendSentenceRepository.findAll(); + if (postRecommendSentences.isEmpty()) { + throw new NotFoundException(CommonErrorCode.SENTENCES_NOT_FOUND); + } + PostRecommendSentence randomPostRecommendSentence = RandomProvider.getRandomElement(postRecommendSentences); + return randomPostRecommendSentence.getSentence(); + } + } diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/security/config/WebSecurityConfig.java b/backend/streetdrop-api/src/main/java/com/depromeet/security/config/WebSecurityConfig.java index 16f49932..6fee79f5 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/security/config/WebSecurityConfig.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/security/config/WebSecurityConfig.java @@ -34,6 +34,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { .requestMatchers("/users/**").authenticated() .requestMatchers("/items/**").authenticated() .requestMatchers("/pop-up/**").authenticated() + .requestMatchers("/post-recommend/**").authenticated() .requestMatchers(HttpMethod.POST, "notifications/tokens").authenticated() .anyRequest().permitAll() .and().exceptionHandling() From 2822ac7fc1defa79fa926be999145a6c978336b2 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Sun, 20 Oct 2024 21:55:21 +0900 Subject: [PATCH 4/7] :white_check_mark: test(api): get once a day --- .../PostRecommendControllerTest.java | 51 +++++++++++++++---- .../service/PostRecommendServiceTest.java | 40 ++++++++++++--- 2 files changed, 75 insertions(+), 16 deletions(-) diff --git a/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java b/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java index 64768bf0..c1e1594c 100644 --- a/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java +++ b/backend/streetdrop-api/src/test/java/unit/domains/recommend/controller/PostRecommendControllerTest.java @@ -1,29 +1,32 @@ package unit.domains.recommend.controller; -import com.depromeet.common.error.dto.CommonErrorCode; -import com.depromeet.common.error.exception.internal.NotFoundException; +import com.depromeet.common.error.GlobalExceptionHandler; import com.depromeet.domains.recommend.controller.PostRecommendController; import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; import com.depromeet.domains.recommend.service.PostRecommendService; +import com.depromeet.user.User; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.web.servlet.MockMvc; +import unit.annotation.MockAnonymousUser; +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.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@ContextConfiguration(classes = PostRecommendController.class) -@WebMvcTest(controllers = {PostRecommendController.class}, excludeAutoConfiguration = {SecurityAutoConfiguration.class}) -@Import(PostRecommendController.class) +@ContextConfiguration(classes = {PostRecommendController.class, ValidationAutoConfiguration.class}) +@WebMvcTest(controllers = {PostRecommendController.class}) +@Import({PostRecommendController.class, GlobalExceptionHandler.class}) @DisplayName("[API][Controller] PostRecommendController ํ…Œ์ŠคํŠธ") public class PostRecommendControllerTest { @@ -33,22 +36,52 @@ public class PostRecommendControllerTest { @MockBean PostRecommendService postRecommendService; + User user; + + @BeforeEach + void setUp() { + user = User.builder() + .idfv("new-idfv") + .build(); + } + @DisplayName("[GET] ํ™ˆ ํ™”๋ฉด ๋“œ๋ž ์œ ๋„ - ๋ฌด์ž‘์œ„ ๋ฌธ์žฅ ์ถ”์ฒœ") @Nested + @MockAnonymousUser class GetRandomSentenceTest { @Nested @DisplayName("์„ฑ๊ณต") + @MockAnonymousUser class Success { @DisplayName("๋ฌด์ž‘์œ„ ์ถ”์ฒœ ๋ฌธ์žฅ 1๊ฐœ ์กฐํšŒ") @Test - void getOneRandomSentenceSuccess() throws Exception { + void getOneRandomSentenceSuccess1() throws Exception { + var randomSentence = new PostRecommendSentenceResponseDto("random sentence"); - when(postRecommendService.getOneRandomSentence()).thenReturn(randomSentence); + when(postRecommendService.getOneRandomSentence(any(User.class))).thenReturn(randomSentence); - var response = mvc.perform(get("/post-recommend/random-sentence")); + var response = mvc.perform( + get("/post-recommend/random-sentence") + .header("x-sdp-idfv", "new-idfv") + ); response.andExpect(status().isOk()) .andExpect(jsonPath("$.sentence").value("random sentence")); } + + @DisplayName("๋ฌด์ž‘์œ„ ์ถ”์ฒœ ๋ฌธ์žฅ์ด ์—†๋Š” ๊ฒฝ์šฐ") + @Test + void getOneRandomSentenceSuccess2() throws Exception { + + var emptySentence = PostRecommendSentenceResponseDto.empty(); + when(postRecommendService.getOneRandomSentence(any(User.class))).thenReturn(emptySentence); + + var response = mvc.perform( + get("/post-recommend/random-sentence") + .header("x-sdp-idfv", "new-idfv") + ); + response.andExpect(status().isOk()) + .andExpect(jsonPath("$.sentence").isEmpty()); + } } } } diff --git a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java index cfa97e19..21271ab2 100644 --- a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java +++ b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java @@ -2,8 +2,11 @@ import com.depromeet.common.error.exception.internal.NotFoundException; import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; +import com.depromeet.domains.recommend.repository.UserTimestampRepository; import com.depromeet.domains.recommend.service.PostRecommendService; import com.depromeet.recommend.post.PostRecommendSentence; +import com.depromeet.user.User; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; @@ -12,6 +15,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.lang.reflect.Field; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; @@ -28,24 +32,46 @@ public class PostRecommendServiceTest { @Mock private PostRecommendSentenceRepository postRecommendSentenceRepository; + @Mock + private UserTimestampRepository userTimestampRepository; + + User user; + + @BeforeEach + void setUp() throws NoSuchFieldException, IllegalAccessException { + user = User.builder().build(); + Field userIdField = User.class.getDeclaredField("id"); + userIdField.setAccessible(true); + userIdField.set(user, 1L); + } + @DisplayName("๋ฌด์ž‘์œ„ ๋ฌธ์žฅ ์ถ”์ฒœ") @Nested class GetOneRandomSentenceTest { @Nested @DisplayName("์„ฑ๊ณต") class Success { + @DisplayName("์ด๋ฏธ ์ถ”์ฒœ ๋ฌธ์žฅ์„ ๋ฐ›์€ ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ") + @Test + void getOneRandomSentenceSuccess1() { + given(userTimestampRepository.isSent(user.getId())).willReturn(true); + var result = postRecommendService.getOneRandomSentence(user); + + assertThat(result.sentence()).isNull(); + } + @DisplayName("๋ฌด์ž‘์œ„ ์ถ”์ฒœ ๋ฌธ์žฅ 1๊ฐœ ์กฐํšŒ") @Test - void getOneRandomSentenceSuccess() { + void getOneRandomSentenceSuccess2() { List sentences = List.of( new PostRecommendSentence("First sentence"), new PostRecommendSentence("Second sentence"), new PostRecommendSentence("Third sentence") ); - given(postRecommendSentenceRepository.findAll()) - .willReturn(sentences); - var result = postRecommendService.getOneRandomSentence(); + given(postRecommendSentenceRepository.findAll()).willReturn(sentences); + given(userTimestampRepository.isSent(user.getId())).willReturn(false); + var result = postRecommendService.getOneRandomSentence(user); assertThat(result).isNotNull(); assertThat(result.sentence()).isIn( @@ -62,10 +88,10 @@ class Fail { @DisplayName("์ €์žฅ์†Œ์— ์ถ”์ฒœ ๋ฌธ์žฅ์ด ์—†๋Š” ๊ฒฝ์šฐ") @Test void getOneRandomSentenceFail() { - given(postRecommendSentenceRepository.findAll()) - .willReturn(List.of()); + given(userTimestampRepository.isSent(user.getId())).willReturn(false); + given(postRecommendSentenceRepository.findAll()).willReturn(List.of()); - assertThatThrownBy(() -> postRecommendService.getOneRandomSentence()) + assertThatThrownBy(() -> postRecommendService.getOneRandomSentence(user)) .isInstanceOf(NotFoundException.class); } } From cbe74dbeea3e9af8200f67056ea25d9a68ffd583 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Sun, 20 Oct 2024 21:56:04 +0900 Subject: [PATCH 5/7] :recycle: refactor(api): remove unnecessary codes --- .../recommend/repository/MemoryUserTimestampRepository.java | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java index c58999ab..a8f8ad94 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java @@ -19,7 +19,6 @@ public void save(Long userId) { @Override public Boolean isSent(Long userId) { - System.out.println(">> " + store); return Optional.ofNullable(store.get(userId)) .map(readTime -> !readTime.isBefore(LocalDateTime.now().minusDays(1))) .orElse(false); From d9d7bb03bd5d0715b2c258d9b009bdfe2cf7f165 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Mon, 21 Oct 2024 13:53:09 +0900 Subject: [PATCH 6/7] :recycle: refactor(api): rename UserTimestampRepository -> UserReadRepository --- ...mpRepository.java => MemoryUserReadRepository.java} | 2 +- ...imestampRepository.java => UserReadRepository.java} | 2 +- .../recommend/service/PostRecommendService.java | 8 ++++---- .../recommend/service/PostRecommendServiceTest.java | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) rename backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/{MemoryUserTimestampRepository.java => MemoryUserReadRepository.java} (88%) rename backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/{UserTimestampRepository.java => UserReadRepository.java} (72%) diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserReadRepository.java similarity index 88% rename from backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java rename to backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserReadRepository.java index a8f8ad94..e32061c4 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserTimestampRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserReadRepository.java @@ -8,7 +8,7 @@ import java.util.Optional; @Repository -public class MemoryUserTimestampRepository implements UserTimestampRepository{ +public class MemoryUserReadRepository implements UserReadRepository { private static Map store = new HashMap<>(); diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserReadRepository.java similarity index 72% rename from backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java rename to backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserReadRepository.java index feb52247..d598fccc 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserTimestampRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserReadRepository.java @@ -1,6 +1,6 @@ package com.depromeet.domains.recommend.repository; -public interface UserTimestampRepository { +public interface UserReadRepository { void save(Long userId); Boolean isSent(Long userId); } diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java index f0b09195..1846435f 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java @@ -5,7 +5,7 @@ import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; import com.depromeet.domains.recommend.provider.RandomProvider; import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; -import com.depromeet.domains.recommend.repository.UserTimestampRepository; +import com.depromeet.domains.recommend.repository.UserReadRepository; import com.depromeet.recommend.post.PostRecommendSentence; import com.depromeet.user.User; import lombok.RequiredArgsConstructor; @@ -16,14 +16,14 @@ public class PostRecommendService { private final PostRecommendSentenceRepository postRecommendSentenceRepository; - private final UserTimestampRepository userTimestampRepository; + private final UserReadRepository userReadRepository; public PostRecommendSentenceResponseDto getOneRandomSentence(User user) { - if (userTimestampRepository.isSent(user.getId())) { + if (userReadRepository.isSent(user.getId())) { return PostRecommendSentenceResponseDto.empty(); } String randomSentence = getRandomSentence(); - userTimestampRepository.save(user.getId()); + userReadRepository.save(user.getId()); return new PostRecommendSentenceResponseDto(randomSentence); } diff --git a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java index 21271ab2..22acd6b5 100644 --- a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java +++ b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java @@ -2,7 +2,7 @@ import com.depromeet.common.error.exception.internal.NotFoundException; import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; -import com.depromeet.domains.recommend.repository.UserTimestampRepository; +import com.depromeet.domains.recommend.repository.UserReadRepository; import com.depromeet.domains.recommend.service.PostRecommendService; import com.depromeet.recommend.post.PostRecommendSentence; import com.depromeet.user.User; @@ -33,7 +33,7 @@ public class PostRecommendServiceTest { private PostRecommendSentenceRepository postRecommendSentenceRepository; @Mock - private UserTimestampRepository userTimestampRepository; + private UserReadRepository userReadRepository; User user; @@ -54,7 +54,7 @@ class Success { @DisplayName("์ด๋ฏธ ์ถ”์ฒœ ๋ฌธ์žฅ์„ ๋ฐ›์€ ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ") @Test void getOneRandomSentenceSuccess1() { - given(userTimestampRepository.isSent(user.getId())).willReturn(true); + given(userReadRepository.isSent(user.getId())).willReturn(true); var result = postRecommendService.getOneRandomSentence(user); assertThat(result.sentence()).isNull(); @@ -70,7 +70,7 @@ void getOneRandomSentenceSuccess2() { ); given(postRecommendSentenceRepository.findAll()).willReturn(sentences); - given(userTimestampRepository.isSent(user.getId())).willReturn(false); + given(userReadRepository.isSent(user.getId())).willReturn(false); var result = postRecommendService.getOneRandomSentence(user); assertThat(result).isNotNull(); @@ -88,7 +88,7 @@ class Fail { @DisplayName("์ €์žฅ์†Œ์— ์ถ”์ฒœ ๋ฌธ์žฅ์ด ์—†๋Š” ๊ฒฝ์šฐ") @Test void getOneRandomSentenceFail() { - given(userTimestampRepository.isSent(user.getId())).willReturn(false); + given(userReadRepository.isSent(user.getId())).willReturn(false); given(postRecommendSentenceRepository.findAll()).willReturn(List.of()); assertThatThrownBy(() -> postRecommendService.getOneRandomSentence(user)) From 53df9cce9982d11bf72a0f3c088185fdcb9f5886 Mon Sep 17 00:00:00 2001 From: siyeonSon Date: Mon, 21 Oct 2024 15:11:35 +0900 Subject: [PATCH 7/7] :recycle: refactor(api): rename UserReadRepository -> userRecommendSendHistoryRepository --- ...a => MemoryUserRecommendSendHistoryRepository.java} | 2 +- ...ry.java => UserRecommendSendHistoryRepository.java} | 2 +- .../recommend/service/PostRecommendService.java | 8 ++++---- .../recommend/service/PostRecommendServiceTest.java | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) rename backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/{MemoryUserReadRepository.java => MemoryUserRecommendSendHistoryRepository.java} (86%) rename backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/{UserReadRepository.java => UserRecommendSendHistoryRepository.java} (68%) diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserReadRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserRecommendSendHistoryRepository.java similarity index 86% rename from backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserReadRepository.java rename to backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserRecommendSendHistoryRepository.java index e32061c4..bcac6e4e 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserReadRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/MemoryUserRecommendSendHistoryRepository.java @@ -8,7 +8,7 @@ import java.util.Optional; @Repository -public class MemoryUserReadRepository implements UserReadRepository { +public class MemoryUserRecommendSendHistoryRepository implements UserRecommendSendHistoryRepository { private static Map store = new HashMap<>(); diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserReadRepository.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserRecommendSendHistoryRepository.java similarity index 68% rename from backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserReadRepository.java rename to backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserRecommendSendHistoryRepository.java index d598fccc..9a4d5e85 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserReadRepository.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/repository/UserRecommendSendHistoryRepository.java @@ -1,6 +1,6 @@ package com.depromeet.domains.recommend.repository; -public interface UserReadRepository { +public interface UserRecommendSendHistoryRepository { void save(Long userId); Boolean isSent(Long userId); } diff --git a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java index 1846435f..9ebe94b0 100644 --- a/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java +++ b/backend/streetdrop-api/src/main/java/com/depromeet/domains/recommend/service/PostRecommendService.java @@ -5,7 +5,7 @@ import com.depromeet.domains.recommend.dto.response.PostRecommendSentenceResponseDto; import com.depromeet.domains.recommend.provider.RandomProvider; import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; -import com.depromeet.domains.recommend.repository.UserReadRepository; +import com.depromeet.domains.recommend.repository.UserRecommendSendHistoryRepository; import com.depromeet.recommend.post.PostRecommendSentence; import com.depromeet.user.User; import lombok.RequiredArgsConstructor; @@ -16,14 +16,14 @@ public class PostRecommendService { private final PostRecommendSentenceRepository postRecommendSentenceRepository; - private final UserReadRepository userReadRepository; + private final UserRecommendSendHistoryRepository userRecommendSendHistoryRepository; public PostRecommendSentenceResponseDto getOneRandomSentence(User user) { - if (userReadRepository.isSent(user.getId())) { + if (userRecommendSendHistoryRepository.isSent(user.getId())) { return PostRecommendSentenceResponseDto.empty(); } String randomSentence = getRandomSentence(); - userReadRepository.save(user.getId()); + userRecommendSendHistoryRepository.save(user.getId()); return new PostRecommendSentenceResponseDto(randomSentence); } diff --git a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java index 22acd6b5..5d4111a3 100644 --- a/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java +++ b/backend/streetdrop-api/src/test/java/unit/domains/recommend/service/PostRecommendServiceTest.java @@ -2,7 +2,7 @@ import com.depromeet.common.error.exception.internal.NotFoundException; import com.depromeet.domains.recommend.repository.PostRecommendSentenceRepository; -import com.depromeet.domains.recommend.repository.UserReadRepository; +import com.depromeet.domains.recommend.repository.UserRecommendSendHistoryRepository; import com.depromeet.domains.recommend.service.PostRecommendService; import com.depromeet.recommend.post.PostRecommendSentence; import com.depromeet.user.User; @@ -33,7 +33,7 @@ public class PostRecommendServiceTest { private PostRecommendSentenceRepository postRecommendSentenceRepository; @Mock - private UserReadRepository userReadRepository; + private UserRecommendSendHistoryRepository userRecommendSendHistoryRepository; User user; @@ -54,7 +54,7 @@ class Success { @DisplayName("์ด๋ฏธ ์ถ”์ฒœ ๋ฌธ์žฅ์„ ๋ฐ›์€ ์‚ฌ์šฉ์ž์ธ ๊ฒฝ์šฐ") @Test void getOneRandomSentenceSuccess1() { - given(userReadRepository.isSent(user.getId())).willReturn(true); + given(userRecommendSendHistoryRepository.isSent(user.getId())).willReturn(true); var result = postRecommendService.getOneRandomSentence(user); assertThat(result.sentence()).isNull(); @@ -70,7 +70,7 @@ void getOneRandomSentenceSuccess2() { ); given(postRecommendSentenceRepository.findAll()).willReturn(sentences); - given(userReadRepository.isSent(user.getId())).willReturn(false); + given(userRecommendSendHistoryRepository.isSent(user.getId())).willReturn(false); var result = postRecommendService.getOneRandomSentence(user); assertThat(result).isNotNull(); @@ -88,7 +88,7 @@ class Fail { @DisplayName("์ €์žฅ์†Œ์— ์ถ”์ฒœ ๋ฌธ์žฅ์ด ์—†๋Š” ๊ฒฝ์šฐ") @Test void getOneRandomSentenceFail() { - given(userReadRepository.isSent(user.getId())).willReturn(false); + given(userRecommendSendHistoryRepository.isSent(user.getId())).willReturn(false); given(postRecommendSentenceRepository.findAll()).willReturn(List.of()); assertThatThrownBy(() -> postRecommendService.getOneRandomSentence(user))