Skip to content
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 an integration test that runs on JDK-8 #795

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/test-jdk8-compat-unit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Compatibility Unit Tests (JDK-8)

on: [push, pull_request]

jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- name: Checkout Java Client
uses: actions/checkout@v3

- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: 8
distribution: 'temurin'
cache: 'gradle'

- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: 11
distribution: 'temurin'
cache: 'gradle'

- name: Run Unit Test
run: ./gradlew clean unitTest -Pcheck-jdk8-compatibility=true
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Changed
- Restore support for Java 8 ([#767](https://github.com/opensearch-project/opensearch-java/pull/767))
- Add an integration test that runs on JDK-8 ([#795](https://github.com/opensearch-project/opensearch-java/pull/795))

### Deprecated
- Deprecated "_toQuery()" in Query and QueryVariant ([#760](https://github.com/opensearch-project/opensearch-java/pull/760)
Expand Down
19 changes: 17 additions & 2 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import com.github.jk1.license.ProjectData
import com.github.jk1.license.render.ReportRenderer
import org.gradle.api.tasks.testing.Test
import java.io.FileWriter

buildscript {
Expand Down Expand Up @@ -337,7 +338,9 @@ publishing {
}
}

if (JavaVersion.current() >= JavaVersion.VERSION_11) {
// Use `-Pcheck-jdk8-compatibility=true` to
val jdk8compatibility = (project.findProperty("check-jdk8-compatibility") as String?).toBoolean()
if (JavaVersion.current() >= JavaVersion.VERSION_11 && jdk8compatibility == false) {
Copy link
Member

@dblock dblock Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dislike the whole check-jdk8-compatibility=true business. Why do we need this at all and not just this?

if (JavaVersion.current() >= JavaVersion.VERSION_11) {
 ...
} else if (JavaVersion.current() >= JavaVersion.VERSION_8) {
 ...
}

Copy link
Collaborator Author

@reta reta Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That the thing, the JavaVersion.current() is the version Gradle run with, it is always 11 or higher. I will try to experiment with toolchains here (alternatively, we could make all tests JDK-8 compatible and that would eliminate the conditional part)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see.

Maybe we could improve on this by introducing optional JAVA_TOOLCHAIN_VERSION and default it to 11. Basically avoiding this whole if logic?

Stepping back, this does seem to work, so we should merge it and improve it later if you can't come up with something a lot nicer. Leave some TODOs with an explanation at the least?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea, thanks @dblock , I refactored it to use similar to OpenSearch core approach with runtime.java version.

val java11: SourceSet = sourceSets.create("java11") {
java {
compileClasspath += sourceSets.main.get().output + sourceSets.test.get().output
Expand Down Expand Up @@ -369,5 +372,17 @@ if (JavaVersion.current() >= JavaVersion.VERSION_11) {
testClassesDirs += java11.output.classesDirs
classpath = sourceSets["java11"].runtimeClasspath
}

} else if (jdk8compatibility == true) {
java {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dblock @VachaShah curious what you think about this approach: we cannot run Gradle build using JDK-8 but we could use toolchains to run tests under JDK-8.

toolchain {
languageVersion = JavaLanguageVersion.of(8)
vendor = JvmVendorSpec.ADOPTIUM
}
}

tasks.register<Test>("tests-jdk8") {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(8)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch._types.GeoLocation;
import org.opensearch.client.opensearch.model.ModelTestCase;
Expand All @@ -9,7 +9,7 @@ public class GeoDistanceQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
GeoDistanceQuery origin = new GeoDistanceQuery.Builder().field("field")
.location(new GeoLocation.Builder().coords(List.of(1.0)).build())
.location(new GeoLocation.Builder().coords(Collections.singletonList(1.0)).build())
.build();
GeoDistanceQuery copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class PinnedQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
PinnedQuery origin = new PinnedQuery.Builder().organic(buildDummyQuery()).ids(List.of("1")).build();
PinnedQuery origin = new PinnedQuery.Builder().organic(buildDummyQuery()).ids(Collections.singletonList("1")).build();
PinnedQuery copied = origin.toBuilder().build();

assertEquals(toJson(copied), toJson(origin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanContainingQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanContainingQuery origin = new SpanContainingQuery.Builder().big(
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build()
).little(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build()).build();
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()
).little(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()).build();
SpanContainingQuery copied = origin.toBuilder().build();

assertEquals(toJson(copied), toJson(origin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanFieldMaskingQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanFieldMaskingQuery origin = new SpanFieldMaskingQuery.Builder().field("field")
.query(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build())
.query(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build())
.build();
SpanFieldMaskingQuery copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanFirstQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanFirstQuery origin = new SpanFirstQuery.Builder().end(1)
.match(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build())
.match(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build())
.build();
SpanFirstQuery copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanNearQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanNearQuery origin = new SpanNearQuery.Builder().clauses(
List.of(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build())
Collections.singletonList(
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()
)
).build();
SpanNearQuery copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanNotQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanNotQuery origin = new SpanNotQuery.Builder().include(
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build()
).exclude(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build()).build();
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()
).exclude(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()).build();
SpanNotQuery copied = origin.toBuilder().build();

assertEquals(toJson(copied), toJson(origin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanOrQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanOrQuery origin = new SpanOrQuery.Builder().clauses(
List.of(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build())
Collections.singletonList(
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()
)
).build();
SpanOrQuery copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanQuery origin = new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build();
SpanQuery origin = new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build();
SpanQuery copied = origin.toBuilder().build();

assertEquals(toJson(copied), toJson(origin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SpanWithinQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
SpanWithinQuery origin = new SpanWithinQuery.Builder().big(
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build()
).little(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(List.of()).build()).build()).build();
new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()
).little(new SpanQuery.Builder().spanOr(new SpanOrQuery.Builder().clauses(Collections.emptyList()).build()).build()).build();
SpanWithinQuery copied = origin.toBuilder().build();

assertEquals(toJson(copied), toJson(origin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class TermsQueryFieldTest extends ModelTestCase {
@Test
public void toBuilder() {
TermsQueryField origin = new TermsQueryField.Builder().value(List.of(FieldValue.of("1"))).build();
TermsQueryField origin = new TermsQueryField.Builder().value(Collections.singletonList(FieldValue.of("1"))).build();
TermsQueryField copied = origin.toBuilder().build();

assertEquals(toJson(copied), toJson(origin));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.opensearch.client.opensearch._types.query_dsl;

import java.util.List;
import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;
Expand All @@ -9,7 +9,7 @@ public class TermsQueryTest extends ModelTestCase {
@Test
public void toBuilder() {
TermsQuery origin = new TermsQuery.Builder().field("field")
.terms(new TermsQueryField.Builder().value(List.of(FieldValue.of("1"))).build())
.terms(new TermsQueryField.Builder().value(Collections.singletonList(FieldValue.of("1"))).build())
.build();
TermsQuery copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.opensearch.client.opensearch.core;

import java.util.List;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;
import org.opensearch.client.opensearch.core.bulk.BulkOperation;
Expand All @@ -11,7 +11,9 @@ public class BulkRequestTest extends Assert {
@Test
public void toBuilder() {
BulkRequest origin = new BulkRequest.Builder().index("index")
.operations(List.of(new BulkOperation.Builder().delete(new DeleteOperation.Builder().id("id").build()).build()))
.operations(
Collections.singletonList(new BulkOperation.Builder().delete(new DeleteOperation.Builder().id("id").build()).build())
)
.build();
BulkRequest copied = origin.toBuilder().build();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.opensearch.client.opensearch.core;

import java.util.List;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;

public class ClearScrollRequestTest extends Assert {

@Test
public void toBuilder() {
ClearScrollRequest origin = new ClearScrollRequest.Builder().scrollId(List.of("1")).build();
ClearScrollRequest origin = new ClearScrollRequest.Builder().scrollId(Collections.singletonList("1")).build();
ClearScrollRequest copied = origin.toBuilder().build();

assertEquals(copied.scrollId(), origin.scrollId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.opensearch.client.opensearch.core;

import java.util.List;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;

public class MsearchRequestTest extends Assert {

@Test
public void toBuilder() {
MsearchRequest origin = new MsearchRequest.Builder().index("index").searches(List.of()).build();
MsearchRequest origin = new MsearchRequest.Builder().index("index").searches(Collections.emptyList()).build();
MsearchRequest copied = origin.toBuilder().build();

assertEquals(copied.index(), origin.index());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package org.opensearch.client.opensearch.core;

import java.util.List;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Test;

public class MsearchTemplateRequestTest extends Assert {

@Test
public void toBuilder() {
MsearchTemplateRequest origin = new MsearchTemplateRequest.Builder().index("index").searchTemplates(List.of()).build();
MsearchTemplateRequest origin = new MsearchTemplateRequest.Builder().index("index")
.searchTemplates(Collections.emptyList())
.build();
MsearchTemplateRequest copied = origin.toBuilder().build();

assertEquals(copied.index(), origin.index());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.util.List;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -16,15 +18,19 @@ public class PutIndexTemplateRequestTest extends Assert {
@Test
public void deserialize_validFieldsIncluded_RequestIsBuilt() throws JsonProcessingException {
final JsonpMapper mapper = new JsonbJsonpMapper();
final Map<String, Object> indexTemplateMap = Map.of("name", "test", "index_patterns", "*", "create", true, "priority", 1);
final Map<String, Object> indexTemplateMap = new HashMap<>();
indexTemplateMap.put("name", "test");
indexTemplateMap.put("index_patterns", "*");
indexTemplateMap.put("create", true);
indexTemplateMap.put("priority", 1);

final String indexTemplate = new ObjectMapper().writeValueAsString(indexTemplateMap);
final var parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));
final JsonParser parser = mapper.jsonProvider().createParser(new StringReader(indexTemplate));

final PutIndexTemplateRequest putIndexTemplateRequest = PutIndexTemplateRequest._DESERIALIZER.deserialize(parser, mapper);

assertEquals(putIndexTemplateRequest.name(), "test");
assertEquals(putIndexTemplateRequest.indexPatterns(), List.of("*"));
assertEquals(putIndexTemplateRequest.indexPatterns(), Collections.singletonList("*"));
assertEquals((int) putIndexTemplateRequest.priority(), 1);
}

Expand Down
Loading
Loading