-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add elasticsearch repository test #679
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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")) // | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current released version of Spring Data Elasticsearch is built against Elasticsearch 8.13.4 so. would at least use that. |
||
.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 |
---|---|---|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd inline this method |
||
|
||
assertThat(it.getKeywords()).contains(expectedWord); | ||
assertThat(it.getDate()).isAfter(expectedDate); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of parsing, wouldn't it be better to use