Skip to content

Commit 40e3ad0

Browse files
committed
Tests for repository > PostRepository > findByCategoryId:
Previously called findByCategory, changed for causing a wrong query.
1 parent 28a1d65 commit 40e3ad0

4 files changed

Lines changed: 114 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public interface PostRepository extends JpaRepository<Post, Long> {
1414
Page<Post> findByCreatedBy(Long userId, Pageable pageable);
1515

16-
Page<Post> findByCategory(Long categoryId, Pageable pageable);
16+
Page<Post> findByCategoryId(Long categoryId, Pageable pageable);
1717

1818
Page<Post> findByTagsIn(List<Tag> tags, Pageable pageable);
1919

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public PagedResponse<Post> getPostsByCategory(Long id, int page, int size) {
8686
.orElseThrow(() -> new ResourceNotFoundException(CATEGORY, ID, id));
8787

8888
Pageable pageable = PageRequest.of(page, size, Sort.Direction.DESC, CREATED_AT);
89-
Page<Post> posts = postRepository.findByCategory(category.getId(), pageable);
89+
Page<Post> posts = postRepository.findByCategoryId(category.getId(), pageable);
9090

9191
List<Post> content = posts.getNumberOfElements() == 0 ? Collections.emptyList() : posts.getContent();
9292

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.sopromadze.blogapi.repositoryTest.todo;
2+
3+
public class FindByCreatedByTest {
4+
}

0 commit comments

Comments
 (0)