Skip to content

Commit d784b9e

Browse files
committed
Add elasticsearch repository test
1 parent e145037 commit d784b9e

File tree

12 files changed

+243
-157
lines changed

12 files changed

+243
-157
lines changed

elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,9 @@
1515
*/
1616
package example.springdata.elasticsearch.conference;
1717

18+
import java.time.LocalDate;
19+
import java.time.format.DateTimeFormatter;
1820
import java.util.Arrays;
19-
import java.util.List;
2021

2122
import jakarta.annotation.PostConstruct;
2223
import jakarta.annotation.PreDestroy;
@@ -31,10 +32,13 @@
3132
* @author Oliver Gierke
3233
* @author Christoph Strobl
3334
* @author Prakhar Gupta
35+
* @author Haibo Liu
3436
*/
3537
@SpringBootApplication
3638
class ApplicationConfiguration {
3739

40+
private final DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE;
41+
3842
@Autowired ElasticsearchOperations operations;
3943
@Autowired ConferenceRepository repository;
4044

@@ -51,16 +55,16 @@ public void insertDataSample() {
5155
// Save data sample
5256

5357
var documents = Arrays.asList(
54-
Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
58+
Conference.builder().date(LocalDate.parse("2014-11-06", FORMAT)).name("Spring eXchange 2014 - London")
5559
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
56-
Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London")
60+
Conference.builder().date(LocalDate.parse("2014-12-07", FORMAT)).name("Scala eXchange 2014 - London")
5761
.keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
58-
Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin")
62+
Conference.builder().date(LocalDate.parse("2014-11-20", FORMAT)).name("Elasticsearch 2014 - Berlin")
5963
.keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999))
6064
.build(), //
61-
Conference.builder().date("2014-11-12").name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
65+
Conference.builder().date(LocalDate.parse("2014-11-12", FORMAT)).name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
6266
.location(new GeoPoint(51.500152D, -0.126236D)).build(), //
63-
Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
67+
Conference.builder().date(LocalDate.parse("2014-10-04", FORMAT)).name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
6468
.location(new GeoPoint(50.0646501D, 19.9449799)).build());
6569

6670
repository.saveAll(documents);

elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/Conference.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import lombok.Builder;
2121
import lombok.Data;
2222

23+
import java.time.LocalDate;
2324
import java.util.List;
2425

