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