Skip to content

Commit c503387

Browse files
committed
test cases and model fixes
1 parent b3976c7 commit c503387

2 files changed

Lines changed: 73 additions & 41 deletions

File tree

‎spring-data-solr/src/main/java/com/baeldung/spring/data/solr/model/Product.java‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
package com.baeldung.spring.data.solr.model;
22

3-
import org.apache.solr.client.solrj.beans.Field;
43
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.solr.core.mapping.Indexed;
55
import org.springframework.data.solr.core.mapping.SolrDocument;
66

77
@SolrDocument(solrCoreName = "product")
88
public class Product {
99

1010
@Id
11-
@Field("id")
11+
@Indexed(name="id",type = "string")
1212
private String id;
1313

14-
@Field("name")
14+
15+
@Indexed(name="name",type = "string")
1516
private String name;
1617

17-
@Field("category")
18+
19+
@Indexed(name="category",type = "string")
1820
private String category;
1921

20-
@Field("description")
22+
23+
@Indexed(name="description",type = "string")
2124
private String description;
22-
23-
public Product(String id, String name, String category) {
24-
this.id = id;
25-
this.name = name;
26-
this.category = category;
25+
26+
public Product(){
27+
2728
}
2829

30+
2931
public String getId() {
3032
return id;
3133
}

‎spring-data-solr/src/test/java/com/baeldung/spring/data/solr/repo/ProductRepositoryIntegrationTest.java‎

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,94 +21,124 @@
2121
@RunWith(SpringJUnit4ClassRunner.class)
2222
@ContextConfiguration(classes = SolrConfig.class)
2323
public class ProductRepositoryIntegrationTest {
24-
24+
2525
@Autowired
2626
private ProductRepository productRepository;
27-
27+
2828
@Before
2929
public void clearSolrData() {
3030
productRepository.deleteAll();
3131
}
32-
32+
3333
@Test
3434
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");
3639
product.setDescription("New Desk");
3740
productRepository.save(product);
3841
final Product retrievedProduct = productRepository.findOne(product.getId());
3942
assertEquals(product.getId(), retrievedProduct.getId());
4043
}
41-
44+
4245
@Test
4346
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");
4551
product.setDescription("New T-Shirt");
4652
productRepository.save(product);
47-
53+
4854
product.setCategory("Clothes");
4955
productRepository.save(product);
50-
56+
5157
final Product retrievedProduct = productRepository.findOne(product.getId());
5258
assertEquals(product.getCategory(), retrievedProduct.getCategory());
5359
}
54-
60+
5561
@Test
5662
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");
5867
product.setDescription("New Desk");
5968
productRepository.save(product);
60-
69+
6170
productRepository.delete(product);
62-
71+
6372
Product retrievedProduct = productRepository.findOne(product.getId());
6473
assertNull(retrievedProduct);
65-
74+
6675
}
67-
76+
6877
@Test
6978
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");
7183
phone.setDescription("New Phone");
7284
productRepository.save(phone);
73-
85+
7486
List<Product> retrievedProducts = productRepository.findByName("Phone");
7587
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
7688
}
77-
89+
7890
@Test
7991
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");
8196
phone.setDescription("New Item");
8297
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");
85103
phoneCover.setDescription("New Product");
86104
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");
89110
wirelessCharger.setDescription("Wireless Charger for Phone");
90111
productRepository.save(wirelessCharger);
91-
112+
92113
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
93114
assertEquals(3, result.getNumberOfElements());
94115
}
95-
116+
96117
@Test
97118
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");
99123
phone.setDescription("New Item");
100124
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");
103130
phoneCover.setDescription("New Product");
104131
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");
107137
wirelessCharger.setDescription("Wireless Charger for Phone");
108138
productRepository.save(wirelessCharger);
109-
139+
110140
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
111141
assertEquals(3, result.getNumberOfElements());
112142
}
113-
143+
114144
}

0 commit comments

Comments
 (0)