Skip to content

Commit 3fad855

Browse files
Test AlbumRepository findByUserId
1 parent 1f31460 commit 3fad855

4 files changed

Lines changed: 104 additions & 3 deletions

File tree

src/main/java/com/sopromadze/blogapi/repository/AlbumRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88

99
@Repository
1010
public interface AlbumRepository extends JpaRepository<Album, Long> {
11-
Page<Album> findByCreatedBy(Long userId, Pageable pageable);
11+
Page<Album> findByUserId(Long userId, Pageable pageable);
1212
}

src/main/java/com/sopromadze/blogapi/service/impl/AlbumServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public PagedResponse<Album> getUserAlbums(String username, int page, int size) {
124124

125125
Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);
126126

127-
Page<Album> albums = albumRepository.findByCreatedBy(user.getId(), pageable);
127+
Page<Album> albums = albumRepository.findByUserId(user.getId(), pageable);
128128

129129
List<Album> content = albums.getNumberOfElements() > 0 ? albums.getContent() : Collections.emptyList();
130130

src/test/java/com/sopromadze/blogapi/VerifyMockito.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void testWithMockito_SuccessYaPorFin() {
6262

6363
when(userRepository.getUserByName("user")).thenReturn(user);
6464

65-
when(albumRepository.findByCreatedBy(any(Long.class), any(Pageable.class))).thenReturn(pageResult);
65+
when(albumRepository.findByUserId(any(Long.class), any(Pageable.class))).thenReturn(pageResult);
6666

6767
assertEquals(result, albumService.getUserAlbums("user", 0, 10));
6868

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)