#24 simple sns: 피드 목록, 내 피드 목록 api

This commit is contained in:
haerong22
2022-11-22 00:57:44 +09:00
parent d4458cc646
commit 48f3b8e7bb
5 changed files with 106 additions and 0 deletions

View File

@@ -7,6 +7,8 @@ import com.example.sns.controller.response.Response;
import com.example.sns.model.Post;
import com.example.sns.service.PostService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
@@ -42,4 +44,14 @@ public class PostController {
postService.delete(authentication.getName(), postId);
return Response.success();
}
@GetMapping
public Response<Page<PostResponse>> list(Pageable pageable, Authentication authentication) {
return Response.success(postService.list(pageable).map(PostResponse::fromPost));
}
@GetMapping("/my")
public Response<Page<PostResponse>> my(Pageable pageable, Authentication authentication) {
return Response.success(postService.my(authentication.getName(), pageable).map(PostResponse::fromPost));
}
}

View File

@@ -1,9 +1,14 @@
package com.example.sns.repository;
import com.example.sns.model.entity.PostEntity;
import com.example.sns.model.entity.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface PostEntityRepository extends JpaRepository<PostEntity, Integer> {
Page<PostEntity> findAllByUser(UserEntity entity, Pageable pageable);
}

View File

@@ -7,6 +7,8 @@ import com.example.sns.model.entity.UserEntity;
import com.example.sns.repository.PostEntityRepository;
import com.example.sns.repository.UserEntityRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -90,4 +92,19 @@ public class PostService {
postEntityRepository.delete(postEntity);
}
public Page<Post> list(Pageable pageable) {
return postEntityRepository.findAll(pageable).map(Post::fromEntity);
}
public Page<Post> my(String username, Pageable pageable) {
UserEntity userEntity = userEntityRepository.findByUsername(username)
.orElseThrow(
() -> new SnsApplicationException(
USER_NOT_FOUND,
String.format("%s not founded", username)
)
);
return postEntityRepository.findAllByUser(userEntity, pageable).map(Post::fromEntity);
}
}

View File

@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithAnonymousUser;
import org.springframework.security.test.context.support.WithMockUser;
@@ -177,4 +178,52 @@ public class PostControllerTest {
).andDo(print())
.andExpect(status().isNotFound());
}
@Test
@WithMockUser
void 피드목록() throws Exception {
when(postService.list(any())).thenReturn(Page.empty());
mockMvc.perform(get("/api/v1/posts")
.contentType(MediaType.APPLICATION_JSON)
).andDo(print())
.andExpect(status().isOk());
}
@Test
@WithAnonymousUser
void 피드목록요청시_로그인하지_않은경우() throws Exception {
when(postService.list(any())).thenReturn(Page.empty());
mockMvc.perform(get("/api/v1/posts")
.contentType(MediaType.APPLICATION_JSON)
).andDo(print())
.andExpect(status().isUnauthorized());
}
@Test
@WithMockUser
void 내피드목록() throws Exception {
when(postService.my(any(), any())).thenReturn(Page.empty());
mockMvc.perform(get("/api/v1/posts/my")
.contentType(MediaType.APPLICATION_JSON)
).andDo(print())
.andExpect(status().isOk());
}
@Test
@WithAnonymousUser
void 내피드목록요청시_로그인하지_않은경우() throws Exception {
when(postService.my(any(), any())).thenReturn(Page.empty());
mockMvc.perform(get("/api/v1/posts/my")
.contentType(MediaType.APPLICATION_JSON)
).andDo(print())
.andExpect(status().isUnauthorized());
}
}

View File

@@ -12,11 +12,14 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -164,4 +167,24 @@ public class PostServiceTest {
assertEquals(ErrorCode.INVALID_PERMISSION, e.getErrorCode());
}
@Test
void 피드목록요청이_성공한경우() {
Pageable pageable = mock(Pageable.class);
when(postEntityRepository.findAll(pageable)).thenReturn(Page.empty());
assertDoesNotThrow(() -> postService.list(pageable));
}
@Test
void 내피드목록요청이_성공한경우() {
Pageable pageable = mock(Pageable.class);
UserEntity user = mock(UserEntity.class);
when(userEntityRepository.findByUsername(any())).thenReturn(Optional.of(user));
when(postEntityRepository.findAllByUser(user, pageable)).thenReturn(Page.empty());
assertDoesNotThrow(() -> postService.my("", pageable));
}
}