|
| 1 | +package com.sopromadze.blogapi.repositoryTest.post; |
| 2 | + |
| 3 | +import com.sopromadze.blogapi.model.Category; |
| 4 | +import com.sopromadze.blogapi.model.Comment; |
| 5 | +import com.sopromadze.blogapi.model.Post; |
| 6 | +import com.sopromadze.blogapi.model.Tag; |
| 7 | +import com.sopromadze.blogapi.model.user.User; |
| 8 | +import com.sopromadze.blogapi.repository.PostRepository; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.BeforeEach; |
| 11 | +import org.junit.jupiter.api.DisplayName; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 15 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 16 | +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; |
| 17 | +import org.springframework.data.domain.Page; |
| 18 | +import org.springframework.data.domain.PageRequest; |
| 19 | +import org.springframework.data.domain.Pageable; |
| 20 | +import org.springframework.data.domain.Sort; |
| 21 | +import org.springframework.test.context.ActiveProfiles; |
| 22 | + |
| 23 | +import java.time.Instant; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.*; |
| 28 | + |
| 29 | +import static com.sopromadze.blogapi.utils.AppConstants.CREATED_AT; |
| 30 | + |
| 31 | +@DataJpaTest |
| 32 | +@ActiveProfiles("test") |
| 33 | +@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) |
| 34 | +public class FindByCategoryTest { |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private PostRepository postRepository; |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private TestEntityManager testEntityManager; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void init(){ |
| 44 | + //Category creation |
| 45 | + Category ca = new Category("category"); |
| 46 | + ca.setCreatedAt(Instant.now()); |
| 47 | + ca.setUpdatedAt(Instant.now()); |
| 48 | + |
| 49 | + //Post creation |
| 50 | + Post p= new Post(); |
| 51 | + p.setCategory(ca); |
| 52 | + p.setCreatedAt(Instant.now()); |
| 53 | + p.setUpdatedAt(Instant.now()); |
| 54 | + p.setTitle("post"); |
| 55 | + p.setBody("body"); |
| 56 | + |
| 57 | + //Comments creation |
| 58 | + List<Comment> coments= new ArrayList<Comment>(); |
| 59 | + p.setComments(coments); |
| 60 | + |
| 61 | + //User creation |
| 62 | + User user = new User(); |
| 63 | + user.setEmail("user@email.com"); |
| 64 | + user.setFirstName("FirstName"); |
| 65 | + user.setUsername("UserName"); |
| 66 | + user.setLastName("LastName"); |
| 67 | + user.setPassword("password"); |
| 68 | + user.setCreatedAt(Instant.now()); |
| 69 | + user.setUpdatedAt(Instant.now()); |
| 70 | + |
| 71 | + p.setUser(user); |
| 72 | + List<Tag> tgs= new ArrayList<Tag>(); |
| 73 | + p.setTags(tgs); |
| 74 | + List<Post> lp= new ArrayList<Post>(); |
| 75 | + |
| 76 | + testEntityManager.persist(p); |
| 77 | + testEntityManager.persist(ca); |
| 78 | + testEntityManager.persist(user); |
| 79 | + } |
| 80 | + |
| 81 | + //Test: Find by category id. |
| 82 | + //Entrada: Long categoryId, Pageable |
| 83 | + //Salida esperada: Page<post from category name> |
| 84 | + @Test |
| 85 | + @DisplayName("Find by category") |
| 86 | + void findByCateogy_success(){ |
| 87 | + |
| 88 | + Pageable pageable = PageRequest.of(1, 25, Sort.Direction.DESC, CREATED_AT); |
| 89 | + |
| 90 | + Page<Post> result =postRepository.findByCategoryId(1L,pageable); |
| 91 | + |
| 92 | + assertTrue(result.getTotalElements()!=0); |
| 93 | + } |
| 94 | + |
| 95 | + //Test: Find by non-existent category . |
| 96 | + //Entrada: Long categoryId, Pageable |
| 97 | + //Salida esperada: page with no elements |
| 98 | + @Test |
| 99 | + @DisplayName("Find by non-existent category") |
| 100 | + void findByCateogy_fail(){ |
| 101 | + |
| 102 | + Pageable pageable = PageRequest.of(1, 25, Sort.Direction.DESC, CREATED_AT); |
| 103 | + |
| 104 | + Page<Post> result =postRepository.findByCategoryId(0L,pageable); |
| 105 | + |
| 106 | + assertFalse(result.getTotalElements()!=0); |
| 107 | + } |
| 108 | +} |
0 commit comments