|
| 1 | +package com.sopromadze.blogapi.serviceTest.post; |
| 2 | + |
| 3 | +import com.sopromadze.blogapi.exception.ResourceNotFoundException; |
| 4 | +import com.sopromadze.blogapi.model.Category; |
| 5 | +import com.sopromadze.blogapi.model.Post; |
| 6 | +import com.sopromadze.blogapi.model.role.Role; |
| 7 | +import com.sopromadze.blogapi.model.role.RoleName; |
| 8 | +import com.sopromadze.blogapi.model.user.User; |
| 9 | +import com.sopromadze.blogapi.payload.PagedResponse; |
| 10 | +import com.sopromadze.blogapi.repository.CategoryRepository; |
| 11 | +import com.sopromadze.blogapi.repository.PostRepository; |
| 12 | +import com.sopromadze.blogapi.service.impl.PostServiceImpl; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 17 | +import org.mockito.InjectMocks; |
| 18 | +import org.mockito.Mock; |
| 19 | +import org.mockito.Mockito; |
| 20 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 21 | +import org.mockito.junit.jupiter.MockitoSettings; |
| 22 | +import org.mockito.quality.Strictness; |
| 23 | +import org.springframework.data.domain.Page; |
| 24 | +import org.springframework.data.domain.PageImpl; |
| 25 | +import org.springframework.data.domain.PageRequest; |
| 26 | +import org.springframework.data.domain.Pageable; |
| 27 | + |
| 28 | +import java.time.Instant; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | + |
| 38 | +@ExtendWith (MockitoExtension.class) |
| 39 | +@MockitoSettings (strictness = Strictness.LENIENT) |
| 40 | +public class GetPostByCategory { |
| 41 | + |
| 42 | + @Mock |
| 43 | + private PostRepository postRepository; |
| 44 | + |
| 45 | + @Mock |
| 46 | + private CategoryRepository categoryRepository; |
| 47 | + |
| 48 | + @InjectMocks |
| 49 | + private PostServiceImpl postService; |
| 50 | + |
| 51 | + Pageable pageable; |
| 52 | + Post post; |
| 53 | + Category category; |
| 54 | + Page<Post> posts; |
| 55 | + List<Post> content; |
| 56 | + PagedResponse pagedResponse; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + void init(){ |
| 60 | + category = new Category(); |
| 61 | + category.setId(1L); |
| 62 | + category.setName("Categoria"); |
| 63 | + |
| 64 | + post = new Post(); |
| 65 | + post.setId(1L); |
| 66 | + post.setTitle("Post"); |
| 67 | + category.setPosts(List.of(post)); |
| 68 | + |
| 69 | + |
| 70 | + pageable = PageRequest.of(1,1); |
| 71 | + posts = new PageImpl<>(List.of(post)); |
| 72 | + |
| 73 | + |
| 74 | + pagedResponse = new PagedResponse(); |
| 75 | + pagedResponse.setContent(posts.getContent()); |
| 76 | + pagedResponse.setLast(true); |
| 77 | + pagedResponse.setPage(0); |
| 78 | + pagedResponse.setSize(1); |
| 79 | + pagedResponse.setTotalElements(1); |
| 80 | + pagedResponse.setTotalPages(1); |
| 81 | + } |
| 82 | + |
| 83 | + /* |
| 84 | + Test: Obtener los posts de una categoria |
| 85 | + Entrada: id(category) |
| 86 | + Salida esperada: El test se realiza con exito cuando obtienen los posts por su id de categoria |
| 87 | + */ |
| 88 | + @Test |
| 89 | + @DisplayName ("Get posts by category successfully") |
| 90 | + void getPostsByCategory_success(){ |
| 91 | + when(categoryRepository.findById(1L)).thenReturn(java.util.Optional.of(category)); |
| 92 | + when(postRepository.findByCategoryId(Mockito.any(),Mockito.any())).thenReturn(posts); |
| 93 | + |
| 94 | + assertEquals(pagedResponse,postService.getPostsByCategory(1L,1,1)); |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + /* |
| 99 | + Test: Al obtener los posts por su categoria, salte excepcion ResourceNotFoundException |
| 100 | + Entrada: id(category) |
| 101 | + Salida esperada: El test se realiza con exito cuando obtiene la excepcion en la peticion |
| 102 | + */ |
| 103 | + @Test |
| 104 | + @DisplayName ("Get posts by category exception ResourceNotFoundException") |
| 105 | + void getPostsByCategory_failException(){ |
| 106 | + when(categoryRepository.findById(1L)).thenReturn(java.util.Optional.of(category)); |
| 107 | + |
| 108 | + assertThrows(ResourceNotFoundException.class, () -> postService.getPostsByCategory(0L,1,1)); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments