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