From c50338720f553c29fc5ceaae248474bb9e991969 Mon Sep 17 00:00:00 2001 From: mujah Date: Wed, 9 Nov 2016 18:28:01 +0800 Subject: [PATCH] test cases and model fixes --- .../spring/data/solr/model/Product.java | 22 +++-- .../ProductRepositoryIntegrationTest.java | 92 ++++++++++++------- 2 files changed, 73 insertions(+), 41 deletions(-) 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 d08640123a..54962b7e51 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 @@ -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; } 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 b55e65e04a..74d94ef91c 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,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 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 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 result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); assertEquals(3, result.getNumberOfElements()); } - + }