[BAEL-2538] Added tutorial queries and tests
This commit is contained in:
+170
-35
@@ -1,12 +1,12 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
package com.baeldung.dao.repositories.user;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.dao.repositories.user.UserRepository;
|
||||
import com.baeldung.domain.user.User;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
@@ -17,13 +17,15 @@ import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
@@ -48,6 +50,8 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
@Autowired
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
@@ -270,9 +274,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithSortByNameThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
@@ -282,9 +286,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test(expected = PropertyReferenceException.class)
|
||||
public void givenUsersInDBWhenFindAllSortWithFunctionThenThrowException() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
@@ -296,9 +300,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllSortWithFunctionQueryAnnotationJPQLThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
|
||||
userRepository.findAllUsers(new Sort("name"));
|
||||
|
||||
@@ -310,12 +314,12 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationJPQLThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", LocalDate.now(), USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL6, INACTIVE_STATUS));
|
||||
|
||||
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
||||
|
||||
@@ -326,12 +330,12 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationNativeThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL6, INACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_ADAM, LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL4, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", LocalDate.now(), USER_EMAIL5, INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL6, INACTIVE_STATUS));
|
||||
|
||||
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
||||
|
||||
@@ -343,10 +347,10 @@ public class UserRepositoryIntegrationTest {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationJPQLThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
@@ -356,10 +360,10 @@ public class UserRepositoryIntegrationTest {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", LocalDate.now(), USER_EMAIL2, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", LocalDate.now(), USER_EMAIL3, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", LocalDate.now(), USER_EMAIL4, ACTIVE_STATUS));
|
||||
userRepository.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
||||
@@ -391,6 +395,137 @@ public class UserRepositoryIntegrationTest {
|
||||
assertThat(usersWithEmails.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenFindByNameUsr01_ThenUserUsr01() {
|
||||
User usr01 = new User("usr01", LocalDate.now(), "usr01@baeldung.com", 1);
|
||||
User usr02 = new User("usr02", LocalDate.now(), "usr02@baeldung.com", 1);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
try (Stream<User> users = userRepository.findAllByName("usr01")) {
|
||||
assertTrue(users.allMatch(usr -> usr.equals(usr01)));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenFindByNameUsr00_ThenNoUsers() {
|
||||
User usr01 = new User("usr01", LocalDate.now(), "usr01@baeldung.com", 1);
|
||||
User usr02 = new User("usr02", LocalDate.now(), "usr02@baeldung.com", 1);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
try (Stream<User> users = userRepository.findAllByName("usr00")) {
|
||||
assertEquals(0, users.count());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoUsers_whenFindUsersWithGmailAddress_ThenUserUsr02() {
|
||||
User usr01 = new User("usr01", LocalDate.now(), "usr01@baeldung.com", 1);
|
||||
User usr02 = new User("usr02", LocalDate.now(), "usr02@gmail.com", 1);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
List<User> users = userRepository.findUsersWithGmailAddress();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals(usr02, users.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenDeleteAllByCreationDateAfter_ThenOneUserRemains() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.com", 1);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
userRepository.deleteAllByCreationDateAfter(LocalDate.of(2018, 5, 1));
|
||||
|
||||
List<User> users = userRepository.findAll();
|
||||
|
||||
assertEquals(1, users.size());
|
||||
assertEquals(usr01, users.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoUsers_whenFindAllUsersByPredicates_ThenUserUsr01() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.org", 1);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
List<Predicate<User>> predicates = new ArrayList<>();
|
||||
predicates.add(usr -> usr.getCreationDate().isAfter(LocalDate.of(2017, 12, 31)));
|
||||
predicates.add(usr -> usr.getEmail().endsWith(".com"));
|
||||
|
||||
List<User> users = userRepository.findAllUsersByPredicates(predicates);
|
||||
|
||||
assertEquals(1, users.size());
|
||||
assertEquals(usr01, users.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenDeactivateUsersNotLoggedInSince_ThenUserUsr02Deactivated() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.org", 1);
|
||||
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
userRepository.deactivateUsersNotLoggedInSince(LocalDate.of(2018, 8, 1));
|
||||
|
||||
List<User> users = userRepository.findAllUsers(Sort.by(Sort.Order.asc("name")));
|
||||
assertTrue(users.get(0).isActive());
|
||||
assertFalse(users.get(1).isActive());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenDeleteDeactivatedUsers_ThenUserUsr02Deleted() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.org", 1);
|
||||
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||
usr02.setActive(false);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
userRepository.deleteDeactivatedUsers();
|
||||
|
||||
List<User> users = userRepository.findAll();
|
||||
assertEquals(1, users.size());
|
||||
assertEquals(usr01, users.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenTwoUsers_whenAddDeletedColumn_ThenUsersHaveDeletedColumn() {
|
||||
User usr01 = new User("usr01", LocalDate.of(2018, 1, 1), "usr01@baeldung.com", 1);
|
||||
usr01.setLastLoginDate(LocalDate.now());
|
||||
User usr02 = new User("usr02", LocalDate.of(2018, 6, 1), "usr02@baeldung.org", 1);
|
||||
usr02.setLastLoginDate(LocalDate.of(2018, 7, 20));
|
||||
usr02.setActive(false);
|
||||
|
||||
userRepository.save(usr01);
|
||||
userRepository.save(usr02);
|
||||
|
||||
userRepository.addDeletedColumn();
|
||||
|
||||
Query nativeQuery = entityManager.createNativeQuery("select deleted from USERS.USERS where NAME = 'usr01'");
|
||||
assertEquals(0, nativeQuery.getResultList().get(0));
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
userRepository.deleteAll();
|
||||
Reference in New Issue
Block a user