Skip to content

Commit 9fedd67

Browse files
author
Eugen
committed
Merge pull request eugenp#144 from Doha2012/master
test search api
2 parents 538274b + fcdca0d commit 9fedd67

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package org.baeldung.persistence.query;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.baeldung.persistence.dao.UserRepository;
7+
import org.baeldung.persistence.model.User;
8+
import org.baeldung.spring.ConfigTest;
9+
import org.baeldung.spring.PersistenceConfig;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.test.context.ActiveProfiles;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
18+
19+
import com.jayway.restassured.RestAssured;
20+
import com.jayway.restassured.response.Response;
21+
import com.jayway.restassured.specification.RequestSpecification;
22+
23+
@RunWith(SpringJUnit4ClassRunner.class)
24+
@ContextConfiguration(classes = { ConfigTest.class, PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
25+
@ActiveProfiles("test")
26+
public class JPASpecificationLiveTest {
27+
28+
@Autowired
29+
private UserRepository repository;
30+
31+
private User userJohn;
32+
33+
private User userTom;
34+
35+
private final String URL_PREFIX = "http://localhost:8080/spring-security-rest-full/users/spec?search=";
36+
37+
@Before
38+
public void init() {
39+
userJohn = new User();
40+
userJohn.setFirstName("John");
41+
userJohn.setLastName("Doe");
42+
userJohn.setEmail("[email protected]");
43+
userJohn.setAge(22);
44+
repository.save(userJohn);
45+
46+
userTom = new User();
47+
userTom.setFirstName("Tom");
48+
userTom.setLastName("Doe");
49+
userTom.setEmail("[email protected]");
50+
userTom.setAge(26);
51+
repository.save(userTom);
52+
}
53+
54+
@Test
55+
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
56+
final Response response = givenAuth().get(URL_PREFIX + "firstName:john,lastName:doe");
57+
final String result = response.body().asString();
58+
59+
assertTrue(result.contains(userJohn.getEmail()));
60+
assertFalse(result.contains(userTom.getEmail()));
61+
}
62+
63+
@Test
64+
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
65+
final Response response = givenAuth().get(URL_PREFIX + "firstName!john");
66+
final String result = response.body().asString();
67+
68+
assertTrue(result.contains(userTom.getEmail()));
69+
assertFalse(result.contains(userJohn.getEmail()));
70+
}
71+
72+
@Test
73+
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
74+
final Response response = givenAuth().get(URL_PREFIX + "age>25");
75+
final String result = response.body().asString();
76+
77+
assertTrue(result.contains(userTom.getEmail()));
78+
assertFalse(result.contains(userJohn.getEmail()));
79+
}
80+
81+
@Test
82+
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
83+
final Response response = givenAuth().get(URL_PREFIX + "firstName:jo*");
84+
final String result = response.body().asString();
85+
86+
assertTrue(result.contains(userJohn.getEmail()));
87+
assertFalse(result.contains(userTom.getEmail()));
88+
}
89+
90+
@Test
91+
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
92+
final Response response = givenAuth().get(URL_PREFIX + "firstName:*n");
93+
final String result = response.body().asString();
94+
95+
assertTrue(result.contains(userJohn.getEmail()));
96+
assertFalse(result.contains(userTom.getEmail()));
97+
}
98+
99+
@Test
100+
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
101+
final Response response = givenAuth().get(URL_PREFIX + "firstName:*oh*");
102+
final String result = response.body().asString();
103+
104+
assertTrue(result.contains(userJohn.getEmail()));
105+
assertFalse(result.contains(userTom.getEmail()));
106+
}
107+
108+
@Test
109+
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
110+
final Response response = givenAuth().get(URL_PREFIX + "age>20,age<25");
111+
final String result = response.body().asString();
112+
113+
assertTrue(result.contains(userJohn.getEmail()));
114+
assertFalse(result.contains(userTom.getEmail()));
115+
}
116+
117+
private final RequestSpecification givenAuth() {
118+
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
119+
}
120+
}

0 commit comments

Comments
 (0)