[BAEL-2048] Rearranging code
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package com.baeldung.repository;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.domain.Article;
|
||||
import com.baeldung.repository.ArticleRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -16,7 +18,7 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||
public class ArticleRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@@ -1,23 +1,21 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.baeldung.config.StudentJPAH2Config;
|
||||
import org.baeldung.extended.persistence.dao.ExtendedStudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.domain.Student;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { StudentJPAH2Config.class })
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {PersistenceConfiguration.class})
|
||||
@DirtiesContext
|
||||
public class ExtendedStudentRepositoryIntegrationTest {
|
||||
@Resource
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.repository;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
@@ -9,10 +9,19 @@ import static junit.framework.TestCase.assertTrue;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.dao.repositories.ItemTypeRepository;
|
||||
import com.baeldung.dao.repositories.LocationRepository;
|
||||
import com.baeldung.dao.repositories.ReadOnlyLocationRepository;
|
||||
import com.baeldung.dao.repositories.StoreRepository;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
@@ -21,7 +30,7 @@ import com.baeldung.domain.Location;
|
||||
import com.baeldung.domain.Store;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest
|
||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||
public class JpaRepositoriesIntegrationTest {
|
||||
@Autowired
|
||||
private LocationRepository locationRepository;
|
||||
@@ -1,23 +1,25 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import org.baeldung.config.PersistenceJPAConfigL2Cache;
|
||||
import org.baeldung.persistence.model.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.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.JpaSort;
|
||||
import org.springframework.data.mapping.PropertyReferenceException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -25,32 +27,71 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Created by adam.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = PersistenceJPAConfigL2Cache.class)
|
||||
@SpringBootTest(classes = PersistenceConfiguration.class)
|
||||
@DirtiesContext
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
private final String USER_NAME_ADAM = "Adam";
|
||||
private final String USER_NAME_PETER = "Peter";
|
||||
|
||||
private final String USER_EMAIL = "email@example.com";
|
||||
private final String USER_EMAIL2 = "email2@example.com";
|
||||
private final String USER_EMAIL3 = "email3@example.com";
|
||||
private final String USER_EMAIL4 = "email4@example.com";
|
||||
private final String USER_EMAIL5 = "email5@example.com";
|
||||
private final String USER_EMAIL6 = "email6@example.com";
|
||||
|
||||
private final Integer INACTIVE_STATUS = 0;
|
||||
private final Integer ACTIVE_STATUS = 1;
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersWithSameNameInDBWhenFindAllByNameThenReturnStreamOfUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
userRepository.save(user3);
|
||||
|
||||
User user4 = new User();
|
||||
user4.setName("SAMPLE");
|
||||
user4.setEmail(USER_EMAIL4);
|
||||
userRepository.save(user4);
|
||||
|
||||
try (Stream<User> foundUsersStream = userRepository.findAllByName(USER_NAME_ADAM)) {
|
||||
assertThat(foundUsersStream.count()).isEqualTo(3l);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithQueryAnnotationThenReturnCollectionWithActiveUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
user1.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
user3.setStatus(INACTIVE_STATUS);
|
||||
userRepository.save(user3);
|
||||
|
||||
@@ -63,16 +104,19 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindAllWithQueryAnnotationNativeThenReturnCollectionWithActiveUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
user1.setEmail(USER_EMAIL);
|
||||
user1.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
user3.setEmail(USER_EMAIL3);
|
||||
user3.setStatus(INACTIVE_STATUS);
|
||||
userRepository.save(user3);
|
||||
|
||||
@@ -85,6 +129,7 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUserInDBWhenFindUserByStatusWithQueryAnnotationThenReturnActiveUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
@@ -97,6 +142,7 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUserInDBWhenFindUserByStatusWithQueryAnnotationNativeThenReturnActiveUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
@@ -109,11 +155,13 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationIndexedParamsThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
@@ -126,11 +174,13 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
@@ -143,11 +193,13 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNativeNamedParamsThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
@@ -160,11 +212,13 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByStatusAndNameWithQueryAnnotationNamedParamsCustomNamesThenReturnOneUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_PETER);
|
||||
user2.setEmail(USER_EMAIL2);
|
||||
user2.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user2);
|
||||
|
||||
@@ -177,6 +231,7 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationIndexedParamsThenReturnUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
@@ -189,6 +244,7 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationNamedParamsThenReturnUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
@@ -201,6 +257,7 @@ public class UserRepositoryIntegrationTest {
|
||||
public void givenUsersInDBWhenFindUserByNameLikeWithQueryAnnotationNativeThenReturnUser() {
|
||||
User user = new User();
|
||||
user.setName(USER_NAME_ADAM);
|
||||
user.setEmail(USER_EMAIL);
|
||||
user.setStatus(ACTIVE_STATUS);
|
||||
userRepository.save(user);
|
||||
|
||||
@@ -211,9 +268,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithSortByNameThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
||||
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));
|
||||
|
||||
List<User> usersSortByName = userRepository.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
@@ -224,9 +281,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test(expected = PropertyReferenceException.class)
|
||||
public void givenUsersInDBWhenFindAllSortWithFunctionThenThrowException() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
||||
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.findAll(new Sort(Sort.Direction.ASC, "name"));
|
||||
|
||||
@@ -239,9 +296,9 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllSortWithFunctionQueryAnnotationJPQLThenReturnUsersSorted() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
||||
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.findAllUsers(new Sort("name"));
|
||||
|
||||
@@ -254,12 +311,12 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationJPQLThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", INACTIVE_STATUS));
|
||||
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));
|
||||
|
||||
Page<User> usersPage = userRepository.findAllUsersWithPagination(new PageRequest(1, 3));
|
||||
|
||||
@@ -271,12 +328,12 @@ public class UserRepositoryIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenUsersInDBWhenFindAllWithPageRequestQueryAnnotationNativeThenReturnPageOfUsers() {
|
||||
userRepository.save(new User(USER_NAME_ADAM, ACTIVE_STATUS));
|
||||
userRepository.save(new User(USER_NAME_PETER, ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE2", INACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", INACTIVE_STATUS));
|
||||
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));
|
||||
|
||||
Page<User> usersSortByNameLength = userRepository.findAllUsersWithPaginationNative(new PageRequest(1, 3));
|
||||
|
||||
@@ -289,10 +346,10 @@ public class UserRepositoryIntegrationTest {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationJPQLThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", ACTIVE_STATUS));
|
||||
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));
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForName(INACTIVE_STATUS, "SAMPLE");
|
||||
|
||||
@@ -302,10 +359,10 @@ public class UserRepositoryIntegrationTest {
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersInDBWhenUpdateStatusForNameModifyingQueryAnnotationNativeThenModifyMatchingUsers() {
|
||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE1", ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE", ACTIVE_STATUS));
|
||||
userRepository.save(new User("SAMPLE3", ACTIVE_STATUS));
|
||||
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.flush();
|
||||
|
||||
int updatedUsersSize = userRepository.updateUserSetStatusForNameNative(INACTIVE_STATUS, "SAMPLE");
|
||||
@@ -1,8 +1,7 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import org.baeldung.persistence.IOperations;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.util.IDUtil;
|
||||
import com.baeldung.domain.Foo;
|
||||
import com.baeldung.util.IDUtil;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@@ -1,8 +1,7 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import org.baeldung.persistence.IOperations;
|
||||
import org.baeldung.persistence.model.Foo;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.domain.Foo;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -10,14 +9,14 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {PersistenceConfiguration.class}, loader = AnnotationConfigContextLoader.class)
|
||||
public class FooServicePersistenceIntegrationTest extends AbstractServicePersistenceIntegrationTest<Foo> {
|
||||
|
||||
@Autowired
|
||||
@@ -1,13 +1,13 @@
|
||||
package org.baeldung.persistence.service;
|
||||
package com.baeldung.services;
|
||||
|
||||
import org.baeldung.config.ProductConfig;
|
||||
import org.baeldung.config.UserConfig;
|
||||
import org.baeldung.persistence.multiple.dao.product.ProductRepository;
|
||||
import org.baeldung.persistence.multiple.dao.user.PossessionRepository;
|
||||
import org.baeldung.persistence.multiple.dao.user.UserRepository;
|
||||
import org.baeldung.persistence.multiple.model.product.Product;
|
||||
import org.baeldung.persistence.multiple.model.user.Possession;
|
||||
import org.baeldung.persistence.multiple.model.user.User;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.dao.repositories.user.PossessionRepository;
|
||||
import com.baeldung.dao.repositories.product.ProductRepository;
|
||||
import com.baeldung.dao.repositories.user.UserRepository;
|
||||
import com.baeldung.domain.user.Possession;
|
||||
import com.baeldung.domain.product.Product;
|
||||
import com.baeldung.domain.user.User;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -15,6 +15,7 @@ import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -23,8 +24,8 @@ import java.util.Optional;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { UserConfig.class, ProductConfig.class })
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = {PersistenceUserConfiguration.class, PersistenceProductConfiguration.class})
|
||||
@EnableTransactionManagement
|
||||
@DirtiesContext
|
||||
public class JpaMultipleDBIntegrationTest {
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.baeldung.persistence.audit;
|
||||
package com.baeldung.services;
|
||||
|
||||
import com.baeldung.persistence.model.Bar;
|
||||
import com.baeldung.persistence.service.IBarService;
|
||||
import com.baeldung.spring.config.PersistenceTestConfig;
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.domain.Bar;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
@@ -12,6 +11,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.security.test.context.support.WithMockUser;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
@@ -20,8 +20,8 @@ import javax.persistence.EntityManagerFactory;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceTestConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfiguration.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class SpringDataJPABarAuditIntegrationTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class);
|
||||
@@ -1,169 +0,0 @@
|
||||
package com.baeldung.spring.config;
|
||||
|
||||
import com.baeldung.persistence.dao.IBarAuditableDao;
|
||||
import com.baeldung.persistence.dao.IBarDao;
|
||||
import com.baeldung.persistence.dao.IFooAuditableDao;
|
||||
import com.baeldung.persistence.dao.IFooDao;
|
||||
import com.baeldung.persistence.dao.impl.*;
|
||||
import com.baeldung.persistence.service.IBarAuditableService;
|
||||
import com.baeldung.persistence.service.IBarService;
|
||||
import com.baeldung.persistence.service.IFooAuditableService;
|
||||
import com.baeldung.persistence.service.IFooService;
|
||||
import com.baeldung.persistence.service.impl.*;
|
||||
import com.google.common.base.Preconditions;
|
||||
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Properties;
|
||||
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager")
|
||||
@EnableJpaAuditing
|
||||
@PropertySource({ "classpath:persistence-h2.properties" })
|
||||
@ComponentScan({ "com.baeldung.persistence" })
|
||||
public class PersistenceTestConfig {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
public PersistenceTestConfig() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalSessionFactoryBean sessionFactory() {
|
||||
final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
|
||||
sessionFactory.setDataSource(restDataSource());
|
||||
sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||
sessionFactory.setHibernateProperties(hibernateProperties());
|
||||
|
||||
return sessionFactory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
|
||||
emf.setDataSource(restDataSource());
|
||||
emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" });
|
||||
|
||||
final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||
emf.setJpaVendorAdapter(vendorAdapter);
|
||||
emf.setJpaProperties(hibernateProperties());
|
||||
|
||||
return emf;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public DataSource restDataSource() {
|
||||
final BasicDataSource dataSource = new BasicDataSource();
|
||||
dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName")));
|
||||
dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url")));
|
||||
dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user")));
|
||||
dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass")));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager hibernateTransactionManager() {
|
||||
final HibernateTransactionManager transactionManager = new HibernateTransactionManager();
|
||||
transactionManager.setSessionFactory(sessionFactory().getObject());
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PlatformTransactionManager jpaTransactionManager() {
|
||||
final JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
|
||||
return new PersistenceExceptionTranslationPostProcessor();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IBarService barJpaService() {
|
||||
return new BarJpaService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IBarService barSpringDataJpaService() {
|
||||
return new BarSpringDataJpaService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IFooService fooHibernateService() {
|
||||
return new FooService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IBarAuditableService barHibernateAuditableService() {
|
||||
return new BarAuditableService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IFooAuditableService fooHibernateAuditableService() {
|
||||
return new FooAuditableService();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IBarDao barJpaDao() {
|
||||
return new BarJpaDao();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IBarDao barHibernateDao() {
|
||||
return new BarDao();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IBarAuditableDao barHibernateAuditableDao() {
|
||||
return new BarAuditableDao();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IFooDao fooHibernateDao() {
|
||||
return new FooDao();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IFooAuditableDao fooHibernateAuditableDao() {
|
||||
return new FooAuditableDao();
|
||||
}
|
||||
|
||||
private final Properties hibernateProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
|
||||
hibernateProperties.setProperty("hibernate.show_sql", "true");
|
||||
// hibernateProperties.setProperty("hibernate.format_sql", "true");
|
||||
// hibernateProperties.setProperty("hibernate.globally_quoted_identifiers", "true");
|
||||
|
||||
// Envers properties
|
||||
hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix"));
|
||||
|
||||
return hibernateProperties;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.baeldung.util;
|
||||
package com.baeldung.util;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
package org.baeldung.repository;
|
||||
|
||||
import org.baeldung.config.H2JpaConfig;
|
||||
import org.baeldung.persistence.model.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.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = H2JpaConfig.class)
|
||||
public class UserRepositoryIntegrationTest {
|
||||
|
||||
private final String USER_NAME_ADAM = "Adam";
|
||||
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void givenUsersWithSameNameInDBWhenFindAllByNameThenReturnStreamOfUsers() {
|
||||
User user1 = new User();
|
||||
user1.setName(USER_NAME_ADAM);
|
||||
userRepository.save(user1);
|
||||
|
||||
User user2 = new User();
|
||||
user2.setName(USER_NAME_ADAM);
|
||||
userRepository.save(user2);
|
||||
|
||||
User user3 = new User();
|
||||
user3.setName(USER_NAME_ADAM);
|
||||
userRepository.save(user3);
|
||||
|
||||
User user4 = new User();
|
||||
user4.setName("SAMPLE");
|
||||
userRepository.save(user4);
|
||||
|
||||
try (Stream<User> foundUsersStream = userRepository.findAllByName(USER_NAME_ADAM)) {
|
||||
assertThat(foundUsersStream.count()).isEqualTo(3l);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanUp() {
|
||||
userRepository.deleteAll();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user