From 957fe0531f5be7b9801e3e9c9dc89655a1c59ec3 Mon Sep 17 00:00:00 2001 From: Peter-Josef Meisch Date: Fri, 19 Jan 2024 21:51:12 +0100 Subject: [PATCH] Remove deprecated code; add arch unit tests. Original Pull Request #2832 Closes #2831 --- pom.xml | 7 + .../migration-guide-5.2-5.3.adoc | 4 + .../client/elc/QueryBuilders.java | 174 ------------------ .../ClientArchitectureTests.java | 23 +++ .../data/elasticsearch/ELCQueries.java | 59 ------ 5 files changed, 34 insertions(+), 233 deletions(-) delete mode 100644 src/main/java/org/springframework/data/elasticsearch/client/elc/QueryBuilders.java create mode 100644 src/test/java/org/springframework/data/elasticsearch/ClientArchitectureTests.java delete mode 100644 src/test/java/org/springframework/data/elasticsearch/ELCQueries.java diff --git a/pom.xml b/pom.xml index 78b6e2640..3877b7909 100644 --- a/pom.xml +++ b/pom.xml @@ -324,6 +324,13 @@ test + + com.tngtech.archunit + archunit-junit5 + ${archunit} + test + + diff --git a/src/main/antora/modules/ROOT/pages/migration-guides/migration-guide-5.2-5.3.adoc b/src/main/antora/modules/ROOT/pages/migration-guides/migration-guide-5.2-5.3.adoc index a79f18800..17ab9063c 100644 --- a/src/main/antora/modules/ROOT/pages/migration-guides/migration-guide-5.2-5.3.adoc +++ b/src/main/antora/modules/ROOT/pages/migration-guides/migration-guide-5.2-5.3.adoc @@ -9,3 +9,7 @@ This section describes breaking changes from version 5.2.x to 5.3.x and how remo [[elasticsearch-migration-guide-5.2-5.3.deprecations]] == Deprecations + +=== Removals +The deprecated classes `org.springframework.data.elasticsearch.ELCQueries` + and `org.springframework.data.elasticsearch.client.elc.QueryBuilders` have been removed, use `org.springframework.data.elasticsearch.client.elc.Queries` instead. diff --git a/src/main/java/org/springframework/data/elasticsearch/client/elc/QueryBuilders.java b/src/main/java/org/springframework/data/elasticsearch/client/elc/QueryBuilders.java deleted file mode 100644 index 66e58c6c4..000000000 --- a/src/main/java/org/springframework/data/elasticsearch/client/elc/QueryBuilders.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright 2022-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 - * - * https://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 org.springframework.data.elasticsearch.client.elc; - -import co.elastic.clients.elasticsearch._types.FieldValue; -import co.elastic.clients.elasticsearch._types.LatLonGeoLocation; -import co.elastic.clients.elasticsearch._types.query_dsl.IdsQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.Operator; -import co.elastic.clients.elasticsearch._types.query_dsl.Query; -import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.WildcardQuery; -import co.elastic.clients.elasticsearch._types.query_dsl.WrapperQuery; -import co.elastic.clients.util.ObjectBuilder; - -import java.nio.charset.StandardCharsets; -import java.util.Base64; -import java.util.List; -import java.util.function.Function; - -import org.springframework.data.elasticsearch.core.geo.GeoPoint; -import org.springframework.lang.Nullable; -import org.springframework.util.Assert; - -/** - * Utility class simplifying the creation of some more complex queries and type. - * - * @author Peter-Josef Meisch - * @since 4.4 - * @deprecated since 5.1, use {@link Queries} instead. - */ -@Deprecated(forRemoval = true) -public final class QueryBuilders { - - private QueryBuilders() {} - - public static IdsQuery idsQuery(List ids) { - - Assert.notNull(ids, "ids must not be null"); - - return IdsQuery.of(i -> i.values(ids)); - } - - public static Query idsQueryAsQuery(List ids) { - - Assert.notNull(ids, "ids must not be null"); - - Function> builder = b -> b.ids(idsQuery(ids)); - - return builder.apply(new Query.Builder()).build(); - } - - public static MatchQuery matchQuery(String fieldName, String query, @Nullable Operator operator, - @Nullable Float boost) { - - Assert.notNull(fieldName, "fieldName must not be null"); - Assert.notNull(query, "query must not be null"); - - return MatchQuery.of(mb -> mb.field(fieldName).query(FieldValue.of(query)).operator(operator).boost(boost)); - } - - public static Query matchQueryAsQuery(String fieldName, String query, @Nullable Operator operator, - @Nullable Float boost) { - - Function> builder = b -> b.match(matchQuery(fieldName, query, operator, boost)); - - return builder.apply(new Query.Builder()).build(); - } - - public static MatchAllQuery matchAllQuery() { - - return MatchAllQuery.of(b -> b); - } - - public static Query matchAllQueryAsQuery() { - - Function> builder = b -> b.matchAll(matchAllQuery()); - - return builder.apply(new Query.Builder()).build(); - } - - public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Float boost) { - return queryStringQuery(fieldName, query, null, null, boost); - } - - public static QueryStringQuery queryStringQuery(String fieldName, String query, Operator defaultOperator, - @Nullable Float boost) { - return queryStringQuery(fieldName, query, null, defaultOperator, boost); - } - - public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Boolean analyzeWildcard, - @Nullable Float boost) { - return queryStringQuery(fieldName, query, analyzeWildcard, null, boost); - } - - public static QueryStringQuery queryStringQuery(String fieldName, String query, @Nullable Boolean analyzeWildcard, - @Nullable Operator defaultOperator, @Nullable Float boost) { - - Assert.notNull(fieldName, "fieldName must not be null"); - Assert.notNull(query, "query must not be null"); - - return QueryStringQuery.of(qs -> qs.fields(fieldName).query(query).analyzeWildcard(analyzeWildcard) - .defaultOperator(defaultOperator).boost(boost)); - } - - public static TermQuery termQuery(String fieldName, String value) { - - Assert.notNull(fieldName, "fieldName must not be null"); - Assert.notNull(value, "value must not be null"); - - return TermQuery.of(t -> t.field(fieldName).value(FieldValue.of(value))); - } - - public static Query termQueryAsQuery(String fieldName, String value) { - - Function> builder = q -> q.term(termQuery(fieldName, value)); - return builder.apply(new Query.Builder()).build(); - } - - public static WildcardQuery wildcardQuery(String field, String value) { - - Assert.notNull(field, "field must not be null"); - Assert.notNull(value, "value must not be null"); - - return WildcardQuery.of(w -> w.field(field).wildcard(value)); - } - - public static Query wildcardQueryAsQuery(String field, String value) { - Function> builder = q -> q.wildcard(wildcardQuery(field, value)); - return builder.apply(new Query.Builder()).build(); - } - - public static Query wrapperQueryAsQuery(String query) { - - Function> builder = q -> q.wrapper(wrapperQuery(query)); - - return builder.apply(new Query.Builder()).build(); - } - - public static WrapperQuery wrapperQuery(String query) { - - Assert.notNull(query, "query must not be null"); - - String encodedValue = Base64.getEncoder().encodeToString(query.getBytes(StandardCharsets.UTF_8)); - - return WrapperQuery.of(wq -> wq.query(encodedValue)); - } - - public static LatLonGeoLocation latLon(GeoPoint geoPoint) { - - Assert.notNull(geoPoint, "geoPoint must not be null"); - - return latLon(geoPoint.getLat(), geoPoint.getLon()); - } - - public static LatLonGeoLocation latLon(double lat, double lon) { - return LatLonGeoLocation.of(_0 -> _0.lat(lat).lon(lon)); - } -} diff --git a/src/test/java/org/springframework/data/elasticsearch/ClientArchitectureTests.java b/src/test/java/org/springframework/data/elasticsearch/ClientArchitectureTests.java new file mode 100644 index 000000000..d71707572 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/ClientArchitectureTests.java @@ -0,0 +1,23 @@ +package org.springframework.data.elasticsearch; + +import com.tngtech.archunit.core.importer.ImportOption; +import com.tngtech.archunit.junit.AnalyzeClasses; +import com.tngtech.archunit.junit.ArchTest; +import com.tngtech.archunit.lang.ArchRule; +import com.tngtech.archunit.lang.syntax.ArchRuleDefinition; + +@AnalyzeClasses( + packages = { "org.springframework.data.elasticsearch", "co.elastic" }, + importOptions = ImportOption.DoNotIncludeTests.class) +class ClientArchitectureTests { + + @ArchTest public static final ArchRule elasticLibrariesShouldOnlyBeUsedInClientElc = ArchRuleDefinition + .noClasses() + .that() + .resideInAPackage("org.springframework.data.elasticsearch..") + .and() + .resideOutsideOfPackage("org.springframework.data.elasticsearch.client.elc..") + .should() + .dependOnClassesThat() + .resideInAnyPackage("co.elastic.clients..", "org.elasticsearch.client.."); +} diff --git a/src/test/java/org/springframework/data/elasticsearch/ELCQueries.java b/src/test/java/org/springframework/data/elasticsearch/ELCQueries.java deleted file mode 100644 index aa776ccc3..000000000 --- a/src/test/java/org/springframework/data/elasticsearch/ELCQueries.java +++ /dev/null @@ -1,59 +0,0 @@ -/* -// * Copyright 2022-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 - * - * https://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 org.springframework.data.elasticsearch; - -import static org.springframework.data.elasticsearch.client.elc.Queries.*; - -import co.elastic.clients.elasticsearch._types.aggregations.Aggregation; - -import org.springframework.data.elasticsearch.client.elc.NativeQuery; -import org.springframework.data.elasticsearch.client.elc.Queries; -import org.springframework.data.elasticsearch.core.query.BaseQueryBuilder; -import org.springframework.data.elasticsearch.core.query.Query; - -/** - * Class providing some queries for the new Elasticsearch client needed in different tests. - * - * @author Peter-Josef Meisch - * @since 4.4 - * @deprecated since 5.1, use the corresponding methods from {@link Queries}. - */ -@Deprecated(forRemoval = true) -public final class ELCQueries { - - private ELCQueries() {} - - public static Query getTermsAggsQuery(String aggsName, String aggsField) { - return NativeQuery.builder() // - .withQuery(Queries.matchAllQueryAsQuery()) // - .withAggregation(aggsName, Aggregation.of(a -> a // - .terms(ta -> ta.field(aggsField)))) // - .withMaxResults(0) // - .build(); - } - - public static Query queryWithIds(String... ids) { - return NativeQuery.builder().withIds(ids).build(); - } - - public static BaseQueryBuilder getBuilderWithMatchAllQuery() { - return NativeQuery.builder().withQuery(matchAllQueryAsQuery()); - } - - public static BaseQueryBuilder getBuilderWithTermQuery(String field, String value) { - return NativeQuery.builder().withQuery(termQueryAsQuery(field, value)); - } -}