2526
import org.springframework.data.annotation.Id;
@@ -31,6 +32,7 @@
3132
* @author Artur Konczak
3233
* @author Oliver Gierke
3334
* @author Christoph Strobl
35+
* @author Haibo Liu
3436
*/
3537
@Data
3638
@Builder
@@ -39,7 +41,7 @@ public class Conference {
3941

4042
private @Id String id;
4143
private String name;
42-
private @Field(type = Date) String date;
44+
private @Field(type = Date) LocalDate date;
4345
private GeoPoint location;
4446
private List<String> keywords;
4547

elasticsearch/example/src/main/java/example/springdata/elasticsearch/conference/ConferenceRepository.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,10 +15,16 @@
1515
*/
1616
package example.springdata.elasticsearch.conference;
1717

18+
import java.time.LocalDate;
19+
1820
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
1921

2022
/**
2123
* @author Artur Konczak
2224
* @author Oliver Gierke
25+
* @author Haibo Liu
2326
*/
24-
interface ConferenceRepository extends ElasticsearchRepository<Conference, String> {}
27+
interface ConferenceRepository extends ElasticsearchRepository<Conference, String> {
28+
29+
Iterable<Conference> findAllByKeywordsContainsAndDateAfter(String keyword, LocalDate Date);
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.elasticsearch.conference;
17+
18+
import java.time.format.DateTimeFormatter;
19+
20+
import org.springframework.boot.test.context.SpringBootTest;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.data.elasticsearch.client.ClientConfiguration;
23+
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
24+
import org.springframework.lang.NonNull;
25+
import org.springframework.util.Assert;
26+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
27+
import org.testcontainers.utility.DockerImageName;
28+
29+
/**
30+
* singleton container
31+
*
32+
* @author Haibo Liu
33+
*/
34+
@SpringBootTest(classes = {ApplicationConfiguration.class, AbstractContainerBaseTest.TestConfiguration.class})
35+
public class AbstractContainerBaseTest {
36+
37+
protected static final DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE;
38+
39+
private static final ElasticsearchContainer CONTAINER = new ElasticsearchContainer(
40+
DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.7.0")) //
41+
.withPassword("foobar");
42+
43+
static {
44+
CONTAINER.start();
45+
}
46+
47+
@Configuration
48+
static class TestConfiguration extends ElasticsearchConfiguration {
49+
@Override
50+
@NonNull
51+
public ClientConfiguration clientConfiguration() {
52+
53+
Assert.notNull(CONTAINER, "TestContainer is not initialized!");
54+
55+
return ClientConfiguration.builder() //
56+
.connectedTo(CONTAINER.getHttpHostAddress()) //
57+
.usingSsl(CONTAINER.createSslContextFromCa()) //
58+
.withBasicAuth("elastic", "foobar") //
59+
.build();
60+
}
61+
}
62+
}

elasticsearch/example/src/test/java/example/springdata/elasticsearch/conference/ElasticsearchOperationsTest.java

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,25 +17,15 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import java.text.ParseException;
21-
import java.text.SimpleDateFormat;
20+
import java.time.LocalDate;
2221

2322
import org.junit.jupiter.api.Test;
2423
import org.springframework.beans.factory.annotation.Autowired;
25-
import org.springframework.boot.test.context.SpringBootTest;
26-
import org.springframework.context.annotation.Configuration;
27-
import org.springframework.data.elasticsearch.client.ClientConfiguration;
28-
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
2924
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
3025
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
3126
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
3227
import org.springframework.data.elasticsearch.core.query.Criteria;
3328
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
34-
import org.springframework.util.Assert;
35-
import org.testcontainers.elasticsearch.ElasticsearchContainer;
36-
import org.testcontainers.junit.jupiter.Container;
37-
import org.testcontainers.junit.jupiter.Testcontainers;
38-
import org.testcontainers.utility.DockerImageName;
3929

4030
/**
4131
* Test case to show Spring Data Elasticsearch functionality.
@@ -45,39 +35,16 @@
4535
* @author Christoph Strobl
4636
* @author Prakhar Gupta
4737
* @author Peter-Josef Meisch
38+
* @author Haibo Liu
4839
*/
49-
@SpringBootTest(classes = { ApplicationConfiguration.class, ElasticsearchOperationsTest.TestConfiguration.class })
50-
@Testcontainers
51-
class ElasticsearchOperationsTest {
52-
53-
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
54-
@Container //
55-
private static final ElasticsearchContainer container = new ElasticsearchContainer(
56-
DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.7.0")) //
57-
.withPassword("foobar") //
58-
.withReuse(true);
59-
60-
@Configuration
61-
static class TestConfiguration extends ElasticsearchConfiguration {
62-
@Override
63-
public ClientConfiguration clientConfiguration() {
64-
65-
Assert.notNull(container, "TestContainer is not initialized!");
66-
67-
return ClientConfiguration.builder() //
68-
.connectedTo(container.getHttpHostAddress()) //
69-
.usingSsl(container.createSslContextFromCa()) //
70-
.withBasicAuth("elastic", "foobar") //
71-
.build();
72-
}
73-
}
40+
class ElasticsearchOperationsTest extends AbstractContainerBaseTest {
7441

7542
@Autowired ElasticsearchOperations operations;
7643

7744
@Test
78-
void textSearch() throws ParseException {
45+
void textSearch() {
7946

80-
var expectedDate = "2014-10-29";
47+
var expectedDate = LocalDate.parse( "2014-10-29", FORMAT);
8148
var expectedWord = "java";
8249
var query = new CriteriaQuery(
8350
new Criteria("keywords").contains(expectedWord).and(new Criteria("date").greaterThanEqual(expectedDate)));
@@ -88,7 +55,7 @@ void textSearch() throws ParseException {
8855

8956
for (var conference : result) {
9057
assertThat(conference.getContent().getKeywords()).contains(expectedWord);
91-
assertThat(format.parse(conference.getContent().getDate())).isAfter(format.parse(expectedDate));
58+
assertThat(conference.getContent().getDate()).isAfter(expectedDate);
9259
}
9360
}
9461

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.elasticsearch.conference;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.time.LocalDate;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
25+
/**
26+
* Test case to show Spring Data Elasticsearch Repository functionality.
27+
*
28+
* @author Haibo Liu
29+
*/
30+
class ElasticsearchRepositoryTest extends AbstractContainerBaseTest {
31+
32+
@Autowired ConferenceRepository repository;
33+
34+
@Test
35+
void textSearch() {
36+
37+
var expectedDate = LocalDate.parse("2014-10-29", FORMAT);
38+
var expectedWord = "java";
39+
40+
var result = repository.findAllByKeywordsContainsAndDateAfter(expectedWord, expectedDate);
41+
42+
assertThat(result).hasSize(3);
43+
44+
result.forEach(it -> verify(it, expectedWord, expectedDate));
45+
}
46+
47+
private void verify(Conference it, String expectedWord, LocalDate expectedDate) {
48+
49+
assertThat(it.getKeywords()).contains(expectedWord);
50+
assertThat(it.getDate()).isAfter(expectedDate);
51+
}
52+
}

elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/ApplicationConfiguration.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package example.springdata.elasticsearch.conference;
1717

18+
import java.time.LocalDate;
19+
import java.time.format.DateTimeFormatter;
1820
import java.util.Arrays;
1921

2022
import jakarta.annotation.PostConstruct;
@@ -27,10 +29,13 @@
2729

2830
/**
2931
* @author Christoph Strobl
32+
* @author Haibo Liu
3033
*/
3134
@SpringBootApplication
3235
class ApplicationConfiguration {
3336

37+
private final DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE;
38+
3439
@Autowired ElasticsearchOperations operations;
3540
@Autowired ConferenceRepository repository;
3641

@@ -47,16 +52,16 @@ public void insertDataSample() {
4752
// Save data sample
4853

4954
var documents = Arrays.asList(
50-
Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
55+
Conference.builder().date(LocalDate.parse("2014-11-06", FORMAT)).name("Spring eXchange 2014 - London")
5156
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
52-
Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London")
57+
Conference.builder().date(LocalDate.parse("2014-12-07", FORMAT)).name("Scala eXchange 2014 - London")
5358
.keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
54-
Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin")
59+
Conference.builder().date(LocalDate.parse("2014-11-20", FORMAT)).name("Elasticsearch 2014 - Berlin")
5560
.keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999))
5661
.build(), //
57-
Conference.builder().date("2014-11-12").name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
62+
Conference.builder().date(LocalDate.parse("2014-11-12", FORMAT)).name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
5863
.location(new GeoPoint(51.500152D, -0.126236D)).build(), //
59-
Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
64+
Conference.builder().date(LocalDate.parse("2014-10-04", FORMAT)).name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
6065
.location(new GeoPoint(50.0646501D, 19.9449799)).build());
6166

6267
operations.save(documents);

elasticsearch/reactive/src/main/java/example/springdata/elasticsearch/conference/Conference.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import lombok.Builder;
2121
import lombok.Data;
2222

23+
import java.time.LocalDate;
2324
import java.util.List;
2425

2526
import org.springframework.data.annotation.Id;
@@ -29,6 +30,7 @@
2930

3031
/**
3132
* @author Christoph Strobl
33+
* @author Haibo Liu
3234
*/
3335
@Data
3436
@Builder
@@ -37,7 +39,7 @@ public class Conference {
3739

3840
private @Id String id;
3941
private String name;
40-
private @Field(type = Date) String date;
42+
private @Field(type = Date) LocalDate date;
4143
private GeoPoint location;
4244
private List<String> keywords;
4345
}

0 commit comments

Comments
 (0)