formatting format changes

This commit is contained in:
mujah 2016-10-30 17:12:50 +08:00
parent d9eeb7c21a
commit b3976c7fb5
3 changed files with 32 additions and 38 deletions

View File

@ -4,7 +4,7 @@ import org.apache.solr.client.solrj.beans.Field;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.solr.core.mapping.SolrDocument; import org.springframework.data.solr.core.mapping.SolrDocument;
@SolrDocument(solrCoreName="product") @SolrDocument(solrCoreName = "product")
public class Product { public class Product {
@Id @Id
@ -20,13 +20,12 @@ public class Product {
@Field("description") @Field("description")
private String description; private String description;
public Product(String id, String name, String category) {
public Product(String id,String name,String category){
this.id = id; this.id = id;
this.name = name; this.name = name;
this.category = category; this.category = category;
} }
public String getId() { public String getId() {
return id; return id;
} }

View File

@ -1,6 +1,5 @@
package com.baeldung.spring.data.solr.repository; package com.baeldung.spring.data.solr.repository;
import java.util.List; import java.util.List;
import org.springframework.data.domain.Page; 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; 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); public List<Product> findByName(String name);
@Query("name:*?0* OR category:*?0* OR description:*?0*") @Query("name:*?0* OR category:*?0* OR description:*?0*")
public Page<Product> findByCustomQuery(String searchTerm,Pageable pageable); public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
@Query(name = "Product.findByNamedQuery")
// @Query(name="Product.findByNamedQuery") public Page<Product> findByNamedQuery(String searchTerm, Pageable pageable);
public Page<Product> findByNamedQuery(String searchTerm,Pageable pageable);
} }

View File

@ -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.model.Product;
import com.baeldung.spring.data.solr.repository.ProductRepository; import com.baeldung.spring.data.solr.repository.ProductRepository;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SolrConfig.class) @ContextConfiguration(classes = SolrConfig.class)
public class ProductRepositoryIntegrationTest { public class ProductRepositoryIntegrationTest {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
@Before @Before
public void clearSolrData(){ public void clearSolrData() {
productRepository.deleteAll(); productRepository.deleteAll();
} }
@Test @Test
public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{ public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception {
final Product product = new Product("P00001","Desk","Furniture"); final Product product = new Product("P00001", "Desk", "Furniture");
product.setDescription("New Desk"); product.setDescription("New Desk");
productRepository.save(product); productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId()); final Product retrievedProduct = productRepository.findOne(product.getId());
assertEquals(product.getId(),retrievedProduct.getId()); assertEquals(product.getId(), retrievedProduct.getId());
} }
@Test @Test
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { 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"); product.setDescription("New T-Shirt");
productRepository.save(product); productRepository.save(product);
product.setCategory("Clothes"); product.setCategory("Clothes");
productRepository.save(product); productRepository.save(product);
final Product retrievedProduct = productRepository.findOne(product.getId()); final Product retrievedProduct = productRepository.findOne(product.getId());
assertEquals(product.getCategory(), retrievedProduct.getCategory()); assertEquals(product.getCategory(), retrievedProduct.getCategory());
} }
@Test @Test
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { 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"); product.setDescription("New Desk");
productRepository.save(product); productRepository.save(product);
productRepository.delete(product); productRepository.delete(product);
Product retrievedProduct = productRepository.findOne(product.getId()); Product retrievedProduct = productRepository.findOne(product.getId());
assertNull(retrievedProduct); assertNull(retrievedProduct);
} }
@Test @Test
public void whenFindByName_thenAvailableOnRetrieval() throws Exception{ public void whenFindByName_thenAvailableOnRetrieval() throws Exception {
final Product phone = new Product("P0001", "Phone", "Electronics"); Product phone = new Product("P0001", "Phone", "Electronics");
phone.setDescription("New Phone"); phone.setDescription("New Phone");
productRepository.save(phone); productRepository.save(phone);
@ -92,7 +89,7 @@ public class ProductRepositoryIntegrationTest {
wirelessCharger.setDescription("Wireless Charger for Phone"); wirelessCharger.setDescription("Wireless Charger for Phone");
productRepository.save(wirelessCharger); 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()); assertEquals(3, result.getNumberOfElements());
} }