Skip to content

Commit e2c7609

Browse files
author
Eugen
authored
Merge pull request eugenp#822 from mujahid/master
BAEL-346 - [PR -2]
2 parents b14bae2 + 082ae7b commit e2c7609

5 files changed

Lines changed: 85 additions & 88 deletions

File tree

‎spring-data-solr/pom.xml‎

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3-
3+
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.baeldung</groupId>
66
<artifactId>spring-data-solr</artifactId>
77
<version>0.0.1-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<name>spring-data-solr</name>
10-
11-
10+
11+
1212
<properties>
1313
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1414
<spring.version>4.2.5.RELEASE</spring.version>
1515
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
1616
<spring-data-solr>2.0.4.RELEASE</spring-data-solr>
1717
</properties>
18-
18+
1919
<dependencies>
2020
<dependency>
2121
<groupId>org.springframework</groupId>
@@ -50,20 +50,58 @@
5050
<scope>test</scope>
5151
</dependency>
5252
</dependencies>
53-
53+
5454
<build>
5555
<plugins>
56+
<plugin>
57+
<artifactId>maven-compiler-plugin</artifactId>
58+
</plugin>
5659
<plugin>
5760
<groupId>org.apache.maven.plugins</groupId>
5861
<artifactId>maven-surefire-plugin</artifactId>
5962
<version>${maven-surefire-plugin.version}</version>
6063
<configuration>
61-
<includes>
62-
<include>**/*IntegrationTest.java</include>
63-
</includes>
64+
<excludes>
65+
<exclude>**/*IntegrationTest.java</exclude>
66+
<exclude>**/*LiveTest.java</exclude>
67+
</excludes>
6468
</configuration>
6569
</plugin>
6670
</plugins>
6771
</build>
68-
69-
</project>
72+
73+
<profiles>
74+
<profile>
75+
<id>integration</id>
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-surefire-plugin</artifactId>
81+
<executions>
82+
<execution>
83+
<phase>integration-test</phase>
84+
<goals>
85+
<goal>test</goal>
86+
</goals>
87+
<configuration>
88+
<excludes>
89+
<exclude>**/*LiveTest.java</exclude>
90+
</excludes>
91+
<includes>
92+
<include>**/*IntegrationTest.java</include>
93+
</includes>
94+
</configuration>
95+
</execution>
96+
</executions>
97+
<configuration>
98+
<systemPropertyVariables>
99+
<test.mime>json</test.mime>
100+
</systemPropertyVariables>
101+
</configuration>
102+
</plugin>
103+
</plugins>
104+
</build>
105+
</profile>
106+
</profiles>
107+
</project>

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

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,28 @@
66

77
@SolrDocument(solrCoreName = "product")
88
public class Product {
9-
9+
1010
@Id
1111
@Indexed(name = "id", type = "string")
1212
private String id;
13-
13+
1414
@Indexed(name = "name", type = "string")
1515
private String name;
16-
17-
@Indexed(name = "category", type = "string")
18-
private String category;
19-
20-
@Indexed(name = "description", type = "string")
21-
private String description;
22-
16+
2317
public String getId() {
2418
return id;
2519
}
26-
20+
2721
public void setId(String id) {
2822
this.id = id;
2923
}
30-
24+
3125
public String getName() {
3226
return name;
3327
}
34-
28+
3529
public void setName(String name) {
3630
this.name = name;
3731
}
38-
39-
public String getCategory() {
40-
return category;
41-
}
42-
43-
public void setCategory(String category) {
44-
this.category = category;
45-
}
46-
47-
public String getDescription() {
48-
return description;
49-
}
50-
51-
public void setDescription(String description) {
52-
this.description = description;
53-
}
54-
32+
5533
}

‎spring-data-solr/src/main/java/com/baeldung/spring/data/solr/repository/ProductRepository.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public interface ProductRepository extends SolrCrudRepository<Product, String> {
1313

1414
public List<Product> findByName(String name);
1515

16-
@Query("name:*?0* OR category:*?0* OR description:*?0*")
16+
@Query("id:*?0* OR name:*?0*")
1717
public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
1818

1919
@Query(name = "Product.findByNamedQuery")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?0*
1+
Product.findByNamedQuery=id:*?0* OR name:*?0*

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

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,124 +21,105 @@
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 {
3535
final Product product = new Product();
3636
product.setId("P000089998");
3737
product.setName("Desk");
38-
product.setCategory("Furniture");
39-
product.setDescription("New Desk");
4038
productRepository.save(product);
4139
final Product retrievedProduct = productRepository.findOne(product.getId());
4240
assertEquals(product.getId(), retrievedProduct.getId());
4341
}
44-
42+
4543
@Test
4644
public void whenUpdatingProduct_thenChangeAvailableOnRetrieval() throws Exception {
4745
final Product product = new Product();
4846
product.setId("P0001");
4947
product.setName("T-Shirt");
50-
product.setCategory("Kitchen");
51-
product.setDescription("New T-Shirt");
48+
5249
productRepository.save(product);
53-
54-
product.setCategory("Clothes");
50+
51+
product.setName("Shirt");
5552
productRepository.save(product);
56-
53+
5754
final Product retrievedProduct = productRepository.findOne(product.getId());
58-
assertEquals(product.getCategory(), retrievedProduct.getCategory());
55+
assertEquals(product.getName(), retrievedProduct.getName());
5956
}
60-
57+
6158
@Test
6259
public void whenDeletingProduct_thenNotAvailableOnRetrieval() throws Exception {
6360
final Product product = new Product();
6461
product.setId("P0001");
6562
product.setName("Desk");
66-
product.setCategory("Furniture");
67-
product.setDescription("New Desk");
6863
productRepository.save(product);
69-
64+
7065
productRepository.delete(product);
71-
66+
7267
Product retrievedProduct = productRepository.findOne(product.getId());
7368
assertNull(retrievedProduct);
74-
69+
7570
}
76-
71+
7772
@Test
7873
public void whenFindByName_thenAvailableOnRetrieval() throws Exception {
7974
Product phone = new Product();
8075
phone.setId("P0001");
8176
phone.setName("Phone");
82-
phone.setCategory("Electronics");
83-
phone.setDescription("New Phone");
8477
productRepository.save(phone);
85-
78+
8679
List<Product> retrievedProducts = productRepository.findByName("Phone");
8780
assertEquals(phone.getId(), retrievedProducts.get(0).getId());
8881
}
89-
82+
9083
@Test
9184
public void whenSearchingProductsByQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
9285
final Product phone = new Product();
9386
phone.setId("P0001");
9487
phone.setName("Smart Phone");
95-
phone.setCategory("Electronics");
96-
phone.setDescription("New Item");
9788
productRepository.save(phone);
98-
89+
9990
final Product phoneCover = new Product();
10091
phoneCover.setId("P0002");
101-
phoneCover.setName("Cover");
102-
phoneCover.setCategory("Phone");
103-
phoneCover.setDescription("New Product");
92+
phoneCover.setName("Phone Cover");
10493
productRepository.save(phoneCover);
105-
94+
10695
final Product wirelessCharger = new Product();
10796
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");
11198
productRepository.save(wirelessCharger);
112-
99+
113100
Page<Product> result = productRepository.findByCustomQuery("Phone", new PageRequest(0, 10));
114101
assertEquals(3, result.getNumberOfElements());
115102
}
116-
103+
117104
@Test
118105
public void whenSearchingProductsByNamedQuery_thenAllMatchingProductsShouldAvialble() throws Exception {
119106
final Product phone = new Product();
120107
phone.setId("P0001");
121108
phone.setName("Smart Phone");
122-
phone.setCategory("Electronics");
123-
phone.setDescription("New Item");
124109
productRepository.save(phone);
125-
110+
126111
final Product phoneCover = new Product();
127112
phoneCover.setId("P0002");
128-
phoneCover.setName("Cover");
129-
phoneCover.setCategory("Phone");
130-
phoneCover.setDescription("New Product");
113+
phoneCover.setName("Phone Cover");
131114
productRepository.save(phoneCover);
132-
115+
133116
final Product wirelessCharger = new Product();
134117
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");
138119
productRepository.save(wirelessCharger);
139-
120+
140121
Page<Product> result = productRepository.findByNamedQuery("one", new PageRequest(0, 10));
141122
assertEquals(3, result.getNumberOfElements());
142123
}
143-
124+
144125
}

0 commit comments

Comments
 (0)