group persistence modules (#2890)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
dbeb5f8ba4
commit
26c50909be
+25
@@ -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);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.spring.data.solr.model;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.solr.core.mapping.Indexed;
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
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<Product, String> {
|
||||
|
||||
public List<Product> findByName(String name);
|
||||
|
||||
@Query("id:*?0* OR name:*?0*")
|
||||
public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
|
||||
|
||||
@Query(name = "Product.findByNamedQuery")
|
||||
public Page<Product> findByNamedQuery(String searchTerm, Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Product.findByNamedQuery=id:*?0* OR name:*?0*
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
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();
|
||||
product.setId("P000089998");
|
||||
product.setName("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");
|
||||
|
||||
productRepository.save(product);
|
||||
|
||||
product.setName("Shirt");
|
||||
productRepository.save(product);
|
||||
|
||||
final Product retrievedProduct = productRepository.findOne(product.getId());
|
||||
assertEquals(product.getName(), retrievedProduct.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
|
||||
final Product product = new Product();
|
||||
product.setId("P0001");
|
||||
product.setName("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");
|
||||
productRepository.save(phone);
|
||||
|
||||
List<Product> 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");
|
||||
productRepository.save(phone);
|
||||
|
||||
final Product phoneCover = new Product();
|
||||
phoneCover.setId("P0002");
|
||||
phoneCover.setName("Phone Cover");
|
||||
productRepository.save(phoneCover);
|
||||
|
||||
final Product wirelessCharger = new Product();
|
||||
wirelessCharger.setId("P0003");
|
||||
wirelessCharger.setName("Phone Charging Cable");
|
||||
productRepository.save(wirelessCharger);
|
||||
|
||||
Page<Product> 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");
|
||||
productRepository.save(phone);
|
||||
|
||||
final Product phoneCover = new Product();
|
||||
phoneCover.setId("P0002");
|
||||
phoneCover.setName("Phone Cover");
|
||||
productRepository.save(phoneCover);
|
||||
|
||||
final Product wirelessCharger = new Product();
|
||||
wirelessCharger.setId("P0003");
|
||||
wirelessCharger.setName("Phone Charging Cable");
|
||||
productRepository.save(wirelessCharger);
|
||||
|
||||
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
|
||||
assertEquals(3, result.getNumberOfElements());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user