|
| 1 | +package com.baeldung.spring.data.solr.repo; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNull; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.Before; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.data.domain.Page; |
| 13 | +import org.springframework.data.domain.PageRequest; |
| 14 | +import org.springframework.test.context.ContextConfiguration; |
| 15 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 16 | + |
| 17 | +import com.baeldung.spring.data.solr.config.SolrConfig; |
| 18 | +import com.baeldung.spring.data.solr.model.Product; |
| 19 | +import com.baeldung.spring.data.solr.repository.ProductRepository; |
| 20 | + |
| 21 | + |
| 22 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 23 | +@ContextConfiguration(classes = SolrConfig.class) |
| 24 | +public class ProductRepositoryIntegrationTest { |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private ProductRepository productRepository; |
| 28 | + |
| 29 | + @Before |
| 30 | + public void clearSolrData(){ |
| 31 | + productRepository.deleteAll(); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception{ |
| 36 | + final Product product = new Product("P00001","Desk","Furniture"); |
| 37 | + product.setDescription("New Desk"); |
| 38 | + productRepository.save(product); |
| 39 | + final Product retrievedProduct = productRepository.findOne(product.getId()); |
| 40 | + assertEquals(product.getId(),retrievedProduct.getId()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { |
| 45 | + final Product product = new Product("P0001", "T-Shirt","Kitchen"); |
| 46 | + product.setDescription("New T-Shirt"); |
| 47 | + productRepository.save(product); |
| 48 | + |
| 49 | + product.setCategory("Clothes"); |
| 50 | + productRepository.save(product); |
| 51 | + |
| 52 | + final Product retrievedProduct = productRepository.findOne(product.getId()); |
| 53 | + assertEquals(product.getCategory(), retrievedProduct.getCategory()); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { |
| 60 | + final Product product = new Product("P0001", "Desk","Furniture"); |
| 61 | + product.setDescription("New Desk"); |
| 62 | + productRepository.save(product); |
| 63 | + |
| 64 | + productRepository.delete(product); |
| 65 | + |
| 66 | + Product retrievedProduct = productRepository.findOne(product.getId()); |
| 67 | + assertNull(retrievedProduct); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void whenFindByName_thenAvailableOnRetrieval() throws Exception{ |
| 73 | + final Product phone = new Product("P0001", "Phone", "Electronics"); |
| 74 | + phone.setDescription("New Phone"); |
| 75 | + productRepository.save(phone); |
| 76 | + |
| 77 | + List<Product> retrievedProducts = productRepository.findByName("Phone"); |
| 78 | + assertEquals(phone.getId(), retrievedProducts.get(0).getId()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception { |
| 83 | + final Product phone = new Product("P0001", "Smart Phone", "Electronics"); |
| 84 | + phone.setDescription("New Item"); |
| 85 | + productRepository.save(phone); |
| 86 | + |
| 87 | + final Product phoneCover = new Product("P0002", "Cover", "Phone"); |
| 88 | + phoneCover.setDescription("New Product"); |
| 89 | + productRepository.save(phoneCover); |
| 90 | + |
| 91 | + final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); |
| 92 | + wirelessCharger.setDescription("Wireless Charger for Phone"); |
| 93 | + productRepository.save(wirelessCharger); |
| 94 | + |
| 95 | + Page<Product> result = productRepository.findByCustomQuery("Pho", new PageRequest(0, 10)); |
| 96 | + assertEquals(3, result.getNumberOfElements()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception { |
| 101 | + final Product phone = new Product("P0001", "Smart Phone", "Electronics"); |
| 102 | + phone.setDescription("New Item"); |
| 103 | + productRepository.save(phone); |
| 104 | + |
| 105 | + final Product phoneCover = new Product("P0002", "Cover", "Phone"); |
| 106 | + phoneCover.setDescription("New Product"); |
| 107 | + productRepository.save(phoneCover); |
| 108 | + |
| 109 | + final Product wirelessCharger = new Product("P0003", "Charging Cable", "Cable"); |
| 110 | + wirelessCharger.setDescription("Wireless Charger for Phone"); |
| 111 | + productRepository.save(wirelessCharger); |
| 112 | + |
| 113 | + Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); |
| 114 | + assertEquals(3, result.getNumberOfElements()); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments