Skip to content

Commit

Permalink
Add elasticsearch repository test
Browse files Browse the repository at this point in the history
  • Loading branch information
puppylpg committed Jan 23, 2024
1 parent 4caa29c commit 4ef5c4d
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 157 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,8 +15,9 @@
*/
package example.springdata.elasticsearch.conference;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
Expand All @@ -31,10 +32,13 @@
* @author Oliver Gierke
* @author Christoph Strobl
* @author Prakhar Gupta
* @author Haibo Liu
*/
@SpringBootApplication
class ApplicationConfiguration {

private final DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE;

@Autowired ElasticsearchOperations operations;
@Autowired ConferenceRepository repository;

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

var documents = Arrays.asList(
Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
Conference.builder().date(LocalDate.parse("2014-11-06", FORMAT)).name("Spring eXchange 2014 - London")
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London")
Conference.builder().date(LocalDate.parse("2014-12-07", FORMAT)).name("Scala eXchange 2014 - London")
.keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin")
Conference.builder().date(LocalDate.parse("2014-11-20", FORMAT)).name("Elasticsearch 2014 - Berlin")
.keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999))
.build(), //
Conference.builder().date("2014-11-12").name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
Conference.builder().date(LocalDate.parse("2014-11-12", FORMAT)).name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
.location(new GeoPoint(51.500152D, -0.126236D)).build(), //
Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
Conference.builder().date(LocalDate.parse("2014-10-04", FORMAT)).name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
.location(new GeoPoint(50.0646501D, 19.9449799)).build());

repository.saveAll(documents);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;
import java.util.List;

