Skip to content

Commit 0e89a17

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 8acbafd + b384c48 commit 0e89a17

14 files changed

Lines changed: 699 additions & 8 deletions

File tree

‎core-java-9/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
## Core Java 9 Examples
44

5-
http://inprogress.baeldung.com/java-9-new-features/
5+
[Java 9 New Features](http://www.baeldung.com/new-java-9)

‎core-java/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@
3535
- [Java 8 Streams Advanced](http://www.baeldung.com/java-8-streams)
3636
- [Introduction to Thread Pools in Java](http://www.baeldung.com/thread-pool-java-and-guava)
3737
- [Introduction to Java 8 Streams](http://www.baeldung.com/java-8-streams-introduction)
38-
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
38+
- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join)
39+
- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java)

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
<module>spring-data-neo4j</module>
9696
<module>spring-data-redis</module>
9797
<module>spring-data-rest</module>
98+
<module>spring-data-solr</module>
9899
<module>spring-dispatcher-servlet</module>
99100
<module>spring-exceptions</module>
100101
<module>spring-freemarker</module>

‎spring-cloud/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@
1414

1515
- [Intro to Spring Cloud Netflix - Hystrix](http://www.baeldung.com/spring-cloud-netflix-hystrix)
1616
- [Dockerizing a Spring Boot Application](http://www.baeldung.com/dockerizing-spring-boot-application)
17+
- [Introduction to Spring Cloud Rest Client with Netflix Ribbon](http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon)
1718

1819

‎spring-data-solr/pom.xml‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>spring-data-solr</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>spring-data-solr</name>
10+
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<spring.version>4.2.5.RELEASE</spring.version>
15+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
16+
<spring-data-solr>2.0.4.RELEASE</spring-data-solr>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-core</artifactId>
23+
<version>${spring.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework.data</groupId>
27+
<artifactId>spring-data-solr</artifactId>
28+
<version>${spring-data-solr}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-context</artifactId>
33+
<version>${spring.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>log4j</groupId>
37+
<artifactId>log4j</artifactId>
38+
<version>1.2.16</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>junit</groupId>
42+
<artifactId>junit</artifactId>
43+
<version>4.12</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.springframework</groupId>
48+
<artifactId>spring-test</artifactId>
49+
<version>${spring.version}</version>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>${maven-surefire-plugin.version}</version>
60+
<configuration>
61+
<includes>
62+
<include>**/*IntegrationTest.java</include>
63+
</includes>
64+
</configuration>
65+
</plugin>
66+
</plugins>
67+
</build>
68+
69+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.spring.data.solr.config;
2+
3+
import org.apache.solr.client.solrj.SolrClient;
4+
import org.apache.solr.client.solrj.impl.HttpSolrClient;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.data.solr.core.SolrTemplate;
9+
import org.springframework.data.solr.repository.config.EnableSolrRepositories;
10+
11+
@Configuration
12+
@EnableSolrRepositories(basePackages = "com.baeldung.spring.data.solr.repository", namedQueriesLocation = "classpath:solr-named-queries.properties", multicoreSupport = true)
13+
@ComponentScan
14+
public class SolrConfig {
15+
16+
@Bean
17+
public SolrClient solrClient() {
18+
return new HttpSolrClient("http://localhost:8983/solr");
19+
}
20+
21+
@Bean
22+
public SolrTemplate solrTemplate(SolrClient client) throws Exception {
23+
return new SolrTemplate(client);
24+
}
25+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.spring.data.solr.model;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.solr.core.mapping.Indexed;
5+
import org.springframework.data.solr.core.mapping.SolrDocument;
6+
7+
@SolrDocument(solrCoreName = "product")
8+
public class Product {
9+
10+
@Id
11+
@Indexed(name = "id", type = "string")
12+
private String id;
13+
14+
@Indexed(name = "name", type = "string")
15+
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+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
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+
55+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.spring.data.solr.repository;
2+
3+
import java.util.List;
4+
5+
import org.springframework.data.domain.Page;
6+
import org.springframework.data.domain.Pageable;
7+
import org.springframework.data.solr.repository.Query;
8+
import org.springframework.data.solr.repository.SolrCrudRepository;
9+
10+
import com.baeldung.spring.data.solr.model.Product;
11+
12+
public interface ProductRepository extends SolrCrudRepository<Product, String> {
13+
14+
public List<Product> findByName(String name);
15+
16+
@Query("name:*?0* OR category:*?0* OR description:*?0*")
17+
public Page<Product> findByCustomQuery(String searchTerm, Pageable pageable);
18+
19+
@Query(name = "Product.findByNamedQuery")
20+
public Page<Product> findByNamedQuery(String searchTerm, Pageable pageable);
21+
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Product.findByNamedQuery=name:*?0* OR category:*?0* OR description:*?0*
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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

Comments
 (0)