Skip to content

Commit b5381a5

Browse files
authored
add example app for java client & update testcontainers to OS 2.19.1 (opensearch-project#438)
* add `TestMarketplaceApplication` also to java client example this allows easily launching a test application for the java client. the code has been copied from the RHLC based example. Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com> * update all testcontainers to OpenSearch 2.19.1 this aligns them all to one version, making future version updates easier (simple search & replace). 2.19.1 is the latest available version at the moment. Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com> --------- Signed-off-by: Ralph Ursprung <Ralph.Ursprung@avaloq.com>
1 parent 358ea76 commit b5381a5

File tree

10 files changed

+68
-13
lines changed

10 files changed

+68
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ See https://docs.spring.io/spring-boot/reference/testing/testcontainers.html#tes
302302
```java
303303
@Container
304304
@ServiceConnection
305-
static final OpenSearchContainer<?> container = new OpenSearchContainer<>("opensearchproject/opensearch:2.15.0");
305+
static final OpenSearchContainer<?> container = new OpenSearchContainer<>("opensearchproject/opensearch:2.19.1");
306306
```
307307

308308
So, the client will take values from the `OpenSearchContainer` configuration.

spring-data-opensearch-examples/spring-boot-gradle/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Spring Data OpenSearch Spring Boot Example Project
22
===
33

44
This sample project demonstrates the usage of the [Spring Data OpenSearch](https://github.com/opensearch-project/spring-data-opensearch/) in the typical Spring Boot web application. The application assumes that there is an [OpenSearch](https://opensearch.org) service up and running on the local machine.
5+
This example uses the [`RestHighLevelClient`](https://opensearch.org/docs/latest/clients/java-rest-high-level/).
56

67
## Pre-requisites
78

@@ -40,4 +41,4 @@ docker run -p 9200:9200 -e "discovery.type=single-node" -e OPENSEARCH_INITIAL_AD
4041

4142
- Fetch all products: `curl 'http://localhost:8080/marketplace/search'`
4243
- Search products by name: `curl 'http://localhost:8080/marketplace/search?name=pillow'`
43-
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`
44+
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`

spring-data-opensearch-examples/spring-boot-gradle/src/test/java/org/opensearch/data/example/repository/MarketplaceRepositoryIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@Tag("integration-test")
2929
public class MarketplaceRepositoryIntegrationTests {
3030
@Container
31-
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.11.1")
31+
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.19.1")
3232
.withStartupAttempts(5)
3333
.withStartupTimeout(Duration.ofMinutes(2));
3434

spring-data-opensearch-examples/spring-boot-gradle/src/test/java/org/opensearch/data/example/repository/TestMarketplaceApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static class ContainerConfig {
2121
@Bean
2222
@ServiceConnection
2323
OpensearchContainer<?> opensearchContainer() {
24-
return new OpensearchContainer<>("opensearchproject/opensearch:2.15.0");
24+
return new OpensearchContainer<>("opensearchproject/opensearch:2.19.1");
2525
}
2626

2727
}
Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
Spring Data OpenSearch Java Client Spring Boot Example Project
22
===
33

4-
This sample project demonstrates the usage of the [Spring Data OpenSearch](https://github.com/opensearch-project/spring-data-opensearch/) in the typical Spring Boot web application using [opensearch-java](https://github.com/opensearch-project/opensearch-java) client. The application assumes that there is an [OpenSearch](https://opensearch.org) service up and running on the local machine, available at `https://localhost:9200` (protected by basic authentication with default credentials).
4+
This sample project demonstrates the usage of the [Spring Data OpenSearch](https://github.com/opensearch-project/spring-data-opensearch/) in the typical Spring Boot web application. The application assumes that there is an [OpenSearch](https://opensearch.org) service up and running on the local machine.
5+
This example uses the [`opensearch-java` client](https://opensearch.org/docs/latest/clients/java/).
56

6-
1. The easiest way to get [OpenSearch](https://opensearch.org) service up and running is by using [Docker](https://www.docker.com/):
7+
## Pre-requisites
8+
9+
* [Docker](https://www.docker.com/)
10+
* Java 17
11+
12+
## Using Docker CLI
13+
14+
1. Start [OpenSearch](https://opensearch.org) using
715

816
```shell
917
docker run -p 9200:9200 -e "discovery.type=single-node" -e OPENSEARCH_INITIAL_ADMIN_PASSWORD=<strong-password> opensearchproject/opensearch:2.15.0
@@ -12,11 +20,25 @@ docker run -p 9200:9200 -e "discovery.type=single-node" -e OPENSEARCH_INITIAL_AD
1220
2. Build and run the project using [Gradle](https://gradle.org/):
1321

1422
```shell
15-
./gradlew :spring-data-opensearch-examples:spring-boot-java-client-gradle:bootRun
23+
./gradlew :spring-data-opensearch-examples:spring-boot-gradle:bootRun
1624
```
1725

1826
3. Exercise the REST endpoint available at: `http://localhost:8080/marketplace`
1927

2028
- Fetch all products: `curl 'http://localhost:8080/marketplace/search'`
2129
- Search products by name: `curl 'http://localhost:8080/marketplace/search?name=pillow'`
22-
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`
30+
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`
31+
32+
## Using Spring Boot Testcontainers integration
33+
34+
1. Build and run the project using [Gradle](https://gradle.org/):
35+
36+
```shell
37+
./gradlew :spring-data-opensearch-examples:spring-boot-gradle:bootTestRun
38+
```
39+
40+
2. Exercise the REST endpoint available at: `http://localhost:8080/marketplace`
41+
42+
- Fetch all products: `curl 'http://localhost:8080/marketplace/search'`
43+
- Search products by name: `curl 'http://localhost:8080/marketplace/search?name=pillow'`
44+
- Search products by name and price greater than: `curl 'http://localhost:8080/marketplace/search?name=pillow&price=35.0'`

spring-data-opensearch-examples/spring-boot-java-client-gradle/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ dependencies {
3232
testImplementation(springLibs.test)
3333
testImplementation(springLibs.boot.test)
3434
testImplementation(springLibs.boot.test.autoconfigure)
35+
testImplementation(springLibs.boot.testcontainers)
3536
testImplementation(opensearchLibs.testcontainers)
37+
testImplementation(project(":spring-data-opensearch-testcontainers")) {
38+
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
39+
}
3640
testImplementation(project(":spring-data-opensearch-test-autoconfigure")) {
3741
exclude("org.opensearch.client", "opensearch-rest-high-level-client")
3842
}

spring-data-opensearch-examples/spring-boot-java-client-gradle/src/test/java/org/opensearch/data/example/repository/MarketplaceRepositoryIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@Tag("integration-test")
2929
public class MarketplaceRepositoryIntegrationTests {
3030
@Container
31-
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.11.1")
31+
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.19.1")
3232
.withStartupAttempts(5)
3333
.withStartupTimeout(Duration.ofMinutes(2));
3434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.opensearch.data.example.repository;
2+
3+
import org.opensearch.data.example.MarketplaceApplication;
4+
import org.opensearch.testcontainers.OpensearchContainer;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.test.context.TestConfiguration;
7+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
8+
import org.springframework.context.annotation.Bean;
9+
10+
public class TestMarketplaceApplication {
11+
12+
public static void main(String[] args) {
13+
SpringApplication.from(MarketplaceApplication::main)
14+
.with(ContainerConfig.class)
15+
.run(args);
16+
}
17+
18+
@TestConfiguration(proxyBeanMethods = false)
19+
static class ContainerConfig {
20+
21+
@Bean
22+
@ServiceConnection
23+
OpensearchContainer<?> opensearchContainer() {
24+
return new OpensearchContainer<>("opensearchproject/opensearch:2.19.1");
25+
}
26+
27+
}
28+
}

spring-data-opensearch-testcontainers/src/test/java/org/opensearch/spring/boot/testcontainers/service/connection/OpenSearchContainerConnectionDetailsFactoryIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class OpenSearchContainerConnectionDetailsFactoryIntegrationTests {
3434

3535
@Container
3636
@ServiceConnection
37-
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.15.0")
37+
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.19.1")
3838
.withStartupAttempts(5)
3939
.withStartupTimeout(Duration.ofMinutes(10));
4040

@@ -48,7 +48,7 @@ void restClientOpensearchNodeVersion() throws IOException {
4848
try (InputStream input = response.getEntity().getContent()) {
4949
JsonNode result = new ObjectMapper().readTree(input);
5050
assertThat(result.path("version").path("number").asText())
51-
.isEqualTo("2.15.0");
51+
.isEqualTo("2.19.1");
5252
}
5353
}
5454

spring-data-opensearch-testcontainers/src/test/java/org/opensearch/spring/boot/testcontainers/service/connection/SecureOpenSearchContainerConnectionDetailsFactoryIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SecureOpenSearchContainerConnectionDetailsFactoryIntegrationTests {
4343

4444
@Container
4545
@ServiceConnection
46-
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.15.0")
46+
static final OpensearchContainer<?> opensearch = new OpensearchContainer<>("opensearchproject/opensearch:2.19.1")
4747
.withSecurityEnabled()
4848
.withEnv("OPENSEARCH_INITIAL_ADMIN_PASSWORD", "D3v3l0p-ment");
4949

@@ -57,7 +57,7 @@ void restClientOpensearchNodeVersion() throws IOException {
5757
try (InputStream input = response.getEntity().getContent()) {
5858
JsonNode result = new ObjectMapper().readTree(input);
5959
assertThat(result.path("version").path("number").asText())
60-
.isEqualTo("2.15.0");
60+
.isEqualTo("2.19.1");
6161
}
6262
}
6363

0 commit comments

Comments
 (0)