import org.springframework.data.annotation.Id;
Expand All @@ -31,6 +32,7 @@
* @author Artur Konczak
* @author Oliver Gierke
* @author Christoph Strobl
* @author Haibo Liu
*/
@Data
@Builder
Expand All @@ -39,7 +41,7 @@ public class Conference {

private @Id String id;
private String name;
private @Field(type = Date) String date;
private @Field(type = Date) LocalDate date;
private GeoPoint location;
private List<String> keywords;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,10 +15,16 @@
*/
package example.springdata.elasticsearch.conference;

import java.time.LocalDate;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
* @author Artur Konczak
* @author Oliver Gierke
* @author Haibo Liu
*/
interface ConferenceRepository extends ElasticsearchRepository<Conference, String> {}
interface ConferenceRepository extends ElasticsearchRepository<Conference, String> {

Iterable<Conference> findAllByKeywordsContainsAndDateAfter(String keyword, LocalDate Date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.elasticsearch.conference;

import java.time.format.DateTimeFormatter;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
import org.springframework.lang.NonNull;
import org.springframework.util.Assert;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.utility.DockerImageName;

/**
* singleton container
*
* @author Haibo Liu
*/
@SpringBootTest(classes = {ApplicationConfiguration.class, AbstractContainerBaseTest.TestConfiguration.class})
public class AbstractContainerBaseTest {

protected static final DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE;

private static final ElasticsearchContainer CONTAINER = new ElasticsearchContainer(
DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.7.0")) //
.withPassword("foobar");

static {
CONTAINER.start();
}

@Configuration
static class TestConfiguration extends ElasticsearchConfiguration {
@Override
@NonNull
public ClientConfiguration clientConfiguration() {

Assert.notNull(CONTAINER, "TestContainer is not initialized!");

return ClientConfiguration.builder() //
.connectedTo(CONTAINER.getHttpHostAddress()) //
.usingSsl(CONTAINER.createSslContextFromCa()) //
.withBasicAuth("elastic", "foobar") //
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,25 +17,15 @@

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.elc.ElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.util.Assert;
import org.testcontainers.elasticsearch.ElasticsearchContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

/**
* Test case to show Spring Data Elasticsearch functionality.
Expand All @@ -45,39 +35,16 @@
* @author Christoph Strobl
* @author Prakhar Gupta
* @author Peter-Josef Meisch
* @author Haibo Liu
*/
@SpringBootTest(classes = { ApplicationConfiguration.class, ElasticsearchOperationsTest.TestConfiguration.class })
@Testcontainers
class ElasticsearchOperationsTest {

private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
@Container //
private static final ElasticsearchContainer container = new ElasticsearchContainer(
DockerImageName.parse("docker.elastic.co/elasticsearch/elasticsearch:8.7.0")) //
.withPassword("foobar") //
.withReuse(true);

@Configuration
static class TestConfiguration extends ElasticsearchConfiguration {
@Override
public ClientConfiguration clientConfiguration() {

Assert.notNull(container, "TestContainer is not initialized!");

return ClientConfiguration.builder() //
.connectedTo(container.getHttpHostAddress()) //
.usingSsl(container.createSslContextFromCa()) //
.withBasicAuth("elastic", "foobar") //
.build();
}
}
class ElasticsearchOperationsTest extends AbstractContainerBaseTest {

@Autowired ElasticsearchOperations operations;

@Test
void textSearch() throws ParseException {
void textSearch() {

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

for (var conference : result) {
assertThat(conference.getContent().getKeywords()).contains(expectedWord);
assertThat(format.parse(conference.getContent().getDate())).isAfter(format.parse(expectedDate));
assertThat(conference.getContent().getDate()).isAfter(expectedDate);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.elasticsearch.conference;

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

import java.time.LocalDate;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Test case to show Spring Data Elasticsearch Repository functionality.
*
* @author Haibo Liu
*/
class ElasticsearchRepositoryTest extends AbstractContainerBaseTest {

@Autowired ConferenceRepository repository;

@Test
void textSearch() {

var expectedDate = LocalDate.parse("2014-10-29", FORMAT);
var expectedWord = "java";

var result = repository.findAllByKeywordsContainsAndDateAfter(expectedWord, expectedDate);

assertThat(result).hasSize(3);

result.forEach(it -> verify(it, expectedWord, expectedDate));
}

private void verify(Conference it, String expectedWord, LocalDate expectedDate) {

assertThat(it.getKeywords()).contains(expectedWord);
assertThat(it.getDate()).isAfter(expectedDate);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,8 @@
*/
package example.springdata.elasticsearch.conference;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

import jakarta.annotation.PostConstruct;
Expand All @@ -27,10 +29,13 @@

/**
* @author Christoph Strobl
* @author Haibo Liu
*/
@SpringBootApplication
class ApplicationConfiguration {

private final DateTimeFormatter FORMAT = DateTimeFormatter.ISO_LOCAL_DATE;

@Autowired ElasticsearchOperations operations;
@Autowired ConferenceRepository repository;

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

var documents = Arrays.asList(
Conference.builder().date("2014-11-06").name("Spring eXchange 2014 - London")
Conference.builder().date(LocalDate.parse("2014-11-06", FORMAT)).name("Spring eXchange 2014 - London")
.keywords(Arrays.asList("java", "spring")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
Conference.builder().date("2014-12-07").name("Scala eXchange 2014 - London")
Conference.builder().date(LocalDate.parse("2014-12-07", FORMAT)).name("Scala eXchange 2014 - London")
.keywords(Arrays.asList("scala", "play", "java")).location(new GeoPoint(51.500152D, -0.126236D)).build(), //
Conference.builder().date("2014-11-20").name("Elasticsearch 2014 - Berlin")
Conference.builder().date(LocalDate.parse("2014-11-20", FORMAT)).name("Elasticsearch 2014 - Berlin")
.keywords(Arrays.asList("java", "elasticsearch", "kibana")).location(new GeoPoint(52.5234051D, 13.4113999))
.build(), //
Conference.builder().date("2014-11-12").name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
Conference.builder().date(LocalDate.parse("2014-11-12", FORMAT)).name("AWS London 2014").keywords(Arrays.asList("cloud", "aws"))
.location(new GeoPoint(51.500152D, -0.126236D)).build(), //
Conference.builder().date("2014-10-04").name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
Conference.builder().date(LocalDate.parse("2014-10-04", FORMAT)).name("JDD14 - Cracow").keywords(Arrays.asList("java", "spring"))
.location(new GeoPoint(50.0646501D, 19.9449799)).build());

operations.save(documents);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2021 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;
import java.util.List;

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

/**
* @author Christoph Strobl
* @author Haibo Liu
*/
@Data
@Builder
Expand All @@ -37,7 +39,7 @@ public class Conference {

private @Id String id;
private String name;
private @Field(type = Date) String date;
private @Field(type = Date) LocalDate date;
private GeoPoint location;
private List<String> keywords;
}
Loading

0 comments on commit 4ef5c4d

Please sign in to comment.