From 771a81d5bd3a409c00279e8acddcdf9535fde3a9 Mon Sep 17 00:00:00 2001 From: mujah Date: Sat, 29 Oct 2016 21:46:34 +0800 Subject: [PATCH] Spring Data Solr project created --- spring-data-solr/pom.xml | 78 ++++++++++++ .../spring/data/solr/config/SolrConfig.java | 25 ++++ .../spring/data/solr/model/Product.java | 62 ++++++++++ .../solr/repository/ProductRepository.java | 24 ++++ .../resources/solr-named-queries.properties | 1 + .../ProductRepositoryIntegrationTest.java | 117 ++++++++++++++++++ 6 files changed, 307 insertions(+) create mode 100644 spring-data-solr/pom.xml create mode 100644 spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java create mode 100644 spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java create mode 100644 spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java create mode 100644 spring-data-solr/src/main/resources/solr-named-queries.properties create mode 100644 spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml new file mode 100644 index 0000000000..9ec017e376 --- /dev/null +++ b/spring-data-solr/pom.xml @@ -0,0 +1,78 @@ + + 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 + spring-core + ${spring.version} + + + + org.springframework.data + spring-data-solr + ${spring-data-solr} + + + + + org.springframework + spring-context + ${spring.version} + + + + + log4j + log4j + 1.2.16 + + + + + junit + junit + 4.12 + test + + + + org.springframework + spring-test + ${spring.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + + diff --git a/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java new file mode 100644 index 0000000000..1fe1e5468b --- /dev/null +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/config/SolrConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.spring.data.solr.config; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.solr.core.SolrTemplate; +import org.springframework.data.solr.repository.config.EnableSolrRepositories; + +@Configuration +@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties", multicoreSupport = true) +@ComponentScan +public class SolrConfig { + + @Bean + public SolrClient solrClient() { + return new HttpSolrClient("http://localhost:8983/solr"); + } + + @Bean + public SolrTemplate solrTemplate(SolrClient client) throws Exception { + return new SolrTemplate(client); + } +} 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 new file mode 100644 index 0000000000..2c16a2cdbf --- /dev/null +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java @@ -0,0 +1,62 @@ +package com.baeldung.spring.data.solr.model; + +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") +public class Product { + + @Id + @Field("id") + private String id; + + @Field("name") + private String name; + + @Field("category") + private String category; + + @Field("description") + private String description; + + + public Product(String id,String name,String category){ + this.id = id; + this.name = name; + this.category = category; + } + + 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 new file mode 100644 index 0000000000..1f57816dd6 --- /dev/null +++ b/spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java @@ -0,0 +1,24 @@ +package com.baeldung.spring.data.solr.repository; + + +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.solr.repository.Query; +import org.springframework.data.solr.repository.SolrCrudRepository; + +import com.baeldung.spring.data.solr.model.Product; + +public interface ProductRepository extends SolrCrudRepository{ + + public List findByName(String name); + + @Query("name:*?0* OR category:*?0* OR description:*?0*") + public Page findByCustomQuery(String searchTerm,Pageable pageable); + + + // @Query(name="Product.findByNamedQuery") + public Page findByNamedQuery(String searchTerm,Pageable pageable); + +} diff --git a/spring-data-solr/src/main/resources/solr-named-queries.properties b/spring-data-solr/src/main/resources/solr-named-queries.properties new file mode 100644 index 0000000000..cec59cbebd --- /dev/null +++ b/spring-data-solr/src/main/resources/solr-named-queries.properties @@ -0,0 +1 @@ +Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?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 new file mode 100644 index 0000000000..52a1a2837d --- /dev/null +++ b/spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java @@ -0,0 +1,117 @@ +package com.baeldung.spring.data.solr.repo; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +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(){ + productRepository.deleteAll(); + } + + @Test + 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()); + } + + @Test + public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { + 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"); + 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{ + final Product phone = new Product("P0001", "Phone", "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("P0001", "Smart Phone", "Electronics"); + phone.setDescription("New Item"); + productRepository.save(phone); + + final Product phoneCover = new Product("P0002", "Cover", "Phone"); + phoneCover.setDescription("New Product"); + productRepository.save(phoneCover); + + final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); + wirelessCharger.setDescription("Wireless Charger for Phone"); + productRepository.save(wirelessCharger); + + Page result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10)); + assertEquals(3, result.getNumberOfElements()); + } + + @Test + public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception { + final Product phone = new Product("P0001", "Smart Phone", "Electronics"); + phone.setDescription("New Item"); + productRepository.save(phone); + + final Product phoneCover = new Product("P0002", "Cover", "Phone"); + phoneCover.setDescription("New Product"); + productRepository.save(phoneCover); + + final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); + wirelessCharger.setDescription("Wireless Charger for Phone"); + productRepository.save(wirelessCharger); + + Page result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); + assertEquals(3, result.getNumberOfElements()); + } + +}