|
| 1 | +package com.sopromadze.blogapi.repositoryTest.album; |
| 2 | + |
| 3 | +import com.sopromadze.blogapi.model.Album; |
| 4 | +import com.sopromadze.blogapi.model.Photo; |
| 5 | +import com.sopromadze.blogapi.model.user.User; |
| 6 | +import com.sopromadze.blogapi.repository.AlbumRepository; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.junit.jupiter.api.DisplayName; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; |
| 12 | +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; |
| 13 | +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; |
| 14 | +import org.springframework.data.domain.Page; |
| 15 | +import org.springframework.data.domain.PageRequest; |
| 16 | +import org.springframework.data.domain.Pageable; |
| 17 | +import org.springframework.data.domain.Sort; |
| 18 | +import org.springframework.test.context.ActiveProfiles; |
| 19 | + |
| 20 | +import java.time.Instant; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import static com.sopromadze.blogapi.utils.AppConstants.CREATED_AT; |
| 25 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 27 | + |
| 28 | +@DataJpaTest |
| 29 | +@ActiveProfiles("test") |
| 30 | +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) |
| 31 | +public class FindByUserId { |
| 32 | + |
| 33 | + @Autowired |
| 34 | + private AlbumRepository albumRepository; |
| 35 | + |
| 36 | + @Autowired |
| 37 | + private TestEntityManager testEntityManager; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void init() { |
| 41 | + //User creation |
| 42 | + User u = new User(); |
| 43 | + u.setEmail("user1@gmail.com"); |
| 44 | + u.setFirstName("FirstName1"); |
| 45 | + u.setUsername("Username1"); |
| 46 | + u.setLastName("Lastname1"); |
| 47 | + u.setPassword("password"); |
| 48 | + u.setCreatedAt(Instant.now()); |
| 49 | + u.setUpdatedAt(Instant.now()); |
| 50 | + |
| 51 | + //Album creation |
| 52 | + Album a = new Album(); |
| 53 | + a.setTitle("Title1"); |
| 54 | + a.setUser(u); |
| 55 | + a.setCreatedAt(Instant.now()); |
| 56 | + a.setUpdatedAt(Instant.now()); |
| 57 | + |
| 58 | + //Photo creation |
| 59 | + Photo p1 = new Photo(); |
| 60 | + p1.setTitle("Title1"); |
| 61 | + p1.setUrl("Url1"); |
| 62 | + p1.setThumbnailUrl("ThumbnailUrl1"); |
| 63 | + p1.setAlbum(a); |
| 64 | + p1.setCreatedAt(Instant.now()); |
| 65 | + p1.setUpdatedAt(Instant.now()); |
| 66 | + |
| 67 | + List<Photo> photos = new ArrayList<Photo>(); |
| 68 | + |
| 69 | + |
| 70 | + a.setPhoto(photos); |
| 71 | + |
| 72 | + testEntityManager.persist(u); |
| 73 | + testEntityManager.persist(a); |
| 74 | + testEntityManager.persist(p1); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + @DisplayName("Find by created by") |
| 80 | + void findByCreatedBy_success() { |
| 81 | + |
| 82 | + Pageable pageable = PageRequest.of(1, 25, Sort.Direction.DESC, CREATED_AT); |
| 83 | + |
| 84 | + Page<Album> result = albumRepository.findByUserId(1L, pageable); |
| 85 | + |
| 86 | + assertTrue(result.getTotalElements()!=0); |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + @DisplayName("Find by created by") |
| 92 | + void findByCreatedBy_fail() { |
| 93 | + |
| 94 | + Pageable pageable = PageRequest.of(1, 25, Sort.Direction.DESC, CREATED_AT); |
| 95 | + |
| 96 | + Page<Album> result = albumRepository.findByUserId(0L, pageable); |
| 97 | + |
| 98 | + assertFalse(result.getTotalElements()!=0); |
| 99 | + |
| 100 | + } |
| 101 | +} |
0 commit comments