|
| 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 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 22 | +@ContextConfiguration(classes = SolrConfig.class) |
| 23 | +public class ProductRepositoryIntegrationTest { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private ProductRepository productRepository; |
| 27 | + |
| 28 | + @Before |
| 29 | + public void clearSolrData() { |
| 30 | + productRepository.deleteAll(); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenSavingProduct_thenAvailableOnRetrieval() throws Exception { |
| 35 | + final Product product = new Product(); |
| 36 | + product.setId("P000089998"); |
| 37 | + product.setName("Desk"); |
| 38 | + product.setCategory("Furniture"); |
| 39 | + product.setDescription("New Desk"); |
| 40 | + productRepository.save(product); |
| 41 | + final Product retrievedProduct = productRepository.findOne(product.getId()); |
| 42 | + assertEquals(product.getId(), retrievedProduct.getId()); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception { |
| 47 | + final Product product = new Product(); |
| 48 | + product.setId("P0001"); |
| 49 | + product.setName("T-Shirt"); |
| 50 | + product.setCategory("Kitchen"); |
| 51 | + product.setDescription("New T-Shirt"); |
| 52 | + productRepository.save(product); |
| 53 | + |
| 54 | + product.setCategory("Clothes"); |
| 55 | + productRepository.save(product); |
| 56 | + |
| 57 | + final Product retrievedProduct = productRepository.findOne(product.getId()); |
| 58 | + assertEquals(product.getCategory(), retrievedProduct.getCategory()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception { |
| 63 | + final Product product = new Product(); |
| 64 | + product.setId("P0001"); |
| 65 | + product.setName("Desk"); |
| 66 | + product.setCategory("Furniture"); |
| 67 | + product.setDescription("New Desk"); |
| 68 | + productRepository.save(product); |
| 69 | + |
| 70 | + productRepository.delete(product); |
| 71 | + |
| 72 | + Product retrievedProduct = productRepository.findOne(product.getId()); |
| 73 | + assertNull(retrievedProduct); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void whenFindByName_thenAvailableOnRetrieval() throws Exception { |
| 79 | + Product phone = new Product(); |
| 80 | + phone.setId("P0001"); |
| 81 | + phone.setName("Phone"); |
| 82 | + phone.setCategory("Electronics"); |
| 83 | + phone.setDescription("New Phone"); |
| 84 | + productRepository.save(phone); |
| 85 | + |
| 86 | + List<Product> retrievedProducts = productRepository.findByName("Phone"); |
| 87 | + assertEquals(phone.getId(), retrievedProducts.get(0).getId()); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception { |
| 92 | + final Product phone = new Product(); |
| 93 | + phone.setId("P0001"); |
| 94 | + phone.setName("Smart Phone"); |
| 95 | + phone.setCategory("Electronics"); |
| 96 | + phone.setDescription("New Item"); |
| 97 | + productRepository.save(phone); |
| 98 | + |
| 99 | + final Product phoneCover = new Product(); |
| 100 | + phoneCover.setId("P0002"); |
| 101 | + phoneCover.setName("Cover"); |
| 102 | + phoneCover.setCategory("Phone"); |
| 103 | + phoneCover.setDescription("New Product"); |
| 104 | + productRepository.save(phoneCover); |
| 105 | + |
| 106 | + final Product wirelessCharger = new Product(); |
| 107 | + wirelessCharger.setId("P0003"); |
| 108 | + wirelessCharger.setName("Charging Cable"); |
| 109 | + wirelessCharger.setCategory("Cable"); |
| 110 | + wirelessCharger.setDescription("Wireless Charger for Phone"); |
| 111 | + productRepository.save(wirelessCharger); |
| 112 | + |
| 113 | + Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10)); |
| 114 | + assertEquals(3, result.getNumberOfElements()); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception { |
| 119 | + final Product phone = new Product(); |
| 120 | + phone.setId("P0001"); |
| 121 | + phone.setName("Smart Phone"); |
| 122 | + phone.setCategory("Electronics"); |
| 123 | + phone.setDescription("New Item"); |
| 124 | + productRepository.save(phone); |
| 125 | + |
| 126 | + final Product phoneCover = new Product(); |
| 127 | + phoneCover.setId("P0002"); |
| 128 | + phoneCover.setName("Cover"); |
| 129 | + phoneCover.setCategory("Phone"); |
| 130 | + phoneCover.setDescription("New Product"); |
| 131 | + productRepository.save(phoneCover); |
| 132 | + |
| 133 | + final Product wirelessCharger = new Product(); |
| 134 | + wirelessCharger.setId("P0003"); |
| 135 | + wirelessCharger.setName("Charging Cable"); |
| 136 | + wirelessCharger.setCategory("Cable"); |
| 137 | + wirelessCharger.setDescription("Wireless Charger for Phone"); |
| 138 | + productRepository.save(wirelessCharger); |
| 139 | + |
| 140 | + Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10)); |
| 141 | + assertEquals(3, result.getNumberOfElements()); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments