test cases and model fixes
This commit is contained in:
parent
b3976c7fb5
commit
c50338720f
|
@ -1,31 +1,33 @@
|
|||
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.Indexed;
|
||||
import org.springframework.data.solr.core.mapping.SolrDocument;
|
||||
|
||||
@SolrDocument(solrCoreName = "product")
|
||||
public class Product {
|
||||
|
||||
@Id
|
||||
@Field("id")
|
||||
@Indexed(name="id",type = "string")
|
||||
private String id;
|
||||
|
||||
@Field("name")
|
||||
|
||||
@Indexed(name="name",type = "string")
|
||||
private String name;
|
||||
|
||||
@Field("category")
|
||||
|
||||
@Indexed(name="category",type = "string")
|
||||
private String category;
|
||||
|
||||
@Field("description")
|
||||
|
||||
@Indexed(name="description",type = "string")
|
||||
private String description;
|
||||
|
||||
public Product(String id, String name, String category) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.category = category;
|
||||
|
||||
public Product(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
|
|
@ -21,94 +21,124 @@ 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");
|
||||
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("P0001", "T-Shirt", "Kitchen");
|
||||
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");
|
||||
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");
|
||||
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("P0001", "Phone", "Electronics");
|
||||
Product phone = new Product();
|
||||
phone.setId("P0001");
|
||||
phone.setName("Phone");
|
||||
phone.setCategory("Electronics");
|
||||
phone.setDescription("New 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("P0001", "Smart Phone", "Electronics");
|
||||
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("P0002", "Cover", "Phone");
|
||||
|
||||
final Product phoneCover = new Product();
|
||||
phoneCover.setId("P0002");
|
||||
phoneCover.setName("Cover");
|
||||
phoneCover.setCategory("Phone");
|
||||
phoneCover.setDescription("New Product");
|
||||
productRepository.save(phoneCover);
|
||||
|
||||
final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable");
|
||||
|
||||
final Product wirelessCharger = new Product();
|
||||
wirelessCharger.setId("P0003");
|
||||
wirelessCharger.setName("Charging Cable");
|
||||
wirelessCharger.setCategory("Cable");
|
||||
wirelessCharger.setDescription("Wireless Charger for Phone");
|
||||
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("P0001", "Smart Phone", "Electronics");
|
||||
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("P0002", "Cover", "Phone");
|
||||
|
||||
final Product phoneCover = new Product();
|
||||
phoneCover.setId("P0002");
|
||||
phoneCover.setName("Cover");
|
||||
phoneCover.setCategory("Phone");
|
||||
phoneCover.setDescription("New Product");
|
||||
productRepository.save(phoneCover);
|
||||
|
||||
final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable");
|
||||
|
||||
final Product wirelessCharger = new Product();
|
||||
wirelessCharger.setId("P0003");
|
||||
wirelessCharger.setName("Charging Cable");
|
||||
wirelessCharger.setCategory("Cable");
|
||||
wirelessCharger.setDescription("Wireless Charger for Phone");
|
||||
productRepository.save(wirelessCharger);
|
||||
|
||||
|
||||
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
|
||||
assertEquals(3, result.getNumberOfElements());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue