formatting
This commit is contained in:
parent
26864e2897
commit
8ccfd201e6
|
@ -13,13 +13,13 @@ import org.springframework.data.solr.repository.config.EnableSolrRepositories;
|
||||||
@ComponentScan
|
@ComponentScan
|
||||||
public class SolrConfig {
|
public class SolrConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SolrClient solrClient() {
|
public SolrClient solrClient() {
|
||||||
return new HttpSolrClient("http://localhost:8983/solr");
|
return new HttpSolrClient("http://localhost:8983/solr");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
|
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
|
||||||
return new SolrTemplate(client);
|
return new SolrTemplate(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,105 +21,105 @@ 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();
|
final Product product = new Product();
|
||||||
product.setId("P000089998");
|
product.setId("P000089998");
|
||||||
product.setName("Desk");
|
product.setName("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();
|
final Product product = new Product();
|
||||||
product.setId("P0001");
|
product.setId("P0001");
|
||||||
product.setName("T-Shirt");
|
product.setName("T-Shirt");
|
||||||
|
|
||||||
productRepository.save(product);
|
productRepository.save(product);
|
||||||
|
|
||||||
product.setName("Shirt");
|
product.setName("Shirt");
|
||||||
productRepository.save(product);
|
productRepository.save(product);
|
||||||
|
|
||||||
final Product retrievedProduct = productRepository.findOne(product.getId());
|
final Product retrievedProduct = productRepository.findOne(product.getId());
|
||||||
assertEquals(product.getName(), retrievedProduct.getName());
|
assertEquals(product.getName(), retrievedProduct.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
|
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
|
||||||
final Product product = new Product();
|
final Product product = new Product();
|
||||||
product.setId("P0001");
|
product.setId("P0001");
|
||||||
product.setName("Desk");
|
product.setName("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 {
|
||||||
Product phone = new Product();
|
Product phone = new Product();
|
||||||
phone.setId("P0001");
|
phone.setId("P0001");
|
||||||
phone.setName("Phone");
|
phone.setName("Phone");
|
||||||
productRepository.save(phone);
|
productRepository.save(phone);
|
||||||
|
|
||||||
List<Product> retrievedProducts = productRepository.findByName("Phone");
|
List<Product> retrievedProducts = productRepository.findByName("Phone");
|
||||||
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
|
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
|
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
|
||||||
final Product phone = new Product();
|
final Product phone = new Product();
|
||||||
phone.setId("P0001");
|
phone.setId("P0001");
|
||||||
phone.setName("Smart Phone");
|
phone.setName("Smart Phone");
|
||||||
productRepository.save(phone);
|
productRepository.save(phone);
|
||||||
|
|
||||||
final Product phoneCover = new Product();
|
final Product phoneCover = new Product();
|
||||||
phoneCover.setId("P0002");
|
phoneCover.setId("P0002");
|
||||||
phoneCover.setName("Phone Cover");
|
phoneCover.setName("Phone Cover");
|
||||||
productRepository.save(phoneCover);
|
productRepository.save(phoneCover);
|
||||||
|
|
||||||
final Product wirelessCharger = new Product();
|
final Product wirelessCharger = new Product();
|
||||||
wirelessCharger.setId("P0003");
|
wirelessCharger.setId("P0003");
|
||||||
wirelessCharger.setName("Phone Charging Cable");
|
wirelessCharger.setName("Phone Charging Cable");
|
||||||
productRepository.save(wirelessCharger);
|
productRepository.save(wirelessCharger);
|
||||||
|
|
||||||
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
|
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
|
||||||
assertEquals(3, result.getNumberOfElements());
|
assertEquals(3, result.getNumberOfElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
|
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
|
||||||
final Product phone = new Product();
|
final Product phone = new Product();
|
||||||
phone.setId("P0001");
|
phone.setId("P0001");
|
||||||
phone.setName("Smart Phone");
|
phone.setName("Smart Phone");
|
||||||
productRepository.save(phone);
|
productRepository.save(phone);
|
||||||
|
|
||||||
final Product phoneCover = new Product();
|
final Product phoneCover = new Product();
|
||||||
phoneCover.setId("P0002");
|
phoneCover.setId("P0002");
|
||||||
phoneCover.setName("Phone Cover");
|
phoneCover.setName("Phone Cover");
|
||||||
productRepository.save(phoneCover);
|
productRepository.save(phoneCover);
|
||||||
|
|
||||||
final Product wirelessCharger = new Product();
|
final Product wirelessCharger = new Product();
|
||||||
wirelessCharger.setId("P0003");
|
wirelessCharger.setId("P0003");
|
||||||
wirelessCharger.setName("Phone Charging Cable");
|
wirelessCharger.setName("Phone Charging Cable");
|
||||||
productRepository.save(wirelessCharger);
|
productRepository.save(wirelessCharger);
|
||||||
|
|
||||||
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
|
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
|
||||||
assertEquals(3, result.getNumberOfElements());
|
assertEquals(3, result.getNumberOfElements());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue