formatting format changes
This commit is contained in:
parent
d9eeb7c21a
commit
b3976c7fb5
|
@ -4,7 +4,7 @@ import org.apache.solr.client.solrj.beans.Field;
|
|||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.solr.core.mapping.SolrDocument;
|
||||
|
||||
@SolrDocument(solrCoreName="product")
|
||||
@SolrDocument(solrCoreName = "product")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
|
@ -20,13 +20,12 @@ public class Product {
|
|||
@Field("description")
|
||||
private String description;
|
||||
|
||||
|
||||
public Product(String id,String name,String category){
|
||||
public Product(String id, String name, String category) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.baeldung.spring.data.solr.repository;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
@ -10,15 +9,14 @@ import org.springframework.data.solr.repository.SolrCrudRepository;
|
|||
|
||||
import com.baeldung.spring.data.solr.model.Product;
|
||||
|
||||
public interface ProductRepository extends SolrCrudRepository<Product, String>{
|
||||
|
||||
public interface ProductRepository extends SolrCrudRepository<Product, String> {
|
||||
|
||||
public List<Product> findByName(String name);
|
||||
|
||||
|
||||
@Query("name:*?0* OR category:*?0* OR description:*?0*")
|
||||
public Page<Product> findByCustomQuery(String searchTerm,Pageable pageable);
|
||||
|
||||
|
||||
// @Query(name="Product.findByNamedQuery")
|
||||
public Page<Product> findByNamedQuery(String searchTerm,Pageable pageable);
|
||||
|
||||
public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
|
||||
|
||||
@Query(name = "Product.findByNamedQuery")
|
||||
public Page<Product> findByNamedQuery(String searchTerm, Pageable pageable);
|
||||
|
||||
}
|
||||
|
|
|
@ -18,59 +18,56 @@ import com.baeldung.spring.data.solr.config.SolrConfig;
|
|||
import com.baeldung.spring.data.solr.model.Product;
|
||||
import com.baeldung.spring.data.solr.repository.ProductRepository;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = SolrConfig.class)
|
||||
public class ProductRepositoryIntegrationTest {
|
||||
|
||||
|
||||
@Autowired
|
||||
private ProductRepository productRepository;
|
||||
|
||||
|
||||
@Before
|
||||
public void clearSolrData(){
|
||||
public void clearSolrData() {
|
||||
productRepository.deleteAll();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{
|
||||
final Product product = new Product("P00001","Desk","Furniture");
|
||||
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception {
|
||||
final Product product = new Product("P00001", "Desk", "Furniture");
|
||||
product.setDescription("New Desk");
|
||||
productRepository.save(product);
|
||||
final Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertEquals(product.getId(),retrievedProduct.getId());
|
||||
assertEquals(product.getId(), retrievedProduct.getId());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception {
|
||||
final Product product = new Product("P0001", "T-Shirt","Kitchen");
|
||||
final Product product = new Product("P0001", "T-Shirt", "Kitchen");
|
||||
product.setDescription("New T-Shirt");
|
||||
productRepository.save(product);
|
||||
|
||||
|
||||
product.setCategory("Clothes");
|
||||
productRepository.save(product);
|
||||
|
||||
|
||||
final Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertEquals(product.getCategory(), retrievedProduct.getCategory());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
|
||||
final Product product = new Product("P0001", "Desk","Furniture");
|
||||
final Product product = new Product("P0001", "Desk", "Furniture");
|
||||
product.setDescription("New Desk");
|
||||
productRepository.save(product);
|
||||
|
||||
|
||||
productRepository.delete(product);
|
||||
|
||||
Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
|
||||
Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertNull(retrievedProduct);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenFindByName_thenAvailableOnRetrieval() throws Exception{
|
||||
final Product phone = new Product("P0001", "Phone", "Electronics");
|
||||
public void whenFindByName_thenAvailableOnRetrieval() throws Exception {
|
||||
Product phone = new Product("P0001", "Phone", "Electronics");
|
||||
phone.setDescription("New Phone");
|
||||
productRepository.save(phone);
|
||||
|
||||
|
@ -92,7 +89,7 @@ public class ProductRepositoryIntegrationTest {
|
|||
wirelessCharger.setDescription("Wireless Charger for Phone");
|
||||
productRepository.save(wirelessCharger);
|
||||
|
||||
Page<Product> result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10));
|
||||
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
|
||||
assertEquals(3, result.getNumberOfElements());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue