fix jpa querydsl test
This commit is contained in:
+22
-18
@@ -1,12 +1,14 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsIn.isIn;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.IUserDAO;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.persistence.service.impl.UserService;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.junit.Before;
|
||||
@@ -26,7 +28,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class JPACriteriaQueryTest {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
private IUserDAO userApi;
|
||||
|
||||
private User userJohn;
|
||||
|
||||
@@ -39,14 +41,14 @@ public class JPACriteriaQueryTest {
|
||||
userJohn.setLastName("Doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
userService.saveUser(userJohn);
|
||||
userApi.save(userJohn);
|
||||
|
||||
userTom = new User();
|
||||
userTom.setFirstName("Tom");
|
||||
userTom.setLastName("Doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
userService.saveUser(userTom);
|
||||
userApi.save(userTom);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -55,10 +57,10 @@ public class JPACriteriaQueryTest {
|
||||
params.add(new SearchCriteria("firstName", ":", "John"));
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
|
||||
final List<User> result = userService.searchUser(params);
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(userJohn.getEmail(), result.get(0).getEmail());
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -66,8 +68,9 @@ public class JPACriteriaQueryTest {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
|
||||
final List<User> result = userService.searchUser(params);
|
||||
assertEquals(2, result.size());
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, isIn(results));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,10 +79,10 @@ public class JPACriteriaQueryTest {
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
params.add(new SearchCriteria("age", ">", "25"));
|
||||
|
||||
final List<User> result = userService.searchUser(params);
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(userTom.getEmail(), result.get(0).getEmail());
|
||||
assertThat(userTom, isIn(results));
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -88,8 +91,9 @@ public class JPACriteriaQueryTest {
|
||||
params.add(new SearchCriteria("firstName", ":", "Adam"));
|
||||
params.add(new SearchCriteria("lastName", ":", "Fox"));
|
||||
|
||||
final List<User> result = userService.searchUser(params);
|
||||
assertEquals(0, result.size());
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -97,9 +101,9 @@ public class JPACriteriaQueryTest {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "jo"));
|
||||
|
||||
final List<User> result = userService.searchUser(params);
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(userJohn.getEmail(), result.get(0).getEmail());
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
}
|
||||
+40
-21
@@ -1,17 +1,18 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.baeldung.persistence.dao.MyUserPrdicates.ageIsGreaterThan;
|
||||
import static org.baeldung.persistence.dao.MyUserPrdicates.firstNameIsLike;
|
||||
import static org.baeldung.persistence.dao.MyUserPrdicates.lastNameIsLike;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
|
||||
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.persistence.service.impl.MyUserService;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -29,7 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class JPAQuerydslTest {
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository repository;
|
||||
private MyUserService service;
|
||||
|
||||
private MyUser userJohn;
|
||||
|
||||
@@ -42,49 +43,67 @@ public class JPAQuerydslTest {
|
||||
userJohn.setLastName("Doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
repository.save(userJohn);
|
||||
service.save(userJohn);
|
||||
|
||||
userTom = new MyUser();
|
||||
userTom.setFirstName("Tom");
|
||||
userTom.setLastName("Doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
repository.save(userTom);
|
||||
service.save(userTom);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> result = repository.findAll(lastNameIsLike("doe"));
|
||||
assertThat(result, containsInAnyOrder(userJohn, userTom));
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
assertThat(results, containsInAnyOrder(userJohn, userTom));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> result = repository.findAll(lastNameIsLike("doe").and(firstNameIsLike("john")));
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "John"));
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
|
||||
assertThat(result, contains(userJohn));
|
||||
assertThat(result, not(contains(userTom)));
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
|
||||
assertThat(results, contains(userJohn));
|
||||
assertThat(results, not(contains(userTom)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> result = repository.findAll(lastNameIsLike("doe").and(ageIsGreaterThan(25)));
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "Doe"));
|
||||
params.add(new SearchCriteria("age", ">", "25"));
|
||||
|
||||
assertThat(result, contains(userTom));
|
||||
assertThat(result, not(contains(userJohn)));
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
|
||||
assertThat(results, contains(userTom));
|
||||
assertThat(results, not(contains(userJohn)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> result = repository.findAll(lastNameIsLike("Adam").and(firstNameIsLike("Fox")));
|
||||
assertThat(result, emptyIterable());
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "Adam"));
|
||||
params.add(new SearchCriteria("lastName", ":", "Fox"));
|
||||
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
assertThat(results, emptyIterable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
|
||||
final Iterable<MyUser> result = repository.findAll(firstNameIsLike("jo"));
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "jo"));
|
||||
|
||||
assertThat(result, contains(userJohn));
|
||||
assertThat(result, not(contains(userTom)));
|
||||
final Iterable<MyUser> results = service.search(params);
|
||||
|
||||
assertThat(results, contains(userJohn));
|
||||
assertThat(results, not(contains(userTom)));
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-14
@@ -1,6 +1,8 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsIn.isIn;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -53,46 +55,48 @@ public class JPASpecificationsTest {
|
||||
@Test
|
||||
public void givenLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SearchCriteria("lastName", ":", "doe"));
|
||||
final List<User> result = repository.findAll(spec);
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertEquals(2, result.size());
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, isIn(results));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SearchCriteria("firstName", ":", "john"));
|
||||
final UserSpecification spec1 = new UserSpecification(new SearchCriteria("lastName", ":", "doe"));
|
||||
final List<User> result = repository.findAll(Specifications.where(spec).and(spec1));
|
||||
final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(userJohn.getEmail(), result.get(0).getEmail());
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SearchCriteria("age", ">", "25"));
|
||||
final UserSpecification spec1 = new UserSpecification(new SearchCriteria("lastName", ":", "doe"));
|
||||
final List<User> result = repository.findAll(Specifications.where(spec).and(spec1));
|
||||
final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(userTom.getEmail(), result.get(0).getEmail());
|
||||
assertThat(userTom, isIn(results));
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SearchCriteria("firstName", ":", "Adam"));
|
||||
final UserSpecification spec1 = new UserSpecification(new SearchCriteria("lastName", ":", "Fox"));
|
||||
final List<User> result = repository.findAll(Specifications.where(spec).and(spec1));
|
||||
final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
|
||||
|
||||
assertEquals(0, result.size());
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SearchCriteria("firstName", ":", "jo"));
|
||||
final List<User> result = repository.findAll(spec);
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertEquals(1, result.size());
|
||||
assertEquals(userJohn.getEmail(), result.get(0).getEmail());
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user