Skip to content

Commit

Permalink
Merge pull request #17 from f-lab-edu/issue16
Browse files Browse the repository at this point in the history
Issue16
  • Loading branch information
misim3 authored Jul 27, 2024
2 parents bd64184 + b91cbde commit 66e3590
Show file tree
Hide file tree
Showing 13 changed files with 196 additions and 91 deletions.
1 change: 1 addition & 0 deletions mitube-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'it.ozimov:embedded-redis:0.7.2'
}

tasks.named('bootBuildImage') {
Expand Down
32 changes: 32 additions & 0 deletions mitube-app/src/main/java/com/misim/config/EmbeddedRedisConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.misim.config;

import jakarta.annotation.PreDestroy;
import java.io.IOException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import redis.embedded.RedisServer;


@Profile("local")
@Configuration
public class EmbeddedRedisConfig {

@Value("${spring.data.redis.port}")
private int redisPort;

private RedisServer redisServer;

@Bean
public RedisServer redisServer() throws IOException {
redisServer = new RedisServer(redisPort);
redisServer.start();
return redisServer;
}

@PreDestroy
public void stopRedis() {
redisServer.stop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,10 @@ public CommonResponse<CommentListResponse> getParentComments(
public CommonResponse<CommentListResponse> getChildComments(
@PathVariable @Parameter(name = "videoId", description = "시청할 동영상 식별 정보", required = true) Long videoId,
@PathVariable @Parameter(name = "parentCommentId", description = "대댓글이 달린 댓글의 식별 정보") Long parentCommentId,
@RequestParam @Parameter(name = "idx", description = "댓글의 인덱스 정보") Long idx,
@RequestParam @Parameter(name = "scrollDirection", description = "댓글 목록 스크롤 방향으로 up, down만 가능하다.") String scrollDirection) {

checkRequests(idx, scrollDirection);
@RequestParam @Parameter(name = "idx", description = "대댓글의 인덱스 정보") Long idx) {

CommentListResponse comments = commentService.getChildComments(videoId, parentCommentId,
idx, scrollDirection);
idx);

return CommonResponse.<CommentListResponse>builder()
.body(comments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,4 @@ List<Comment> findDownCommentByVideoIdAndId(@Param("videoId") Long videoId,
List<Comment> findDownCommentByVideoIdAndIdAndParentCommentId(
@Param("parentCommentId") Long parentCommentId, @Param("videoId") Long videoId,
@Param("id") Long id);

@Query("SELECT c1 from Comment c1 WHERE c1.parentCommentId = :parentCommentId AND c1.video.id = :videoId AND c1.id > :id ORDER BY c1.createdDate ASC LIMIT 10")
List<Comment> findUpCommentByVideoIdAndIdAndParentCommentId(
@Param("parentCommentId") Long parentCommentId, @Param("videoId") Long videoId,
@Param("id") Long id);
}
12 changes: 2 additions & 10 deletions mitube-app/src/main/java/com/misim/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,14 @@ public CommentListResponse getParentComments(Long videoId, Long idx, String scro
return commentListResponse;
}

public CommentListResponse getChildComments(Long videoId, Long parentCommentId, Long idx,
String scrollDirection) {
public CommentListResponse getChildComments(Long videoId, Long parentCommentId, Long idx) {

if (!videoRepository.existsById(videoId)) {
throw new MitubeException(MitubeErrorCode.NOT_FOUND_VIDEO);
}

List<Comment> comments = new ArrayList<>();

if (scrollDirection.equals("up")) {
comments = commentRepository.findUpCommentByVideoIdAndIdAndParentCommentId(
List<Comment> comments = commentRepository.findDownCommentByVideoIdAndIdAndParentCommentId(
parentCommentId, videoId, idx);
} else if (scrollDirection.equals("down")) {
comments = commentRepository.findDownCommentByVideoIdAndIdAndParentCommentId(
parentCommentId, videoId, idx);
}

return CommentListResponse.builder()
.commentResponses(convertCommentResponseList(comments))
Expand Down
13 changes: 13 additions & 0 deletions mitube-app/src/test/java/com/misim/MitubeAppApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.misim;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

@ActiveProfiles("local")
@SpringBootTest
class MitubeAppApplicationTest {

@Test
void contextLoads() {}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.misim.controller;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.misim.controller.model.Response.TermDetailResponse;
import com.misim.controller.model.Response.TermListResponse;
import com.misim.controller.model.Response.TermResponse;
import com.misim.service.TermService;
import java.util.Collections;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,12 +21,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import java.util.Collections;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(TermController.class)
@WithMockUser
class TermControllerTest {
Expand All @@ -31,7 +32,7 @@ class TermControllerTest {
private TermService termService;

@Test
public void testGetTermsByMocking() throws Exception {
public void getTerms_correctly_byMocking() throws Exception {

// mock 객체
TermListResponse mockResponse = TermListResponse.builder()
Expand All @@ -54,7 +55,7 @@ public void testGetTermsByMocking() throws Exception {
}

@Test
public void testGetTermPolicyByMocking() throws Exception {
public void getTermPolicy_correctly_byMocking() throws Exception {

// mock 객체
TermDetailResponse mockResponse = TermDetailResponse.detailBuidler()
Expand All @@ -79,32 +80,12 @@ public void testGetTermPolicyByMocking() throws Exception {

@Test
@DisplayName("파라미터인 title의 값이 없는 경우")
public void testGetTermPolicyNotFoundTermsByNoTitle() throws Exception {
public void getTermPolicy_notFoundTerms_NoTitle() throws Exception {

// 실행 결과 확인
mockMvc.perform(get("/terms/policy")
.param("title", "")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError());
}

@Test
@DisplayName("파라미터인 title로 term을 찾을 수 없는 경우")
public void testGetTermPolicyNotFoundTerms() throws Exception {

// mock 객체
TermDetailResponse mockResponse = TermDetailResponse.detailBuidler()
.title("Sample TermDetailResponse")
.content("Sample TermDetailResponse's Contents")
.isRequired(true)
.build();

given(termService.getTermByTitle("Sample TermDetailResponse")).willReturn(mockResponse);

// 실행 결과 확인
mockMvc.perform(get("/terms/policy")
.param("title", "Unknown TermDetailResponse")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is4xxClientError());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package com.misim.controller;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.misim.controller.model.Request.*;
import com.misim.controller.model.Request.FindNicknameRequest;
import com.misim.controller.model.Request.ResetPasswordRequest;
import com.misim.controller.model.Request.SendSMSRequest;
import com.misim.controller.model.Request.SignUpUserRequest;
import com.misim.controller.model.Request.VerifySMSRequest;
import com.misim.controller.model.Response.FindNicknameResponse;
import com.misim.controller.model.Response.VerifySMSResponse;
import com.misim.service.SmsService;
import com.misim.service.UserService;
import com.misim.service.VerificationTokenService;
import com.misim.util.TimeUtil;
import java.time.LocalDateTime;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -17,15 +30,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import java.time.LocalDateTime;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@WebMvcTest(UserController.class)
@WithMockUser
class UserControllerTest {
Expand Down Expand Up @@ -93,7 +97,7 @@ void checkSMSVerificationCodeByMocking() throws Exception {
mockRequest.setPhoneNumber("01012345678");
mockRequest.setCode("123456");
LocalDateTime mockCurrent = TimeUtil.getNow();
mockRequest.setRequestTime(TimeUtil.formatLocalDateTime(mockCurrent));
//mockRequest.setRequestTime(TimeUtil.formatLocalDateTime(mockCurrent));

VerifySMSResponse mockResponse = new VerifySMSResponse();
mockResponse.setToken("MQ==");
Expand Down Expand Up @@ -140,9 +144,9 @@ void resetPasswordByMocking() throws Exception {
// mock 객체
ResetPasswordRequest mockRequest = new ResetPasswordRequest();
mockRequest.setNickname("hongkildong");
mockRequest.setCode("MQ==");
//mockRequest.setCode("MQ==");

doNothing().when(userService).resetUserPassword(mockRequest.getNickname(), mockRequest.getCode());
//doNothing().when(userService).resetUserPassword(mockRequest.getNickname(), mockRequest.getCode());

// 실행 결과 확인
mockMvc.perform(post("/users/help/resetPassword")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
package com.misim.controller;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.misim.controller.model.Request.CreateVideoRequest;
import com.misim.controller.model.Response.CommentListResponse;
import com.misim.controller.model.Response.CommentResponse;
import com.misim.controller.model.Response.StartWatchingVideoResponse;
import com.misim.service.CommentService;
import com.misim.service.ReactionService;
import com.misim.service.VideoService;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand All @@ -14,13 +30,6 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

@WebMvcTest(VideoController.class)
@WithMockUser
class VideoControllerTest {
Expand All @@ -34,8 +43,14 @@ class VideoControllerTest {
@MockBean
private VideoService videoService;

@MockBean
private CommentService commentService;

@MockBean
private ReactionService reactionService;

@Test
void uploadVideosByMocking() throws Exception {
void uploadVideos_correctly_byMocking() throws Exception {

// mock 객체
MockMultipartFile file = new MockMultipartFile("file", "test file".getBytes());
Expand All @@ -56,7 +71,7 @@ void uploadVideosByMocking() throws Exception {
}

@Test
void createVideosByMocking() throws Exception {
void createVideos_correctly_byMocking() throws Exception {

// mock 객체
CreateVideoRequest mockCreateVideoRequest = new CreateVideoRequest();
Expand All @@ -75,4 +90,77 @@ void createVideosByMocking() throws Exception {
.with(csrf()))
.andExpect(status().isOk());
}

@Test
void startWatchingVideo_correctly_byMocking() throws Exception {

Long videoId = 1L;
Long userId = 1L;

// Mock videoService의 응답
StartWatchingVideoResponse startWatchingVideoResponse = StartWatchingVideoResponse.builder()
.videoId(videoId)
.watchingTime(1234L)
.views(10000L)
.videoLink("http://example.com/video")
.reactionResponse(null)
.build();

CommentResponse commentResponse = CommentResponse.builder()
.commentId(1L)
.content("mock video")
.writerNickname("user1")
.build();

CommentListResponse commentListResponse = CommentListResponse.builder()
.commentResponses(List.of(commentResponse))
.hasNext(false)
.build();

given(videoService.startWatchingVideo(videoId, userId)).willReturn(startWatchingVideoResponse);
given(commentService.getParentComments(videoId, null, "down")).willReturn(commentListResponse);

mockMvc.perform(get("/videos/watch/{videoId}", videoId)
.param("userId", userId.toString()))
.andExpect(status().isOk())
.andExpect(jsonPath("$.body.videoId").value(videoId))
.andExpect(jsonPath("$.body.watchingTime").value(1234))
.andExpect(jsonPath("$.body.views").value(10000))
.andExpect(jsonPath("$.body.videoLink").value("http://example.com/video"))
.andExpect(jsonPath("$.body.commentListResponse.commentResponses[0].commentId").value(1))
.andExpect(jsonPath("$.body.commentListResponse.commentResponses[0].content").value("mock video"))
.andExpect(jsonPath("$.body.commentListResponse.commentResponses[0].writerNickname").value("user1"))
.andExpect(jsonPath("$.body.commentListResponse.hasNext").value(false));
}

@Test
void watcingVideo_correctly_byMocking() throws Exception {

Long videoId = 1L;
Long userId = 1L;
Long watchingTime = 1234L;

mockMvc.perform(post("/videos/watch/{videoId}/update", 1L)
.param("userId", userId.toString())
.param("watchingTime", watchingTime.toString()))
.andExpect(status().isOk());
}

@Test
void completeWatcingVideo_correctly_byMocking() throws Exception {

Long videoId = 1L;
Long userId = 1L;
Long watchingTime = 1234L;

mockMvc.perform(post("/videos/watch/{videoId}/update", 1L)
.param("userId", userId.toString())
.param("watchingTime", watchingTime.toString()))
.andExpect(status().isOk());
}

@Test
void checkVideo_correctly_byMocking() throws Exception {

}
}
Loading

0 comments on commit 66e3590

Please sign in to comment.