resolve merge conflict
This commit is contained in:
commit
9116b6ab40
@ -2,8 +2,10 @@ package com.baeldung.multipledb.dao.product;
|
||||
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
|
||||
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
||||
import com.baeldung.multipledb.model.product.Product;
|
||||
|
||||
public interface ProductRepository extends PagingAndSortingRepository<ProductMultipleDB, Integer> {
|
||||
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
|
||||
|
||||
}
|
||||
|
||||
List<Product> findAllByPrice(double price, Pageable pageable);
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(schema = "products")
|
||||
public class ProductMultipleDB {
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
@ -15,19 +15,19 @@ public class ProductMultipleDB {
|
||||
|
||||
private double price;
|
||||
|
||||
public ProductMultipleDB() {
|
||||
public Product() {
|
||||
super();
|
||||
}
|
||||
|
||||
private ProductMultipleDB(int id, String name, double price) {
|
||||
private Product(int id, String name, double price) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public static ProductMultipleDB from(int id, String name, double price) {
|
||||
return new ProductMultipleDB(id, name, price);
|
||||
public static Product from(int id, String name, double price) {
|
||||
return new Product(id, name, price);
|
||||
}
|
||||
|
||||
public int getId() {
|
@ -19,7 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
||||
import com.baeldung.multipledb.dao.user.UserRepository;
|
||||
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
||||
import com.baeldung.multipledb.model.product.Product;
|
||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||
|
||||
@ -84,7 +84,7 @@ public class JpaMultipleDBIntegrationTest {
|
||||
@Test
|
||||
@Transactional("productTransactionManager")
|
||||
public void whenCreatingProduct_thenCreated() {
|
||||
ProductMultipleDB product = new ProductMultipleDB();
|
||||
Product product = new Product();
|
||||
product.setName("Book");
|
||||
product.setId(2);
|
||||
product.setPrice(20);
|
||||
|
@ -0,0 +1,144 @@
|
||||
package com.baeldung.multipledb;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.junit.Before;
|
||||
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.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||
import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||
import com.baeldung.multipledb.model.product.Product;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||
@EnableTransactionManagement
|
||||
public class ProductRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private ProductRepository productRepository;
|
||||
|
||||
@Before
|
||||
@Transactional("productTransactionManager")
|
||||
public void setUp() {
|
||||
productRepository.save(Product.from(1001, "Book", 21));
|
||||
productRepository.save(Product.from(1002, "Coffee", 10));
|
||||
productRepository.save(Product.from(1003, "Jeans", 30));
|
||||
productRepository.save(Product.from(1004, "Shirt", 32));
|
||||
productRepository.save(Product.from(1005, "Bacon", 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() {
|
||||
Pageable pageRequest = PageRequest.of(0, 2);
|
||||
|
||||
Page<Product> result = productRepository.findAll(pageRequest);
|
||||
|
||||
assertThat(result.getContent(), hasSize(2));
|
||||
assertTrue(result.stream()
|
||||
.map(Product::getId)
|
||||
.allMatch(id -> Arrays.asList(1001, 1002)
|
||||
.contains(id)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() {
|
||||
Pageable pageRequest = PageRequest.of(1, 2);
|
||||
|
||||
Page<Product> result = productRepository.findAll(pageRequest);
|
||||
|
||||
assertThat(result.getContent(), hasSize(2));
|
||||
assertTrue(result.stream()
|
||||
.map(Product::getId)
|
||||
.allMatch(id -> Arrays.asList(1003, 1004)
|
||||
.contains(id)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingLastPage_ThenReturnLastPageWithRemData() {
|
||||
Pageable pageRequest = PageRequest.of(2, 2);
|
||||
|
||||
Page<Product> result = productRepository.findAll(pageRequest);
|
||||
|
||||
assertThat(result.getContent(), hasSize(1));
|
||||
assertTrue(result.stream()
|
||||
.map(Product::getId)
|
||||
.allMatch(id -> Arrays.asList(1005)
|
||||
.contains(id)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() {
|
||||
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));
|
||||
|
||||
Page<Product> result = productRepository.findAll(pageRequest);
|
||||
|
||||
assertThat(result.getContent(), hasSize(3));
|
||||
assertThat(result.getContent()
|
||||
.stream()
|
||||
.map(Product::getId)
|
||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortingByPriceDescAndPaging_ThenReturnSortedPagedResult() {
|
||||
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price")
|
||||
.descending());
|
||||
|
||||
Page<Product> result = productRepository.findAll(pageRequest);
|
||||
|
||||
assertThat(result.getContent(), hasSize(3));
|
||||
assertThat(result.getContent()
|
||||
.stream()
|
||||
.map(Product::getId)
|
||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSortingByPriceDescAndNameAscAndPaging_ThenReturnSortedPagedResult() {
|
||||
Pageable pageRequest = PageRequest.of(0, 5, Sort.by("price")
|
||||
.descending()
|
||||
.and(Sort.by("name")));
|
||||
|
||||
Page<Product> result = productRepository.findAll(pageRequest);
|
||||
|
||||
assertThat(result.getContent(), hasSize(5));
|
||||
assertThat(result.getContent()
|
||||
.stream()
|
||||
.map(Product::getId)
|
||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002)));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() {
|
||||
Pageable pageRequest = PageRequest.of(0, 2);
|
||||
|
||||
List<Product> result = productRepository.findAllByPrice(10, pageRequest);
|
||||
|
||||
assertThat(result, hasSize(2));
|
||||
assertTrue(result.stream()
|
||||
.map(Product::getId)
|
||||
.allMatch(id -> Arrays.asList(1002, 1005)
|
||||
.contains(id)));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user