diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml index bd48a53d06..ec6eb7bf46 100644 --- a/spring-data-solr/pom.xml +++ b/spring-data-solr/pom.xml @@ -1,21 +1,21 @@ - + 4.0.0 com.baeldung spring-data-solr 0.0.1-SNAPSHOT jar spring-data-solr - - + + UTF-8 4.2.5.RELEASE 2.19.1 2.0.4.RELEASE - + org.springframework @@ -50,20 +50,58 @@ test - + + + maven-compiler-plugin + org.apache.maven.plugins maven-surefire-plugin ${maven-surefire-plugin.version} - - **/*IntegrationTest.java - + + **/*IntegrationTest.java + **/*LiveTest.java + - - + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + \ No newline at end of file diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java index 7cd0890718..5286f53309 100644 --- a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java @@ -6,50 +6,28 @@ import org.springframework.data.solr.core.mapping.SolrDocument; @SolrDocument(solrCoreName = "product") public class Product { - + @Id @Indexed(name = "id", type = "string") private String id; - + @Indexed(name = "name", type = "string") private String name; - - @Indexed(name = "category", type = "string") - private String category; - - @Indexed(name = "description", type = "string") - private String description; - + public String getId() { return id; } - + public void setId(String id) { this.id = id; } - + public String getName() { return name; } - + public void setName(String name) { this.name = name; } - - public String getCategory() { - return category; - } - - public void setCategory(String category) { - this.category = category; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - + } diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java index 01ec1fb909..5649cd7888 100644 --- a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java @@ -13,7 +13,7 @@ public interface ProductRepository extends SolrCrudRepository { public List findByName(String name); - @Query("name:*?0* OR category:*?0* OR description:*?0*") + @Query("id:*?0* OR name:*?0*") public Page findByCustomQuery(String searchTerm, Pageable pageable); @Query(name = "Product.findByNamedQuery") diff --git a/spring-data-solr/src/main/resources/solr-named-queries.properties b/spring-data-solr/src/main/resources/solr-named-queries.properties index cec59cbebd..c00b5bace9 100644 --- a/spring-data-solr/src/main/resources/solr-named-queries.properties +++ b/spring-data-solr/src/main/resources/solr-named-queries.properties @@ -1 +1 @@ -Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?0* \ No newline at end of file +Product.findByNamedQuery=id:*?0* OR name:*?0* \ No newline at end of file diff --git a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java index 74d94ef91c..a3765a74ec 100644 --- a/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java +++ b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java @@ -21,124 +21,105 @@ 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() { productRepository.deleteAll(); } - + @Test public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception { final Product product = new Product(); product.setId("P000089998"); product.setName("Desk"); - product.setCategory("Furniture"); - product.setDescription("New Desk"); productRepository.save(product); final Product retrievedProduct = productRepository.findOne(product.getId()); assertEquals(product.getId(), retrievedProduct.getId()); } - + @Test public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { final Product product = new Product(); product.setId("P0001"); product.setName("T-Shirt"); - product.setCategory("Kitchen"); - product.setDescription("New T-Shirt"); + productRepository.save(product); - - product.setCategory("Clothes"); + + product.setName("Shirt"); productRepository.save(product); - + final Product retrievedProduct = productRepository.findOne(product.getId()); - assertEquals(product.getCategory(), retrievedProduct.getCategory()); + assertEquals(product.getName(), retrievedProduct.getName()); } - + @Test public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { final Product product = new Product(); product.setId("P0001"); product.setName("Desk"); - product.setCategory("Furniture"); - product.setDescription("New Desk"); productRepository.save(product); - + productRepository.delete(product); - + Product retrievedProduct = productRepository.findOne(product.getId()); assertNull(retrievedProduct); - + } - + @Test public void whenFindByName_thenAvailableOnRetrieval() throws Exception { Product phone = new Product(); phone.setId("P0001"); phone.setName("Phone"); - phone.setCategory("Electronics"); - phone.setDescription("New Phone"); productRepository.save(phone); - + List retrievedProducts = productRepository.findByName("Phone"); assertEquals(phone.getId(), retrievedProducts.get(0).getId()); } - + @Test public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception { final Product phone = new Product(); phone.setId("P0001"); phone.setName("Smart Phone"); - phone.setCategory("Electronics"); - phone.setDescription("New Item"); productRepository.save(phone); - + final Product phoneCover = new Product(); phoneCover.setId("P0002"); - phoneCover.setName("Cover"); - phoneCover.setCategory("Phone"); - phoneCover.setDescription("New Product"); + phoneCover.setName("Phone Cover"); productRepository.save(phoneCover); - + final Product wirelessCharger = new Product(); wirelessCharger.setId("P0003"); - wirelessCharger.setName("Charging Cable"); - wirelessCharger.setCategory("Cable"); - wirelessCharger.setDescription("Wireless Charger for Phone"); + wirelessCharger.setName("Phone Charging Cable"); productRepository.save(wirelessCharger); - + Page result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10)); assertEquals(3, result.getNumberOfElements()); } - + @Test public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception { final Product phone = new Product(); phone.setId("P0001"); phone.setName("Smart Phone"); - phone.setCategory("Electronics"); - phone.setDescription("New Item"); productRepository.save(phone); - + final Product phoneCover = new Product(); phoneCover.setId("P0002"); - phoneCover.setName("Cover"); - phoneCover.setCategory("Phone"); - phoneCover.setDescription("New Product"); + phoneCover.setName("Phone Cover"); productRepository.save(phoneCover); - + final Product wirelessCharger = new Product(); wirelessCharger.setId("P0003"); - wirelessCharger.setName("Charging Cable"); - wirelessCharger.setCategory("Cable"); - wirelessCharger.setDescription("Wireless Charger for Phone"); + wirelessCharger.setName("Phone Charging Cable"); productRepository.save(wirelessCharger); - + Page result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); assertEquals(3, result.getNumberOfElements()); } - + }