-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
243 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
.../src/test/java/example/springdata/elasticsearch/conference/AbstractContainerBaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...rc/test/java/example/springdata/elasticsearch/conference/ElasticsearchRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.