From 8394f541bc50c1f216b298e0680398a62a3fb9c9 Mon Sep 17 00:00:00 2001 From: Owais Kazi Date: Mon, 25 Oct 2021 10:24:11 -0700 Subject: [PATCH] Run spotless and exclude checkstyle on libs module (#1428) Signed-off-by: Owais Kazi --- .../resources/checkstyle_suppressions.xml | 2 + gradle/formatting.gradle | 10 - .../main/java/org/opensearch/cli/Command.java | 8 +- .../java/org/opensearch/cli/ExitCodes.java | 26 +- .../org/opensearch/cli/SuppressForbidden.java | 1 - .../java/org/opensearch/cli/Terminal.java | 2 +- .../org/opensearch/bootstrap/JarHell.java | 85 ++- .../org/opensearch/bootstrap/JavaVersion.java | 12 +- .../java/org/opensearch/common/Booleans.java | 19 +- .../main/java/org/opensearch/common/Glob.java | 6 +- .../java/org/opensearch/common/Nullable.java | 2 +- .../opensearch/common/SuppressForbidden.java | 2 +- .../org/opensearch/common/collect/List.java | 2 +- .../org/opensearch/common/collect/Map.java | 74 ++- .../org/opensearch/common/collect/Set.java | 2 +- .../org/opensearch/common/io/PathUtils.java | 2 +- .../org/opensearch/common/unit/TimeValue.java | 17 +- .../org/opensearch/common/util/FastMath.java | 76 +-- .../util/concurrent/AbstractRefCounted.java | 1 - .../opensearch/core/internal/io/IOUtils.java | 23 +- .../core/internal/net/NetUtils.java | 3 +- .../org/opensearch/common/collect/Map.java | 72 ++- .../opensearch/bootstrap/JarHellTests.java | 36 +- .../opensearch/common/collect/ListTests.java | 6 +- .../opensearch/common/collect/MapTests.java | 105 +++- .../opensearch/common/collect/SetTests.java | 6 +- .../common/unit/TimeValueTests.java | 100 ++- .../util/OpenSearchSloppyMathTests.java | 4 +- .../core/internal/io/IOUtilsTests.java | 19 +- .../org/opensearch/dissect/DissectKey.java | 37 +- .../org/opensearch/dissect/DissectMatch.java | 6 +- .../org/opensearch/dissect/DissectParser.java | 81 +-- .../opensearch/dissect/DissectKeyTests.java | 13 +- .../opensearch/dissect/DissectMatchTests.java | 20 +- .../dissect/DissectParserTests.java | 208 ++++-- .../java/org/opensearch/geometry/Circle.java | 2 +- .../java/org/opensearch/geometry/Line.java | 3 +- .../org/opensearch/geometry/LinearRing.java | 32 +- .../org/opensearch/geometry/MultiLine.java | 3 +- .../org/opensearch/geometry/MultiPoint.java | 3 +- .../org/opensearch/geometry/MultiPolygon.java | 3 +- .../java/org/opensearch/geometry/Polygon.java | 3 +- .../org/opensearch/geometry/Rectangle.java | 3 +- .../org/opensearch/geometry/package-info.java | 1 - .../opensearch/geometry/utils/BitUtil.java | 18 +- .../geometry/utils/GeographyValidator.java | 11 +- .../opensearch/geometry/utils/Geohash.java | 98 +-- .../geometry/utils/StandardValidator.java | 8 +- .../geometry/utils/WellKnownText.java | 12 +- .../geometry/BaseGeometryTestCase.java | 3 +- .../org/opensearch/geometry/CircleTests.java | 10 +- .../geometry/GeometryCollectionTests.java | 24 +- .../geometry/GeometryValidatorTests.java | 16 +- .../org/opensearch/geometry/LineTests.java | 48 +- .../opensearch/geometry/LinearRingTests.java | 65 +- .../opensearch/geometry/MultiLineTests.java | 23 +- .../opensearch/geometry/MultiPointTests.java | 27 +- .../geometry/MultiPolygonTests.java | 42 +- .../org/opensearch/geometry/PointTests.java | 6 +- .../org/opensearch/geometry/PolygonTests.java | 111 ++-- .../opensearch/geometry/RectangleTests.java | 18 +- .../geometry/utils/GeoHashTests.java | 15 +- .../main/java/org/opensearch/grok/Grok.java | 104 ++- .../org/opensearch/grok/MatcherWatchdog.java | 24 +- .../java/org/opensearch/grok/GrokTests.java | 207 +++--- .../opensearch/grok/MatcherWatchdogTests.java | 19 +- .../opensearch/nio/BytesChannelContext.java | 10 +- .../org/opensearch/nio/BytesWriteHandler.java | 7 +- .../org/opensearch/nio/ChannelFactory.java | 53 +- .../main/java/org/opensearch/nio/Config.java | 15 +- .../org/opensearch/nio/FlushOperation.java | 16 +- .../opensearch/nio/InboundChannelBuffer.java | 28 +- .../java/org/opensearch/nio/NioSelector.java | 17 +- .../org/opensearch/nio/NioSelectorGroup.java | 17 +- .../nio/NioServerSocketChannel.java | 4 +- .../org/opensearch/nio/NioSocketChannel.java | 5 +- .../org/opensearch/nio/SelectionKeyUtils.java | 1 - .../opensearch/nio/ServerChannelContext.java | 14 +- .../opensearch/nio/SocketChannelContext.java | 11 +- .../nio/BytesChannelContextTests.java | 13 +- .../opensearch/nio/ChannelFactoryTests.java | 7 +- .../org/opensearch/nio/EventHandlerTests.java | 25 +- .../opensearch/nio/FlushOperationTests.java | 6 +- .../opensearch/nio/NioSelectorGroupTests.java | 29 +- .../org/opensearch/nio/NioSelectorTests.java | 16 +- .../nio/SocketChannelContextTests.java | 112 +++- .../plugins/ExtendedPluginsClassLoader.java | 5 +- .../org/opensearch/secure_sm/SecureSM.java | 37 +- .../opensearch/secure_sm/SecureSMTests.java | 4 +- .../secure_sm/ThreadPermissionTests.java | 3 +- .../org/opensearch/common/ssl/DerParser.java | 33 +- .../common/ssl/DiagnosticTrustManager.java | 21 +- .../opensearch/common/ssl/KeyStoreUtil.java | 30 +- .../opensearch/common/ssl/PemKeyConfig.java | 8 +- .../opensearch/common/ssl/PemTrustConfig.java | 17 +- .../org/opensearch/common/ssl/PemUtils.java | 37 +- .../ssl/SslClientAuthenticationMode.java | 5 +- .../common/ssl/SslConfiguration.java | 50 +- .../common/ssl/SslConfigurationKeys.java | 18 +- .../common/ssl/SslConfigurationLoader.java | 77 ++- .../opensearch/common/ssl/SslDiagnostics.java | 54 +- .../opensearch/common/ssl/SslKeyConfig.java | 1 - .../opensearch/common/ssl/SslTrustConfig.java | 1 - .../common/ssl/SslVerificationMode.java | 9 +- .../opensearch/common/ssl/StoreKeyConfig.java | 3 +- .../common/ssl/StoreTrustConfig.java | 12 +- .../common/ssl/TrustEverythingConfig.java | 18 +- .../common/ssl/PemKeyConfigTests.java | 8 +- .../common/ssl/PemTrustConfigTests.java | 2 +- .../opensearch/common/ssl/PemUtilsTests.java | 15 +- .../ssl/SslConfigurationLoaderTests.java | 25 +- .../common/ssl/SslConfigurationTests.java | 96 ++- .../common/ssl/SslDiagnosticsTests.java | 593 ++++++++++++------ .../common/ssl/StoreKeyConfigTests.java | 26 +- .../common/xcontent/AbstractObjectParser.java | 114 +++- .../xcontent/ConstructingObjectParser.java | 79 ++- .../common/xcontent/DeprecationHandler.java | 53 +- .../xcontent/InstantiatingObjectParser.java | 66 +- .../xcontent/NamedXContentRegistry.java | 23 +- .../common/xcontent/ObjectParser.java | 147 +++-- .../common/xcontent/ObjectPath.java | 3 +- .../opensearch/common/xcontent/XContent.java | 25 +- .../common/xcontent/XContentBuilder.java | 33 +- .../common/xcontent/XContentFactory.java | 12 +- .../common/xcontent/XContentParser.java | 11 +- .../common/xcontent/XContentSubParser.java | 4 +- .../common/xcontent/XContentType.java | 9 +- .../common/xcontent/XContentUtils.java | 3 +- .../common/xcontent/cbor/CborXContent.java | 28 +- .../xcontent/cbor/CborXContentParser.java | 3 +- .../common/xcontent/json/JsonXContent.java | 29 +- .../xcontent/json/JsonXContentGenerator.java | 24 +- .../xcontent/json/JsonXContentParser.java | 3 +- .../common/xcontent/smile/SmileXContent.java | 28 +- .../xcontent/smile/SmileXContentParser.java | 3 +- .../support/AbstractXContentParser.java | 57 +- .../xcontent/support/MapXContentParser.java | 9 +- .../support/filtering/FilterPath.java | 3 +- .../filtering/FilterPathBasedFilter.java | 1 - .../common/xcontent/yaml/YamlXContent.java | 28 +- .../xcontent/yaml/YamlXContentParser.java | 3 +- .../opensearch/common/ParseFieldTests.java | 7 +- .../ConstructingObjectParserTests.java | 246 +++++--- .../InstantiatingObjectParserTests.java | 18 +- .../xcontent/MapXContentParserTests.java | 17 +- .../common/xcontent/ObjectParserTests.java | 241 ++++--- .../common/xcontent/SimpleStruct.java | 12 +- .../common/xcontent/XContentParserTests.java | 160 ++--- 148 files changed, 3306 insertions(+), 1860 deletions(-) diff --git a/buildSrc/src/main/resources/checkstyle_suppressions.xml b/buildSrc/src/main/resources/checkstyle_suppressions.xml index 879181365d5..b9d3891c751 100644 --- a/buildSrc/src/main/resources/checkstyle_suppressions.xml +++ b/buildSrc/src/main/resources/checkstyle_suppressions.xml @@ -23,6 +23,8 @@ + + we only have to compute atan(x) on [0,+infinity[. @@ -75,9 +75,9 @@ final class FastMath { // Supposed to be >= tan(67.7deg), as fdlibm code is supposed to work with values > 2.4375. private static final double ATAN_MAX_VALUE_FOR_TABS = StrictMath.tan(Math.toRadians(74.0)); - private static final int ATAN_TABS_SIZE = 1<<12 + 1; - private static final double ATAN_DELTA = ATAN_MAX_VALUE_FOR_TABS/(ATAN_TABS_SIZE - 1); - private static final double ATAN_INDEXER = 1/ATAN_DELTA; + private static final int ATAN_TABS_SIZE = 1 << 12 + 1; + private static final double ATAN_DELTA = ATAN_MAX_VALUE_FOR_TABS / (ATAN_TABS_SIZE - 1); + private static final double ATAN_INDEXER = 1 / ATAN_DELTA; private static final double[] atanTab = new double[ATAN_TABS_SIZE]; private static final double[] atanDer1DivF1Tab = new double[ATAN_TABS_SIZE]; private static final double[] atanDer2DivF2Tab = new double[ATAN_TABS_SIZE]; @@ -86,32 +86,32 @@ final class FastMath { private static final double ATAN_HI3 = Double.longBitsToDouble(0x3ff921fb54442d18L); // 1.57079632679489655800e+00 atan(inf)hi private static final double ATAN_LO3 = Double.longBitsToDouble(0x3c91a62633145c07L); // 6.12323399573676603587e-17 atan(inf)lo - private static final double ATAN_AT0 = Double.longBitsToDouble(0x3fd555555555550dL); // 3.33333333333329318027e-01 + private static final double ATAN_AT0 = Double.longBitsToDouble(0x3fd555555555550dL); // 3.33333333333329318027e-01 private static final double ATAN_AT1 = Double.longBitsToDouble(0xbfc999999998ebc4L); // -1.99999999998764832476e-01 - private static final double ATAN_AT2 = Double.longBitsToDouble(0x3fc24924920083ffL); // 1.42857142725034663711e-01 + private static final double ATAN_AT2 = Double.longBitsToDouble(0x3fc24924920083ffL); // 1.42857142725034663711e-01 private static final double ATAN_AT3 = Double.longBitsToDouble(0xbfbc71c6fe231671L); // -1.11111104054623557880e-01 - private static final double ATAN_AT4 = Double.longBitsToDouble(0x3fb745cdc54c206eL); // 9.09088713343650656196e-02 + private static final double ATAN_AT4 = Double.longBitsToDouble(0x3fb745cdc54c206eL); // 9.09088713343650656196e-02 private static final double ATAN_AT5 = Double.longBitsToDouble(0xbfb3b0f2af749a6dL); // -7.69187620504482999495e-02 - private static final double ATAN_AT6 = Double.longBitsToDouble(0x3fb10d66a0d03d51L); // 6.66107313738753120669e-02 + private static final double ATAN_AT6 = Double.longBitsToDouble(0x3fb10d66a0d03d51L); // 6.66107313738753120669e-02 private static final double ATAN_AT7 = Double.longBitsToDouble(0xbfadde2d52defd9aL); // -5.83357013379057348645e-02 - private static final double ATAN_AT8 = Double.longBitsToDouble(0x3fa97b4b24760debL); // 4.97687799461593236017e-02 + private static final double ATAN_AT8 = Double.longBitsToDouble(0x3fa97b4b24760debL); // 4.97687799461593236017e-02 private static final double ATAN_AT9 = Double.longBitsToDouble(0xbfa2b4442c6a6c2fL); // -3.65315727442169155270e-02 private static final double ATAN_AT10 = Double.longBitsToDouble(0x3f90ad3ae322da11L); // 1.62858201153657823623e-02 static { // atan - for (int i=0;i ATAN_MAX_VALUE_FOR_TABS, or value is NaN // This part is derived from fdlibm. if (value < TWO_POW_66) { - double x = -1/value; - double x2 = x*x; - double x4 = x2*x2; - double s1 = x2*(ATAN_AT0+x4*(ATAN_AT2+x4*(ATAN_AT4+x4*(ATAN_AT6+x4*(ATAN_AT8+x4*ATAN_AT10))))); - double s2 = x4*(ATAN_AT1+x4*(ATAN_AT3+x4*(ATAN_AT5+x4*(ATAN_AT7+x4*ATAN_AT9)))); - double result = ATAN_HI3-((x*(s1+s2)-ATAN_LO3)-x); + double x = -1 / value; + double x2 = x * x; + double x4 = x2 * x2; + double s1 = x2 * (ATAN_AT0 + x4 * (ATAN_AT2 + x4 * (ATAN_AT4 + x4 * (ATAN_AT6 + x4 * (ATAN_AT8 + x4 * ATAN_AT10))))); + double s2 = x4 * (ATAN_AT1 + x4 * (ATAN_AT3 + x4 * (ATAN_AT5 + x4 * (ATAN_AT7 + x4 * ATAN_AT9)))); + double result = ATAN_HI3 - ((x * (s1 + s2) - ATAN_LO3) - x); return negateResult ? -result : result; } else { // value >= 2^66, or value is NaN if (Double.isNaN(value)) { return Double.NaN; } else { - return negateResult ? -Math.PI/2 : Math.PI/2; + return negateResult ? -Math.PI / 2 : Math.PI / 2; } } } diff --git a/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java b/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java index ac2865a3595..e55c87c34c7 100644 --- a/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java +++ b/libs/core/src/main/java/org/opensearch/common/util/concurrent/AbstractRefCounted.java @@ -90,7 +90,6 @@ public abstract class AbstractRefCounted implements RefCounted { return this.refCount.get(); } - /** gets the name of this instance */ public String getName() { return name; diff --git a/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java b/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java index 6bf8eae6482..00ecb8b82d7 100644 --- a/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java +++ b/libs/core/src/main/java/org/opensearch/core/internal/io/IOUtils.java @@ -164,7 +164,7 @@ public final class IOUtils { */ public static void closeWhileHandlingException(final Iterable objects) { for (final Closeable object : objects) { - closeWhileHandlingException(object); + closeWhileHandlingException(object); } } @@ -175,8 +175,7 @@ public final class IOUtils { // noinspection EmptyCatchBlock try { close(closeable); - } catch (final IOException | RuntimeException e) { - } + } catch (final IOException | RuntimeException e) {} } /** @@ -212,21 +211,17 @@ public final class IOUtils { * @throws IOException if any of the given files (or their sub-hierarchy files in case of directories) cannot be removed. */ public static void rm(final Path... locations) throws IOException { - final LinkedHashMap unremoved = rm(new LinkedHashMap<>(), locations); + final LinkedHashMap unremoved = rm(new LinkedHashMap<>(), locations); if (!unremoved.isEmpty()) { final StringBuilder b = new StringBuilder("could not remove the following files (in the order of attempts):\n"); - for (final Map.Entry kv : unremoved.entrySet()) { - b.append(" ") - .append(kv.getKey().toAbsolutePath()) - .append(": ") - .append(kv.getValue()) - .append("\n"); + for (final Map.Entry kv : unremoved.entrySet()) { + b.append(" ").append(kv.getKey().toAbsolutePath()).append(": ").append(kv.getValue()).append("\n"); } throw new IOException(b.toString()); } } - private static LinkedHashMap rm(final LinkedHashMap unremoved, final Path... locations) { + private static LinkedHashMap rm(final LinkedHashMap unremoved, final Path... locations) { if (locations != null) { for (final Path location : locations) { // TODO: remove this leniency @@ -306,9 +301,9 @@ public final class IOUtils { file.force(true); } catch (final IOException e) { if (isDir) { - assert (LINUX || MAC_OS_X) == false : - "on Linux and MacOSX fsyncing a directory should not throw IOException, "+ - "we just don't want to rely on that in production (undocumented); got: " + e; + assert (LINUX || MAC_OS_X) == false : "on Linux and MacOSX fsyncing a directory should not throw IOException, " + + "we just don't want to rely on that in production (undocumented); got: " + + e; // ignore exception if it is a directory return; } diff --git a/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java b/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java index e101895b177..6ea3b6f953c 100644 --- a/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java +++ b/libs/core/src/main/java/org/opensearch/core/internal/net/NetUtils.java @@ -92,7 +92,8 @@ public class NetUtils { if (keepalive.booleanValue()) { for (SocketOption option : Arrays.asList( NetUtils.getTcpKeepIdleSocketOptionOrNull(), - NetUtils.getTcpKeepIntervalSocketOptionOrNull())) { + NetUtils.getTcpKeepIntervalSocketOptionOrNull() + )) { setMinValueForSocketOption(socketChannel, option, 300); } } diff --git a/libs/core/src/main/java11/org/opensearch/common/collect/Map.java b/libs/core/src/main/java11/org/opensearch/common/collect/Map.java index 45ba475de22..21de5468693 100644 --- a/libs/core/src/main/java11/org/opensearch/common/collect/Map.java +++ b/libs/core/src/main/java11/org/opensearch/common/collect/Map.java @@ -79,40 +79,92 @@ public class Map { /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6) { + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7) { + public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8 + ) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9 + ) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9); } /** * Delegates to the Java9 {@code Map.of()} method. */ - public static java.util.Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, - K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10) { + public static java.util.Map of( + K k1, + V v1, + K k2, + V v2, + K k3, + V v3, + K k4, + V v4, + K k5, + V v5, + K k6, + V v6, + K k7, + V v7, + K k8, + V v8, + K k9, + V v9, + K k10, + V v10 + ) { return java.util.Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5, k6, v6, k7, v7, k8, v8, k9, v9, k10, v10); } diff --git a/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java b/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java index 446d8df47da..d0e411ae8e3 100644 --- a/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java +++ b/libs/core/src/test/java/org/opensearch/bootstrap/JarHellTests.java @@ -76,8 +76,7 @@ public class JarHellTests extends OpenSearchTestCase { public void testDifferentJars() throws Exception { Path dir = createTempDir(); - Set jars = asSet(makeJar(dir, "foo.jar", null, "DuplicateClass.class"), - makeJar(dir, "bar.jar", null, "DuplicateClass.class")); + Set jars = asSet(makeJar(dir, "foo.jar", null, "DuplicateClass.class"), makeJar(dir, "bar.jar", null, "DuplicateClass.class")); try { JarHell.checkJarHell(jars, logger::debug); fail("did not get expected exception"); @@ -92,10 +91,7 @@ public class JarHellTests extends OpenSearchTestCase { public void testModuleInfo() throws Exception { Path dir = createTempDir(); JarHell.checkJarHell( - asSet( - makeJar(dir, "foo.jar", null, "module-info.class"), - makeJar(dir, "bar.jar", null, "module-info.class") - ), + asSet(makeJar(dir, "foo.jar", null, "module-info.class"), makeJar(dir, "bar.jar", null, "module-info.class")), logger::debug ); } @@ -103,10 +99,7 @@ public class JarHellTests extends OpenSearchTestCase { public void testModuleInfoPackage() throws Exception { Path dir = createTempDir(); JarHell.checkJarHell( - asSet( - makeJar(dir, "foo.jar", null, "foo/bar/module-info.class"), - makeJar(dir, "bar.jar", null, "foo/bar/module-info.class") - ), + asSet(makeJar(dir, "foo.jar", null, "foo/bar/module-info.class"), makeJar(dir, "bar.jar", null, "foo/bar/module-info.class")), logger::debug ); } @@ -114,8 +107,7 @@ public class JarHellTests extends OpenSearchTestCase { public void testDirsOnClasspath() throws Exception { Path dir1 = createTempDir(); Path dir2 = createTempDir(); - Set dirs = asSet(makeFile(dir1, "DuplicateClass.class"), - makeFile(dir2, "DuplicateClass.class")); + Set dirs = asSet(makeFile(dir1, "DuplicateClass.class"), makeFile(dir2, "DuplicateClass.class")); try { JarHell.checkJarHell(dirs, logger::debug); fail("did not get expected exception"); @@ -130,8 +122,7 @@ public class JarHellTests extends OpenSearchTestCase { public void testDirAndJar() throws Exception { Path dir1 = createTempDir(); Path dir2 = createTempDir(); - Set dirs = asSet(makeJar(dir1, "foo.jar", null, "DuplicateClass.class"), - makeFile(dir2, "DuplicateClass.class")); + Set dirs = asSet(makeJar(dir1, "foo.jar", null, "DuplicateClass.class"), makeFile(dir2, "DuplicateClass.class")); try { JarHell.checkJarHell(dirs, logger::debug); fail("did not get expected exception"); @@ -172,7 +163,6 @@ public class JarHellTests extends OpenSearchTestCase { } JavaVersion targetVersion = JavaVersion.parse(Strings.collectionToDelimitedString(target, ".")); - Manifest manifest = new Manifest(); Attributes attributes = manifest.getMainAttributes(); attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0"); @@ -198,8 +188,13 @@ public class JarHellTests extends OpenSearchTestCase { JarHell.checkJarHell(jars, logger::debug); fail("did not get expected exception"); } catch (IllegalStateException e) { - assertTrue(e.getMessage().equals("version string must be a sequence of nonnegative decimal integers separated " + - "by \".\"'s and may have leading zeros but was bogus")); + assertTrue( + e.getMessage() + .equals( + "version string must be a sequence of nonnegative decimal integers separated " + + "by \".\"'s and may have leading zeros but was bogus" + ) + ); } } @@ -214,7 +209,7 @@ public class JarHellTests extends OpenSearchTestCase { } public void testValidVersions() { - String[] versions = new String[]{"1.7", "1.7.0", "0.1.7", "1.7.0.80"}; + String[] versions = new String[] { "1.7", "1.7.0", "0.1.7", "1.7.0.80" }; for (String version : versions) { try { JarHell.checkVersionFormat(version); @@ -225,13 +220,12 @@ public class JarHellTests extends OpenSearchTestCase { } public void testInvalidVersions() { - String[] versions = new String[]{"", "1.7.0_80", "1.7."}; + String[] versions = new String[] { "", "1.7.0_80", "1.7." }; for (String version : versions) { try { JarHell.checkVersionFormat(version); fail("\"" + version + "\"" + " should be rejected as an invalid version format"); - } catch (IllegalStateException e) { - } + } catch (IllegalStateException e) {} } } diff --git a/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java b/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java index 68d99e86b49..70841a102b7 100644 --- a/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java +++ b/libs/core/src/test/java/org/opensearch/common/collect/ListTests.java @@ -50,7 +50,7 @@ public class ListTests extends OpenSearchTestCase { } public void testStringListOfOne() { - final String[] strings = {"foo"}; + final String[] strings = { "foo" }; final java.util.List stringsList = List.of(strings); assertThat(stringsList.size(), equalTo(strings.length)); assertTrue(stringsList.containsAll(Arrays.asList(strings))); @@ -58,7 +58,7 @@ public class ListTests extends OpenSearchTestCase { } public void testStringListOfTwo() { - final String[] strings = {"foo", "bar"}; + final String[] strings = { "foo", "bar" }; final java.util.List stringsList = List.of(strings); assertThat(stringsList.size(), equalTo(strings.length)); assertTrue(stringsList.containsAll(Arrays.asList(strings))); @@ -66,7 +66,7 @@ public class ListTests extends OpenSearchTestCase { } public void testStringListOfN() { - final String[] strings = {"foo", "bar", "baz"}; + final String[] strings = { "foo", "bar", "baz" }; final java.util.List stringsList = List.of(strings); assertThat(stringsList.size(), equalTo(strings.length)); assertTrue(stringsList.containsAll(Arrays.asList(strings))); diff --git a/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java b/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java index 72969fc0e20..8d7ffa71df5 100644 --- a/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java +++ b/libs/core/src/test/java/org/opensearch/common/collect/MapTests.java @@ -38,7 +38,7 @@ import static org.hamcrest.CoreMatchers.equalTo; public class MapTests extends OpenSearchTestCase { - private static final String[] numbers = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; + private static final String[] numbers = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; public void testMapOfZero() { final java.util.Map map = Map.of(); @@ -66,38 +66,117 @@ public class MapTests extends OpenSearchTestCase { } public void testMapOfFive() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4); + final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, numbers[4], 4); validateMapContents(map, 5); } public void testMapOfSix() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5 + ); validateMapContents(map, 6); } public void testMapOfSeven() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6 + ); validateMapContents(map, 7); } public void testMapOfEight() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6, + numbers[7], + 7 + ); validateMapContents(map, 8); } public void testMapOfNine() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7, numbers[8], 8); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6, + numbers[7], + 7, + numbers[8], + 8 + ); validateMapContents(map, 9); } public void testMapOfTen() { - final java.util.Map map = Map.of(numbers[0], 0, numbers[1], 1, numbers[2], 2, numbers[3], 3, - numbers[4], 4, numbers[5], 5, numbers[6], 6, numbers[7], 7, numbers[8], 8, numbers[9], 9); + final java.util.Map map = Map.of( + numbers[0], + 0, + numbers[1], + 1, + numbers[2], + 2, + numbers[3], + 3, + numbers[4], + 4, + numbers[5], + 5, + numbers[6], + 6, + numbers[7], + 7, + numbers[8], + 8, + numbers[9], + 9 + ); validateMapContents(map, 10); } diff --git a/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java b/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java index b0c7ae06899..1b7f29ff0d6 100644 --- a/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java +++ b/libs/core/src/test/java/org/opensearch/common/collect/SetTests.java @@ -50,7 +50,7 @@ public class SetTests extends OpenSearchTestCase { } public void testStringSetOfOne() { - final String[] strings = {"foo"}; + final String[] strings = { "foo" }; final java.util.Set stringsSet = Set.of(strings); assertThat(stringsSet.size(), equalTo(strings.length)); assertTrue(stringsSet.containsAll(Arrays.asList(strings))); @@ -58,7 +58,7 @@ public class SetTests extends OpenSearchTestCase { } public void testStringSetOfTwo() { - final String[] strings = {"foo", "bar"}; + final String[] strings = { "foo", "bar" }; final java.util.Set stringsSet = Set.of(strings); assertThat(stringsSet.size(), equalTo(strings.length)); assertTrue(stringsSet.containsAll(Arrays.asList(strings))); @@ -66,7 +66,7 @@ public class SetTests extends OpenSearchTestCase { } public void testStringSetOfN() { - final String[] strings = {"foo", "bar", "baz"}; + final String[] strings = { "foo", "bar", "baz" }; final java.util.Set stringsSet = Set.of(strings); assertThat(stringsSet.size(), equalTo(strings.length)); assertTrue(stringsSet.containsAll(Arrays.asList(strings))); diff --git a/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java b/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java index 065cd784da5..250f1de1fee 100644 --- a/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java +++ b/libs/core/src/test/java/org/opensearch/common/unit/TimeValueTests.java @@ -89,57 +89,35 @@ public class TimeValueTests extends OpenSearchTestCase { public void testParseTimeValue() { // Space is allowed before unit: - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10 ms", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10ms", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10 MS", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), - TimeValue.parseTimeValue("10MS", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10 ms", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10ms", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10 MS", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MILLISECONDS), TimeValue.parseTimeValue("10MS", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10 s", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10s", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10 S", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.SECONDS), - TimeValue.parseTimeValue("10S", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10 s", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10s", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10 S", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.SECONDS), TimeValue.parseTimeValue("10S", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10 m", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.MINUTES), - TimeValue.parseTimeValue("10m", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10 m", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.MINUTES), TimeValue.parseTimeValue("10m", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10 h", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10h", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10 H", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.HOURS), - TimeValue.parseTimeValue("10H", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 h", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10h", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10 H", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.HOURS), TimeValue.parseTimeValue("10H", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10 d", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10d", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10 D", null, "test")); - assertEquals(new TimeValue(10, TimeUnit.DAYS), - TimeValue.parseTimeValue("10D", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10 d", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10d", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10 D", null, "test")); + assertEquals(new TimeValue(10, TimeUnit.DAYS), TimeValue.parseTimeValue("10D", null, "test")); // Time values of months should throw an exception as months are not // supported. Note that this is the only unit that is not case sensitive // as `m` is the only character that is overloaded in terms of which // time unit is expected between the upper and lower case versions - expectThrows(IllegalArgumentException.class, () -> { - TimeValue.parseTimeValue("10 M", null, "test"); - }); - expectThrows(IllegalArgumentException.class, () -> { - TimeValue.parseTimeValue("10M", null, "test"); - }); + expectThrows(IllegalArgumentException.class, () -> { TimeValue.parseTimeValue("10 M", null, "test"); }); + expectThrows(IllegalArgumentException.class, () -> { TimeValue.parseTimeValue("10M", null, "test"); }); final int length = randomIntBetween(0, 8); final String zeros = new String(new char[length]).replace('\0', '0'); @@ -158,8 +136,7 @@ public class TimeValueTests extends OpenSearchTestCase { public void testNonFractionalTimeValues() { final String s = randomAlphaOfLength(10) + randomTimeUnit(); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); assertThat(e, hasToString(containsString("failed to parse [" + s + "]"))); assertThat(e, not(hasToString(containsString(FRACTIONAL_TIME_VALUES_ARE_NOT_SUPPORTED)))); assertThat(e.getCause(), instanceOf(NumberFormatException.class)); @@ -171,8 +148,7 @@ public class TimeValueTests extends OpenSearchTestCase { value = randomDouble(); } while (value == 0); final String s = Double.toString(randomIntBetween(0, 128) + value) + randomTimeUnit(); - final IllegalArgumentException e = - expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); + final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> TimeValue.parseTimeValue(s, null, "test")); assertThat(e, hasToString(containsString("failed to parse [" + s + "]"))); assertThat(e, hasToString(containsString(FRACTIONAL_TIME_VALUES_ARE_NOT_SUPPORTED))); assertThat(e.getCause(), instanceOf(NumberFormatException.class)); @@ -239,7 +215,7 @@ public class TimeValueTests extends OpenSearchTestCase { public void testCompareUnits() { long number = randomNonNegativeLong(); - TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, ()->randomFrom(TimeUnit.values())); + TimeUnit randomUnit = randomValueOtherThan(TimeUnit.DAYS, () -> randomFrom(TimeUnit.values())); TimeValue firstValue = new TimeValue(number, randomUnit); TimeValue secondValue = new TimeValue(number, TimeUnit.DAYS); assertTrue(firstValue.compareTo(secondValue) < 0); @@ -256,11 +232,20 @@ public class TimeValueTests extends OpenSearchTestCase { final String settingName = "test-value"; final long negativeValue = randomLongBetween(Long.MIN_VALUE, -2); final String negativeTimeValueString = Long.toString(negativeValue) + randomTimeUnit(); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> TimeValue.parseTimeValue(negativeTimeValueString, settingName)); - assertThat(ex.getMessage(), - equalTo("failed to parse setting [" + settingName + "] with value [" + negativeTimeValueString + - "] as a time value: negative durations are not supported")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> TimeValue.parseTimeValue(negativeTimeValueString, settingName) + ); + assertThat( + ex.getMessage(), + equalTo( + "failed to parse setting [" + + settingName + + "] with value [" + + negativeTimeValueString + + "] as a time value: negative durations are not supported" + ) + ); } public void testRejectsNegativeValuesAtCreation() { @@ -270,7 +255,14 @@ public class TimeValueTests extends OpenSearchTestCase { } private TimeUnit randomTimeUnitObject() { - return randomFrom(TimeUnit.NANOSECONDS, TimeUnit.MICROSECONDS, TimeUnit.MILLISECONDS, TimeUnit.SECONDS, - TimeUnit.MINUTES, TimeUnit.HOURS, TimeUnit.DAYS); + return randomFrom( + TimeUnit.NANOSECONDS, + TimeUnit.MICROSECONDS, + TimeUnit.MILLISECONDS, + TimeUnit.SECONDS, + TimeUnit.MINUTES, + TimeUnit.HOURS, + TimeUnit.DAYS + ); } } diff --git a/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java b/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java index 196a51b3581..793b2373294 100644 --- a/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java +++ b/libs/core/src/test/java/org/opensearch/common/util/OpenSearchSloppyMathTests.java @@ -46,8 +46,8 @@ public class OpenSearchSloppyMathTests extends OpenSearchTestCase { public void testAtan() { assertTrue(Double.isNaN(atan(Double.NaN))); - assertEquals(-Math.PI/2, atan(Double.NEGATIVE_INFINITY), ATAN_DELTA); - assertEquals(Math.PI/2, atan(Double.POSITIVE_INFINITY), ATAN_DELTA); + assertEquals(-Math.PI / 2, atan(Double.NEGATIVE_INFINITY), ATAN_DELTA); + assertEquals(Math.PI / 2, atan(Double.POSITIVE_INFINITY), ATAN_DELTA); for (int i = 0; i < 10000; i++) { assertEquals(StrictMath.atan(i), atan(i), ATAN_DELTA); assertEquals(StrictMath.atan(-i), atan(-i), ATAN_DELTA); diff --git a/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java b/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java index 14d93835406..85632ae406a 100644 --- a/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java +++ b/libs/core/src/test/java/org/opensearch/core/internal/io/IOUtilsTests.java @@ -97,8 +97,8 @@ public class IOUtilsTests extends OpenSearchTestCase { runTestCloseWithIOExceptions((Function>) Arrays::asList, IOUtils::close); } - private void runTestCloseWithIOExceptions( - final Function function, final CheckedConsumer close) throws IOException { + private void runTestCloseWithIOExceptions(final Function function, final CheckedConsumer close) + throws IOException { final int numberOfCloseables = randomIntBetween(1, 8); final Closeable[] closeables = new Closeable[numberOfCloseables]; final List indexesThatThrow = new ArrayList<>(numberOfCloseables); @@ -137,7 +137,9 @@ public class IOUtilsTests extends OpenSearchTestCase { } private void runDeleteFilesIgnoringExceptionsTest( - final Function function, CheckedConsumer deleteFilesIgnoringExceptions) throws IOException { + final Function function, + CheckedConsumer deleteFilesIgnoringExceptions + ) throws IOException { final int numberOfFiles = randomIntBetween(0, 7); final Path[] files = new Path[numberOfFiles]; for (int i = 0; i < numberOfFiles; i++) { @@ -170,8 +172,9 @@ public class IOUtilsTests extends OpenSearchTestCase { for (int i = 0; i < numberOfLocations; i++) { if (exception && randomBoolean()) { final Path location = createTempDir(); - final FileSystem fs = - new AccessDeniedWhileDeletingFileSystem(location.getFileSystem()).getFileSystem(URI.create("file:///")); + final FileSystem fs = new AccessDeniedWhileDeletingFileSystem(location.getFileSystem()).getFileSystem( + URI.create("file:///") + ); final Path wrapped = new FilterPath(location, fs); locations[i] = wrapped.resolve(randomAlphaOfLength(8)); Files.createDirectory(locations[i]); @@ -241,10 +244,8 @@ public class IOUtilsTests extends OpenSearchTestCase { } @Override - public FileChannel newFileChannel( - final Path path, - final Set options, - final FileAttribute... attrs) throws IOException { + public FileChannel newFileChannel(final Path path, final Set options, final FileAttribute... attrs) + throws IOException { if (Files.isDirectory(path)) { throw new AccessDeniedException(path.toString()); } diff --git a/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java b/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java index bd5e7a1852f..a4322446422 100644 --- a/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java +++ b/libs/dissect/src/main/java/org/opensearch/dissect/DissectKey.java @@ -124,7 +124,7 @@ public final class DissectKey { * @param key The key to copy (except for the modifier) * @param modifier the modifer to use for this copy */ - DissectKey(DissectKey key, DissectKey.Modifier modifier){ + DissectKey(DissectKey key, DissectKey.Modifier modifier) { this.modifier = modifier; this.skipRightPadding = key.skipRightPadding; this.skip = key.skip; @@ -152,19 +152,29 @@ public final class DissectKey { return name; } - //generated + // generated @Override public String toString() { - return "DissectKey{" + - "modifier=" + modifier + - ", skip=" + skip + - ", appendPosition=" + appendPosition + - ", name='" + name + '\'' + - '}'; + return "DissectKey{" + + "modifier=" + + modifier + + ", skip=" + + skip + + ", appendPosition=" + + appendPosition + + ", name='" + + name + + '\'' + + '}'; } public enum Modifier { - NONE(""), APPEND_WITH_ORDER("/"), APPEND("+"), FIELD_NAME("*"), FIELD_VALUE("&"), NAMED_SKIP("?"); + NONE(""), + APPEND_WITH_ORDER("/"), + APPEND("+"), + FIELD_NAME("*"), + FIELD_VALUE("&"), + NAMED_SKIP("?"); private static final Pattern MODIFIER_PATTERN = Pattern.compile("[/+*&?]"); @@ -179,10 +189,13 @@ public final class DissectKey { this.modifier = modifier; } - //package private for testing + // package private for testing static Modifier fromString(String modifier) { - return EnumSet.allOf(Modifier.class).stream().filter(km -> km.modifier.equals(modifier)) - .findFirst().orElseThrow(() -> new IllegalArgumentException("Found invalid modifier.")); //throw should never happen + return EnumSet.allOf(Modifier.class) + .stream() + .filter(km -> km.modifier.equals(modifier)) + .findFirst() + .orElseThrow(() -> new IllegalArgumentException("Found invalid modifier.")); // throw should never happen } private static Modifier findModifier(String key) { diff --git a/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java b/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java index 98d47e4773e..72dce8f9bb4 100644 --- a/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java +++ b/libs/dissect/src/main/java/org/opensearch/dissect/DissectMatch.java @@ -59,7 +59,7 @@ final class DissectMatch { DissectMatch(String appendSeparator, int maxMatches, int maxResults, int appendCount, int referenceCount) { if (maxMatches <= 0 || maxResults <= 0) { - throw new IllegalArgumentException("Expected results are zero, can not construct DissectMatch");//should never happen + throw new IllegalArgumentException("Expected results are zero, can not construct DissectMatch");// should never happen } this.maxMatches = maxMatches; this.maxResults = maxResults; @@ -91,8 +91,8 @@ final class DissectMatch { appendResults.computeIfAbsent(key.getName(), k -> new AppendResult(appendSeparator)).addValue(value, implicitAppendOrder++); break; case APPEND_WITH_ORDER: - appendResults.computeIfAbsent(key.getName(), - k -> new AppendResult(appendSeparator)).addValue(value, key.getAppendPosition()); + appendResults.computeIfAbsent(key.getName(), k -> new AppendResult(appendSeparator)) + .addValue(value, key.getAppendPosition()); break; case FIELD_NAME: referenceResults.computeIfAbsent(key.getName(), k -> new ReferenceResult()).setKey(value); diff --git a/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java b/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java index 212a65a8260..9861847c9e1 100644 --- a/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java +++ b/libs/dissect/src/main/java/org/opensearch/dissect/DissectParser.java @@ -110,10 +110,12 @@ public final class DissectParser { private static final Pattern KEY_DELIMITER_FIELD_PATTERN = Pattern.compile("%\\{([^}]*?)}([^%]*)", Pattern.DOTALL); private static final EnumSet ASSOCIATE_MODIFIERS = EnumSet.of( DissectKey.Modifier.FIELD_NAME, - DissectKey.Modifier.FIELD_VALUE); + DissectKey.Modifier.FIELD_VALUE + ); private static final EnumSet APPEND_MODIFIERS = EnumSet.of( DissectKey.Modifier.APPEND, - DissectKey.Modifier.APPEND_WITH_ORDER); + DissectKey.Modifier.APPEND_WITH_ORDER + ); private static final Function KEY_NAME = val -> val.getKey().getName(); private final List matchPairs; private final String pattern; @@ -139,16 +141,19 @@ public final class DissectParser { matchPairs.add(new DissectPair(key, delimiter)); } this.maxMatches = matchPairs.size(); - this.maxResults = Long.valueOf(matchPairs.stream() - .filter(dissectPair -> !dissectPair.getKey().skip()).map(KEY_NAME).distinct().count()).intValue(); + this.maxResults = Long.valueOf( + matchPairs.stream().filter(dissectPair -> !dissectPair.getKey().skip()).map(KEY_NAME).distinct().count() + ).intValue(); if (this.maxMatches == 0 || maxResults == 0) { throw new DissectException.PatternParse(pattern, "Unable to find any keys or delimiters."); } - //append validation - look through all of the keys to see if there are any keys that need to participate in an append operation + // append validation - look through all of the keys to see if there are any keys that need to participate in an append operation // but don't have the '+' defined Set appendKeyNames = matchPairs.stream() .filter(dissectPair -> APPEND_MODIFIERS.contains(dissectPair.getKey().getModifier())) - .map(KEY_NAME).distinct().collect(Collectors.toSet()); + .map(KEY_NAME) + .distinct() + .collect(Collectors.toSet()); if (appendKeyNames.size() > 0) { List modifiedMatchPairs = new ArrayList<>(matchPairs.size()); for (DissectPair p : matchPairs) { @@ -162,15 +167,18 @@ public final class DissectParser { } appendCount = appendKeyNames.size(); - //reference validation - ensure that '*' and '&' come in pairs + // reference validation - ensure that '*' and '&' come in pairs Map> referenceGroupings = matchPairs.stream() .filter(dissectPair -> ASSOCIATE_MODIFIERS.contains(dissectPair.getKey().getModifier())) .collect(Collectors.groupingBy(KEY_NAME)); for (Map.Entry> entry : referenceGroupings.entrySet()) { if (entry.getValue().size() != 2) { - throw new DissectException.PatternParse(pattern, "Found invalid key/reference associations: '" - + entry.getValue().stream().map(KEY_NAME).collect(Collectors.joining(",")) + - "' Please ensure each '*' is matched with a matching '&"); + throw new DissectException.PatternParse( + pattern, + "Found invalid key/reference associations: '" + + entry.getValue().stream().map(KEY_NAME).collect(Collectors.joining(",")) + + "' Please ensure each '*' is matched with a matching '&" + ); } } @@ -178,7 +186,6 @@ public final class DissectParser { this.matchPairs = Collections.unmodifiableList(matchPairs); } - /** *

Entry point to dissect a string into it's parts.

* @@ -210,38 +217,39 @@ public final class DissectParser { */ DissectMatch dissectMatch = new DissectMatch(appendSeparator, maxMatches, maxResults, appendCount, referenceCount); Iterator it = matchPairs.iterator(); - //ensure leading delimiter matches - if (inputString != null && inputString.length() > leadingDelimiter.length() + // ensure leading delimiter matches + if (inputString != null + && inputString.length() > leadingDelimiter.length() && leadingDelimiter.equals(inputString.substring(0, leadingDelimiter.length()))) { byte[] input = inputString.getBytes(StandardCharsets.UTF_8); - //grab the first key/delimiter pair + // grab the first key/delimiter pair DissectPair dissectPair = it.next(); DissectKey key = dissectPair.getKey(); byte[] delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8); - //start dissection after the first delimiter + // start dissection after the first delimiter int i = leadingDelimiter.length(); int valueStart = i; int lookAheadMatches; - //start walking the input string byte by byte, look ahead for matches where needed - //if a match is found jump forward to the end of the match + // start walking the input string byte by byte, look ahead for matches where needed + // if a match is found jump forward to the end of the match for (; i < input.length; i++) { lookAheadMatches = 0; - //potential match between delimiter and input string + // potential match between delimiter and input string if (delimiter.length > 0 && input[i] == delimiter[0]) { - //look ahead to see if the entire delimiter matches the input string + // look ahead to see if the entire delimiter matches the input string for (int j = 0; j < delimiter.length; j++) { if (i + j < input.length && input[i + j] == delimiter[j]) { lookAheadMatches++; } } - //found a full delimiter match + // found a full delimiter match if (lookAheadMatches == delimiter.length) { - //record the key/value tuple + // record the key/value tuple byte[] value = Arrays.copyOfRange(input, valueStart, i); dissectMatch.add(key, new String(value, StandardCharsets.UTF_8)); - //jump to the end of the match + // jump to the end of the match i += lookAheadMatches; - //look for consecutive delimiters (e.g. a,,,,d,e) + // look for consecutive delimiters (e.g. a,,,,d,e) while (i < input.length) { lookAheadMatches = 0; for (int j = 0; j < delimiter.length; j++) { @@ -249,39 +257,39 @@ public final class DissectParser { lookAheadMatches++; } } - //found consecutive delimiters + // found consecutive delimiters if (lookAheadMatches == delimiter.length) { - //jump to the end of the match + // jump to the end of the match i += lookAheadMatches; if (!key.skipRightPadding()) { - //progress the keys/delimiter if possible + // progress the keys/delimiter if possible if (!it.hasNext()) { - break; //the while loop + break; // the while loop } dissectPair = it.next(); key = dissectPair.getKey(); - //add the key with an empty value for the empty delimiter + // add the key with an empty value for the empty delimiter dissectMatch.add(key, ""); } } else { - break; //the while loop + break; // the while loop } } - //progress the keys/delimiter if possible + // progress the keys/delimiter if possible if (!it.hasNext()) { - break; //the for loop + break; // the for loop } dissectPair = it.next(); key = dissectPair.getKey(); delimiter = dissectPair.getDelimiter().getBytes(StandardCharsets.UTF_8); - //i is always one byte after the last found delimiter, aka the start of the next value + // i is always one byte after the last found delimiter, aka the start of the next value valueStart = i; } } } - //the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key) - //and there is no trailing delimiter - if (!dissectMatch.fullyMatched() && delimiter.length == 0 ) { + // the last key, grab the rest of the input (unless consecutive delimiters already grabbed the last key) + // and there is no trailing delimiter + if (!dissectMatch.fullyMatched() && delimiter.length == 0) { byte[] value = Arrays.copyOfRange(input, valueStart, input.length); String valueString = new String(value, StandardCharsets.UTF_8); dissectMatch.add(key, valueString); @@ -318,6 +326,3 @@ public final class DissectParser { } } - - - diff --git a/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java b/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java index 185b294a72d..a8d282506a5 100644 --- a/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java +++ b/libs/dissect/src/test/java/org/opensearch/dissect/DissectKeyTests.java @@ -135,7 +135,8 @@ public class DissectKeyTests extends OpenSearchTestCase { public void testMultipleLeftModifiers() { String keyName = randomAlphaOfLengthBetween(1, 10); - List validModifiers = EnumSet.allOf(DissectKey.Modifier.class).stream() + List validModifiers = EnumSet.allOf(DissectKey.Modifier.class) + .stream() .filter(m -> !m.equals(DissectKey.Modifier.NONE)) .map(DissectKey.Modifier::toString) .collect(Collectors.toList()); @@ -154,9 +155,10 @@ public class DissectKeyTests extends OpenSearchTestCase { assertThat(dissectKey.getAppendPosition(), equalTo(0)); assertThat(dissectKey.getName(), equalTo(keyName)); } + public void testNamedSkipKey() { String keyName = "myname"; - DissectKey dissectKey = new DissectKey("?" +keyName); + DissectKey dissectKey = new DissectKey("?" + keyName); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NAMED_SKIP)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(false)); @@ -166,16 +168,17 @@ public class DissectKeyTests extends OpenSearchTestCase { public void testSkipKeyWithPadding() { String keyName = ""; - DissectKey dissectKey = new DissectKey(keyName + "->"); + DissectKey dissectKey = new DissectKey(keyName + "->"); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NONE)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(true)); assertThat(dissectKey.getAppendPosition(), equalTo(0)); assertThat(dissectKey.getName(), equalTo(keyName)); } + public void testNamedEmptySkipKeyWithPadding() { String keyName = ""; - DissectKey dissectKey = new DissectKey("?" +keyName + "->"); + DissectKey dissectKey = new DissectKey("?" + keyName + "->"); assertThat(dissectKey.getModifier(), equalTo(DissectKey.Modifier.NAMED_SKIP)); assertThat(dissectKey.skip(), is(true)); assertThat(dissectKey.skipRightPadding(), is(true)); @@ -184,7 +187,7 @@ public class DissectKeyTests extends OpenSearchTestCase { } public void testInvalidModifiers() { - //should never happen due to regex + // should never happen due to regex IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> DissectKey.Modifier.fromString("x")); assertThat(e.getMessage(), CoreMatchers.containsString("invalid modifier")); } diff --git a/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java b/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java index 289322ff646..0ec617b3d48 100644 --- a/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java +++ b/libs/dissect/src/test/java/org/opensearch/dissect/DissectMatchTests.java @@ -51,8 +51,8 @@ public class DissectMatchTests extends OpenSearchTestCase { public void testValidAndFullyMatched() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.fullyMatched(), equalTo(true)); assertThat(dissectMatch.isValid(dissectMatch.getResults()), equalTo(true)); } @@ -60,21 +60,21 @@ public class DissectMatchTests extends OpenSearchTestCase { public void testNotValidAndFullyMatched() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches - 1) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches - 1) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.fullyMatched(), equalTo(false)); assertThat(dissectMatch.isValid(dissectMatch.getResults()), equalTo(false)); } - public void testGetResultsIdempotent(){ + public void testGetResultsIdempotent() { int expectedMatches = randomIntBetween(1, 26); DissectMatch dissectMatch = new DissectMatch("", expectedMatches, expectedMatches, 0, 0); - IntStream.range(97, 97 + expectedMatches) //allow for a-z values - .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[]{(byte) i}, StandardCharsets.UTF_8)), "")); + IntStream.range(97, 97 + expectedMatches) // allow for a-z values + .forEach(i -> dissectMatch.add(new DissectKey(new String(new byte[] { (byte) i }, StandardCharsets.UTF_8)), "")); assertThat(dissectMatch.getResults(), equalTo(dissectMatch.getResults())); } - public void testAppend(){ + public void testAppend() { DissectMatch dissectMatch = new DissectMatch("-", 3, 1, 3, 0); dissectMatch.add(new DissectKey("+a"), "x"); dissectMatch.add(new DissectKey("+a"), "y"); @@ -84,7 +84,7 @@ public class DissectMatchTests extends OpenSearchTestCase { assertThat(results, equalTo(MapBuilder.newMapBuilder().put("a", "x-y-z").map())); } - public void testAppendWithOrder(){ + public void testAppendWithOrder() { DissectMatch dissectMatch = new DissectMatch("-", 3, 1, 3, 0); dissectMatch.add(new DissectKey("+a/3"), "x"); dissectMatch.add(new DissectKey("+a"), "y"); @@ -94,7 +94,7 @@ public class DissectMatchTests extends OpenSearchTestCase { assertThat(results, equalTo(MapBuilder.newMapBuilder().put("a", "y-z-x").map())); } - public void testReference(){ + public void testReference() { DissectMatch dissectMatch = new DissectMatch("-", 2, 1, 0, 1); dissectMatch.add(new DissectKey("&a"), "x"); dissectMatch.add(new DissectKey("*a"), "y"); diff --git a/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java b/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java index a647a1cc628..fcd0dc8e248 100644 --- a/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java +++ b/libs/dissect/src/test/java/org/opensearch/dissect/DissectParserTests.java @@ -77,46 +77,103 @@ public class DissectParserTests extends OpenSearchTestCase { assertMatch("%{a} %{b} %{+b} %{z}", "foo bar baz quux", Arrays.asList("a", "b", "z"), Arrays.asList("foo", "bar baz", "quux"), " "); assertMatch("%{a}------->%{b}", "foo------->bar baz quux", Arrays.asList("a", "b"), Arrays.asList("foo", "bar baz quux")); assertMatch("%{a}------->%{}", "foo------->bar baz quux", Arrays.asList("a"), Arrays.asList("foo")); - assertMatch("%{a} » %{b}»%{c}€%{d}", "foo » bar»baz€quux", - Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "bar", "baz", "quux")); + assertMatch( + "%{a} » %{b}»%{c}€%{d}", + "foo » bar»baz€quux", + Arrays.asList("a", "b", "c", "d"), + Arrays.asList("foo", "bar", "baz", "quux") + ); assertMatch("%{a} %{b} %{+a}", "foo bar baz quux", Arrays.asList("a", "b"), Arrays.asList("foo baz quux", "bar"), " "); - //Logstash supports implicit ordering based anchored by the key without the '+' - //This implementation will only honor implicit ordering for appending right to left else explicit order (/N) is required. - //The results of this test differ from Logstash. - assertMatch("%{+a} %{a} %{+a} %{b}", "December 31 1999 quux", - Arrays.asList("a", "b"), Arrays.asList("December 31 1999", "quux"), " "); - //Same test as above, but with same result as Logstash using explicit ordering in the pattern - assertMatch("%{+a/1} %{a} %{+a/2} %{b}", "December 31 1999 quux", - Arrays.asList("a", "b"), Arrays.asList("31 December 1999", "quux"), " "); + // Logstash supports implicit ordering based anchored by the key without the '+' + // This implementation will only honor implicit ordering for appending right to left else explicit order (/N) is required. + // The results of this test differ from Logstash. + assertMatch( + "%{+a} %{a} %{+a} %{b}", + "December 31 1999 quux", + Arrays.asList("a", "b"), + Arrays.asList("December 31 1999", "quux"), + " " + ); + // Same test as above, but with same result as Logstash using explicit ordering in the pattern + assertMatch( + "%{+a/1} %{a} %{+a/2} %{b}", + "December 31 1999 quux", + Arrays.asList("a", "b"), + Arrays.asList("31 December 1999", "quux"), + " " + ); assertMatch("%{+a/2} %{+a/4} %{+a/1} %{+a/3}", "bar quux foo baz", Arrays.asList("a"), Arrays.asList("foo bar baz quux"), " "); assertMatch("%{+a} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - assertMatch("%{+a} %{b} %{+a} %{c}", "foo bar baz quux", - Arrays.asList("a", "b", "c"), Arrays.asList("foo baz", "bar", "quux"), " "); - assertMatch("%{} %{syslog_timestamp} %{hostname} %{rt}: %{reason} %{+reason} %{src_ip}/%{src_port}->%{dst_ip}/%{dst_port} " + - "%{polrt} %{+polrt} %{+polrt} %{from_zone} %{to_zone} %{rest}", - "42 2016-05-25T14:47:23Z host.name.com RT_FLOW - RT_FLOW_SESSION_DENY: session denied 2.2.2.20/60000->1.1.1.10/8090 None " + - "6(0) DEFAULT-DENY ZONE-UNTRUST ZONE-DMZ UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0", - Arrays.asList("syslog_timestamp", "hostname", "rt", "reason", "src_ip", "src_port", "dst_ip", "dst_port", "polrt" - , "from_zone", "to_zone", "rest"), - Arrays.asList("2016-05-25T14:47:23Z", "host.name.com", "RT_FLOW - RT_FLOW_SESSION_DENY", "session denied", "2.2.2.20", "60000" - , "1.1.1.10", "8090", "None 6(0) DEFAULT-DENY", "ZONE-UNTRUST", "ZONE-DMZ", "UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0"), " "); + assertMatch( + "%{+a} %{b} %{+a} %{c}", + "foo bar baz quux", + Arrays.asList("a", "b", "c"), + Arrays.asList("foo baz", "bar", "quux"), + " " + ); + assertMatch( + "%{} %{syslog_timestamp} %{hostname} %{rt}: %{reason} %{+reason} %{src_ip}/%{src_port}->%{dst_ip}/%{dst_port} " + + "%{polrt} %{+polrt} %{+polrt} %{from_zone} %{to_zone} %{rest}", + "42 2016-05-25T14:47:23Z host.name.com RT_FLOW - RT_FLOW_SESSION_DENY: session denied 2.2.2.20/60000->1.1.1.10/8090 None " + + "6(0) DEFAULT-DENY ZONE-UNTRUST ZONE-DMZ UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0", + Arrays.asList( + "syslog_timestamp", + "hostname", + "rt", + "reason", + "src_ip", + "src_port", + "dst_ip", + "dst_port", + "polrt", + "from_zone", + "to_zone", + "rest" + ), + Arrays.asList( + "2016-05-25T14:47:23Z", + "host.name.com", + "RT_FLOW - RT_FLOW_SESSION_DENY", + "session denied", + "2.2.2.20", + "60000", + "1.1.1.10", + "8090", + "None 6(0) DEFAULT-DENY", + "ZONE-UNTRUST", + "ZONE-DMZ", + "UNKNOWN UNKNOWN N/A(N/A) ge-0/0/0.0" + ), + " " + ); assertBadKey("%{+/2}"); assertBadKey("%{&+a_field}"); - assertMatch("%{a->} %{b->}---%{c}", "foo bar------------baz", - Arrays.asList("a", "b", "c"), Arrays.asList("foo", "bar", "baz")); + assertMatch( + "%{a->} %{b->}---%{c}", + "foo bar------------baz", + Arrays.asList("a", "b", "c"), + Arrays.asList("foo", "bar", "baz") + ); assertMatch("%{->}-%{a}", "-----666", Arrays.asList("a"), Arrays.asList("666")); assertMatch("%{?skipme->}-%{a}", "-----666", Arrays.asList("a"), Arrays.asList("666")); - assertMatch("%{a},%{b},%{c},%{d},%{e},%{f}", "111,,333,,555,666", - Arrays.asList("a", "b", "c", "d", "e", "f"), Arrays.asList("111", "", "333", "", "555", "666")); + assertMatch( + "%{a},%{b},%{c},%{d},%{e},%{f}", + "111,,333,,555,666", + Arrays.asList("a", "b", "c", "d", "e", "f"), + Arrays.asList("111", "", "333", "", "555", "666") + ); assertMatch("%{a}.࿏.%{b}", "⟳༒.࿏.༒⟲", Arrays.asList("a", "b"), Arrays.asList("⟳༒", "༒⟲")); assertMatch("%{a}", "子", Arrays.asList("a"), Arrays.asList("子")); assertMatch("%{a}{\n}%{b}", "aaa{\n}bbb", Arrays.asList("a", "b"), Arrays.asList("aaa", "bbb")); assertMiss("MACHINE[%{a}] %{b}", "1234567890 MACHINE[foo] bar"); assertMiss("%{a} %{b} %{c}", "foo:bar:baz"); assertMatch("/var/%{key1}/log/%{key2}.log", "/var/foo/log/bar.log", Arrays.asList("key1", "key2"), Arrays.asList("foo", "bar")); - assertMatch("%{a->} %{b}-.-%{c}-%{d}-..-%{e}-%{f}-%{g}-%{h}", "foo bar-.-baz-1111-..-22-333-4444-55555", + assertMatch( + "%{a->} %{b}-.-%{c}-%{d}-..-%{e}-%{f}-%{g}-%{h}", + "foo bar-.-baz-1111-..-22-333-4444-55555", Arrays.asList("a", "b", "c", "d", "e", "f", "g", "h"), - Arrays.asList("foo", "bar", "baz", "1111", "22", "333", "4444", "55555")); + Arrays.asList("foo", "bar", "baz", "1111", "22", "333", "4444", "55555") + ); } public void testBasicMatch() { @@ -124,12 +181,13 @@ public class DissectParserTests extends OpenSearchTestCase { String keyFirstPattern = ""; String delimiterFirstInput = ""; String delimiterFirstPattern = ""; - //parallel arrays + // parallel arrays List expectedKeys = new ArrayList<>(Sets.newSet(generateRandomStringArray(100, 10, false, false))); List expectedValues = new ArrayList<>(expectedKeys.size()); for (String key : expectedKeys) { String value = randomAsciiAlphanumOfLengthBetween(1, 100); - String delimiter = Integer.toString(randomInt()); //int to ensures values and delimiters don't overlap, else validation can fail + String delimiter = Integer.toString(randomInt()); // int to ensures values and delimiters don't overlap, else validation can + // fail keyFirstPattern += "%{" + key + "}" + delimiter; valueFirstInput += value + delimiter; delimiterFirstPattern += delimiter + "%{" + key + "}"; @@ -145,7 +203,7 @@ public class DissectParserTests extends OpenSearchTestCase { String keyFirstPattern = ""; String delimiterFirstInput = ""; String delimiterFirstPattern = ""; - //parallel arrays + // parallel arrays List expectedKeys = new ArrayList<>(); List expectedValues = new ArrayList<>(); for (int i = 0; i < randomIntBetween(1, 100); i++) { @@ -154,7 +212,8 @@ public class DissectParserTests extends OpenSearchTestCase { key = randomAsciiAlphanumOfLengthBetween(1, 100); } String value = randomRealisticUnicodeOfCodepointLengthBetween(1, 100); - String delimiter = Integer.toString(randomInt()); //int to ensures values and delimiters don't overlap, else validation can fail + String delimiter = Integer.toString(randomInt()); // int to ensures values and delimiters don't overlap, else validation can + // fail keyFirstPattern += "%{" + key + "}" + delimiter; valueFirstInput += value + delimiter; delimiterFirstPattern += delimiter + "%{" + key + "}"; @@ -196,8 +255,12 @@ public class DissectParserTests extends OpenSearchTestCase { assertMatch("%{*a} %{&a}", "foo bar", Arrays.asList("foo"), Arrays.asList("bar")); assertMatch("%{&a} %{*a}", "foo bar", Arrays.asList("bar"), Arrays.asList("foo")); assertMatch("%{*a} %{&a} %{*b} %{&b}", "foo bar baz lol", Arrays.asList("foo", "baz"), Arrays.asList("bar", "lol")); - assertMatch("%{*a} %{&a} %{c} %{*b} %{&b}", "foo bar x baz lol", - Arrays.asList("foo", "baz", "c"), Arrays.asList("bar", "lol", "x")); + assertMatch( + "%{*a} %{&a} %{c} %{*b} %{&b}", + "foo bar x baz lol", + Arrays.asList("foo", "baz", "c"), + Arrays.asList("bar", "lol", "x") + ); assertBadPattern("%{*a} %{a}"); assertBadPattern("%{a} %{&a}"); assertMiss("%{*a} %{&a} {a} %{*b} %{&b}", "foo bar x baz lol"); @@ -205,8 +268,12 @@ public class DissectParserTests extends OpenSearchTestCase { public void testAppendAndAssociate() { assertMatch("%{a} %{+a} %{*b} %{&b}", "foo bar baz lol", Arrays.asList("a", "baz"), Arrays.asList("foobar", "lol")); - assertMatch("%{a->} %{+a/2} %{+a/1} %{*b} %{&b}", "foo bar baz lol x", - Arrays.asList("a", "lol"), Arrays.asList("foobazbar", "x")); + assertMatch( + "%{a->} %{+a/2} %{+a/1} %{*b} %{&b}", + "foo bar baz lol x", + Arrays.asList("a", "lol"), + Arrays.asList("foobazbar", "x") + ); } public void testEmptyKey() { @@ -230,19 +297,19 @@ public class DissectParserTests extends OpenSearchTestCase { } public void testConsecutiveDelimiters() { - //leading + // leading assertMatch("%{->},%{a}", ",,,,,foo", Arrays.asList("a"), Arrays.asList("foo")); assertMatch("%{a->},%{b}", ",,,,,foo", Arrays.asList("a", "b"), Arrays.asList("", "foo")); - //trailing + // trailing assertMatch("%{a->},", "foo,,,,,", Arrays.asList("a"), Arrays.asList("foo")); assertMatch("%{a} %{b},", "foo bar,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a} %{b->},", "foo bar,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //middle + // middle assertMatch("%{a->},%{b}", "foo,,,,,bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->}x%{b}", "fooxxxxxbar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} xyz%{b}", "foo xyz xyz xyz xyz xyzbar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //skipped with empty values + // skipped with empty values assertMatch("%{a},%{b},%{c},%{d}", "foo,,,", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "", "")); assertMatch("%{a},%{b},%{c},%{d}", "foo,,bar,baz", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "bar", "baz")); assertMatch("%{a},%{b},%{c},%{d}", "foo,,,baz", Arrays.asList("a", "b", "c", "d"), Arrays.asList("foo", "", "", "baz")); @@ -259,16 +326,20 @@ public class DissectParserTests extends OpenSearchTestCase { assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a->} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{->} %{a}", "foo bar", Arrays.asList("a"), Arrays.asList("bar")); - assertMatch("%{a->} %{+a->} %{*b->} %{&b->} %{c}", "foo bar baz lol x", - Arrays.asList("a", "baz", "c"), Arrays.asList("foobar", "lol", "x")); + assertMatch( + "%{a->} %{+a->} %{*b->} %{&b->} %{c}", + "foo bar baz lol x", + Arrays.asList("a", "baz", "c"), + Arrays.asList("foobar", "lol", "x") + ); } public void testTrimmedEnd() { assertMatch("%{a} %{b}", "foo bar", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); assertMatch("%{a} %{b->} ", "foo bar ", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); - //only whitespace is trimmed in the absence of trailing characters + // only whitespace is trimmed in the absence of trailing characters assertMatch("%{a} %{b->}", "foo bar,,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar,,,,,,")); - //consecutive delimiters + right padding can be used to skip over the trailing delimiters + // consecutive delimiters + right padding can be used to skip over the trailing delimiters assertMatch("%{a} %{b->},", "foo bar,,,,,,", Arrays.asList("a", "b"), Arrays.asList("foo", "bar")); } @@ -305,23 +376,50 @@ public class DissectParserTests extends OpenSearchTestCase { } public void testSyslog() { - assertMatch("%{timestamp} %{+timestamp} %{+timestamp} %{logsource} %{program}[%{pid}]: %{message}", + assertMatch( + "%{timestamp} %{+timestamp} %{+timestamp} %{logsource} %{program}[%{pid}]: %{message}", "Mar 16 00:01:25 evita postfix/smtpd[1713]: connect from camomile.cloud9.net[168.100.1.3]", Arrays.asList("timestamp", "logsource", "program", "pid", "message"), - Arrays.asList("Mar 16 00:01:25", "evita", "postfix/smtpd", "1713", "connect from camomile.cloud9.net[168.100.1.3]"), " "); + Arrays.asList("Mar 16 00:01:25", "evita", "postfix/smtpd", "1713", "connect from camomile.cloud9.net[168.100.1.3]"), + " " + ); } public void testApacheLog() { - assertMatch("%{clientip} %{ident} %{auth} [%{timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{response} %{bytes}" + - " \"%{referrer}\" \"%{agent}\" %{->}", - "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + - "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"", - Arrays.asList("clientip", "ident", "auth", "timestamp", "verb", "request", "httpversion", "response", "bytes", - "referrer", "agent"), - Arrays.asList("31.184.238.164", "-", "-", "24/Jul/2014:05:35:37 +0530", "GET", "/logs/access.log", "1.0", "200", "69849", - "http://8rursodiol.enjin.com", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + - " (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36")); + assertMatch( + "%{clientip} %{ident} %{auth} [%{timestamp}] \"%{verb} %{request} HTTP/%{httpversion}\" %{response} %{bytes}" + + " \"%{referrer}\" \"%{agent}\" %{->}", + "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\"", + Arrays.asList( + "clientip", + "ident", + "auth", + "timestamp", + "verb", + "request", + "httpversion", + "response", + "bytes", + "referrer", + "agent" + ), + Arrays.asList( + "31.184.238.164", + "-", + "-", + "24/Jul/2014:05:35:37 +0530", + "GET", + "/logs/access.log", + "1.0", + "200", + "69849", + "http://8rursodiol.enjin.com", + "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36" + + " (KHTML, like Gecko) Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36" + ) + ); } /** @@ -357,7 +455,7 @@ public class DissectParserTests extends OpenSearchTestCase { } } - private DissectException assertFail(String pattern, String input){ + private DissectException assertFail(String pattern, String input) { return expectThrows(DissectException.class, () -> new DissectParser(pattern, null).parse(input)); } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Circle.java b/libs/geo/src/main/java/org/opensearch/geometry/Circle.java index 8669fd51fa1..6f8b0dc6929 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Circle.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Circle.java @@ -61,7 +61,7 @@ public class Circle implements Geometry { this.x = x; this.radiusMeters = radiusMeters; this.z = z; - if (radiusMeters < 0 ) { + if (radiusMeters < 0) { throw new IllegalArgumentException("Circle radius [" + radiusMeters + "] cannot be negative"); } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Line.java b/libs/geo/src/main/java/org/opensearch/geometry/Line.java index 14819c3f425..7e6806eeba5 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Line.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Line.java @@ -156,8 +156,7 @@ public class Line implements Geometry { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Line line = (Line) o; - return Arrays.equals(y, line.y) && - Arrays.equals(x, line.x) && Arrays.equals(z, line.z); + return Arrays.equals(y, line.y) && Arrays.equals(x, line.x) && Arrays.equals(z, line.z); } @Override diff --git a/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java b/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java index 10ba0f97b74..d9348495930 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/LinearRing.java @@ -42,8 +42,7 @@ import java.util.Arrays; public class LinearRing extends Line { public static final LinearRing EMPTY = new LinearRing(); - private LinearRing() { - } + private LinearRing() {} public LinearRing(double[] x, double[] y) { this(x, y, null); @@ -56,10 +55,22 @@ public class LinearRing extends Line { } int last = x.length - 1; if (x[0] != x[last] || y[0] != y[last] || (z != null && z[0] != z[last])) { - throw new IllegalArgumentException("first and last points of the linear ring must be the same (it must close itself):" + - " x[0]=" + x[0] + " x[" + last + "]=" + x[last] + - " y[0]=" + y[0] + " y[" + last + "]=" + y[last] + - (z == null ? "" : " z[0]=" + z[0] + " z[" + last + "]=" + z[last] )); + throw new IllegalArgumentException( + "first and last points of the linear ring must be the same (it must close itself):" + + " x[0]=" + + x[0] + + " x[" + + last + + "]=" + + x[last] + + " y[0]=" + + y[0] + + " y[" + + last + + "]=" + + y[last] + + (z == null ? "" : " z[0]=" + z[0] + " z[" + last + "]=" + z[last]) + ); } } @@ -75,8 +86,11 @@ public class LinearRing extends Line { @Override public String toString() { - return "linearring(x=" + Arrays.toString(getX()) + - ", y=" + Arrays.toString(getY()) + - (hasZ() ? ", z=" + Arrays.toString(getZ()) : "") + ")"; + return "linearring(x=" + + Arrays.toString(getX()) + + ", y=" + + Arrays.toString(getY()) + + (hasZ() ? ", z=" + Arrays.toString(getZ()) : "") + + ")"; } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java b/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java index 37873488996..07d3e52a7c5 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/MultiLine.java @@ -40,8 +40,7 @@ import java.util.List; public class MultiLine extends GeometryCollection { public static final MultiLine EMPTY = new MultiLine(); - private MultiLine() { - } + private MultiLine() {} public MultiLine(List lines) { super(lines); diff --git a/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java b/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java index 0b7f4dcb930..6cbf2d05004 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/MultiPoint.java @@ -40,8 +40,7 @@ import java.util.List; public class MultiPoint extends GeometryCollection { public static final MultiPoint EMPTY = new MultiPoint(); - private MultiPoint() { - } + private MultiPoint() {} public MultiPoint(List points) { super(points); diff --git a/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java b/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java index 35c140774e9..749b55d69eb 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/MultiPolygon.java @@ -40,8 +40,7 @@ import java.util.List; public class MultiPolygon extends GeometryCollection { public static final MultiPolygon EMPTY = new MultiPolygon(); - private MultiPolygon() { - } + private MultiPolygon() {} public MultiPolygon(List polygons) { super(polygons); diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java b/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java index f372d6ff177..0fb5167c403 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Polygon.java @@ -135,8 +135,7 @@ public final class Polygon implements Geometry { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Polygon polygon1 = (Polygon) o; - return Objects.equals(polygon, polygon1.polygon) && - Objects.equals(holes, polygon1.holes); + return Objects.equals(polygon, polygon1.polygon) && Objects.equals(holes, polygon1.holes); } @Override diff --git a/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java b/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java index 699087a89fe..9ea4a3b3002 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/Rectangle.java @@ -82,6 +82,7 @@ public class Rectangle implements Geometry { public Rectangle(double minX, double maxX, double maxY, double minY) { this(minX, maxX, maxY, minY, Double.NaN, Double.NaN); } + /** * Constructs a bounding box by first validating the provided latitude and longitude coordinates */ @@ -133,7 +134,6 @@ public class Rectangle implements Geometry { return minX; } - public double getMinAlt() { return minZ; } @@ -160,7 +160,6 @@ public class Rectangle implements Geometry { return WellKnownText.INSTANCE.toWKT(this); } - @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/libs/geo/src/main/java/org/opensearch/geometry/package-info.java b/libs/geo/src/main/java/org/opensearch/geometry/package-info.java index 19dc6d8dcbd..ffab6e99ae9 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/package-info.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/package-info.java @@ -25,7 +25,6 @@ * under the License. */ - /** * Common Geometry classes */ diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java index 8c742695d1e..664e7e68d96 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/BitUtil.java @@ -36,13 +36,15 @@ package org.opensearch.geometry.utils; */ public class BitUtil { // magic numbers for bit interleaving private static final long MAGIC[] = { - 0x5555555555555555L, 0x3333333333333333L, - 0x0F0F0F0F0F0F0F0FL, 0x00FF00FF00FF00FFL, - 0x0000FFFF0000FFFFL, 0x00000000FFFFFFFFL, - 0xAAAAAAAAAAAAAAAAL - }; + 0x5555555555555555L, + 0x3333333333333333L, + 0x0F0F0F0F0F0F0F0FL, + 0x00FF00FF00FF00FFL, + 0x0000FFFF0000FFFFL, + 0x00000000FFFFFFFFL, + 0xAAAAAAAAAAAAAAAAL }; // shift values for bit interleaving - private static final short SHIFT[] = {1, 2, 4, 8, 16}; + private static final short SHIFT[] = { 1, 2, 4, 8, 16 }; /** * Interleaves the first 32 bits of each long value @@ -63,7 +65,7 @@ public class BitUtil { // magic numbers for bit interleaving v2 = (v2 | (v2 << SHIFT[1])) & MAGIC[1]; v2 = (v2 | (v2 << SHIFT[0])) & MAGIC[0]; - return (v2<<1) | v1; + return (v2 << 1) | v1; } /** @@ -83,6 +85,6 @@ public class BitUtil { // magic numbers for bit interleaving * flip flops odd with even bits */ public static final long flipFlop(final long b) { - return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1 ); + return ((b & MAGIC[6]) >>> 1) | ((b & MAGIC[0]) << 1); } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java index 2d9d2482b10..bfde23c3da2 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/GeographyValidator.java @@ -83,7 +83,8 @@ public class GeographyValidator implements GeometryValidator { protected void checkLatitude(double latitude) { if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { throw new IllegalArgumentException( - "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL + ); } } @@ -93,14 +94,16 @@ public class GeographyValidator implements GeometryValidator { protected void checkLongitude(double longitude) { if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { throw new IllegalArgumentException( - "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL + ); } } protected void checkAltitude(double zValue) { if (ignoreZValue == false && Double.isNaN(zValue) == false) { - throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " - + "parameter is [" + ignoreZValue + "]"); + throw new IllegalArgumentException( + "found Z value [" + zValue + "] but [ignore_z_value] " + "parameter is [" + ignoreZValue + "]" + ); } } diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java index 5b98bc3b2f2..8b3b841e221 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/Geohash.java @@ -48,21 +48,51 @@ import java.util.Collection; * NOTE: this will replace {@code org.opensearch.common.geo.GeoHashUtils} */ public class Geohash { - private static final char[] BASE_32 = {'0', '1', '2', '3', '4', '5', '6', - '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', - 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; + private static final char[] BASE_32 = { + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'b', + 'c', + 'd', + 'e', + 'f', + 'g', + 'h', + 'j', + 'k', + 'm', + 'n', + 'p', + 'q', + 'r', + 's', + 't', + 'u', + 'v', + 'w', + 'x', + 'y', + 'z' }; private static final String BASE_32_STRING = new String(BASE_32); /** maximum precision for geohash strings */ public static final int PRECISION = 12; /** number of bits used for quantizing latitude and longitude values */ private static final short BITS = 32; - private static final double LAT_SCALE = (0x1L<<(BITS-1))/180.0D; - private static final double LAT_DECODE = 180.0D/(0x1L<>> 4) << shift); Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash)); return new Rectangle(bottomLeft.getX(), topRight.getX(), topRight.getY(), bottomLeft.getY()); } else { // We cannot go north of north pole, so just using 90 degrees instead of calculating it using // add 1 to lon to get lon of topRight, we are going to use 90 for lat - ghLong = BitUtil.interleave((int)lat, (int)(lon + 1)) << 4 | len; + ghLong = BitUtil.interleave((int) lat, (int) (lon + 1)) << 4 | len; final long mortonHash = BitUtil.flipFlop((ghLong >>> 4) << shift); Point topRight = new Point(decodeLongitude(mortonHash), decodeLatitude(mortonHash)); return new Rectangle(bottomLeft.getX(), topRight.getX(), 90D, bottomLeft.getY()); @@ -127,9 +156,9 @@ public class Geohash { /** Array of geohashes one level below the baseGeohash. Sorted. */ public static String[] getSubGeohashes(String baseGeohash) { String[] hashes = new String[BASE_32.length]; - for (int i = 0; i < BASE_32.length; i++) {//note: already sorted + for (int i = 0; i < BASE_32.length; i++) {// note: already sorted char c = BASE_32[i]; - hashes[i] = baseGeohash+c; + hashes[i] = baseGeohash + c; } return hashes; } @@ -143,6 +172,7 @@ public class Geohash { public static Collection getNeighbors(String geohash) { return addNeighborsAtLevel(geohash, geohash.length(), new ArrayList(8)); } + /** * Add all geohashes of the cells next to a given geohash to a list. * @@ -162,8 +192,7 @@ public class Geohash { * @param neighbors list to add the neighbors to * @return the given list */ - public static final > E addNeighborsAtLevel(String geohash, - int level, E neighbors) { + public static final > E addNeighborsAtLevel(String geohash, int level, E neighbors) { String south = getNeighbor(geohash, level, 0, -1); String north = getNeighbor(geohash, level, 0, +1); if (north != null) { @@ -194,7 +223,7 @@ public class Geohash { * @return geohash of the defined cell */ public static final String getNeighbor(String geohash, int level, int dx, int dy) { - int cell = BASE_32_STRING.indexOf(geohash.charAt(level -1)); + int cell = BASE_32_STRING.indexOf(geohash.charAt(level - 1)); // Decoding the Geohash bit pattern to determine grid coordinates int x0 = cell & 1; // first bit of x @@ -250,7 +279,7 @@ public class Geohash { */ public static final long longEncode(final double lon, final double lat, final int level) { // shift to appropriate level - final short msf = (short)(((12 - level) * 5) + (MORTON_OFFSET - 2)); + final short msf = (short) (((12 - level) * 5) + (MORTON_OFFSET - 2)); return ((encodeLatLon(lat, lon) >>> msf) << 4) | level; } @@ -276,13 +305,13 @@ public class Geohash { * Encode to a geohash string from the geohash based long format */ public static final String stringEncode(long geoHashLong) { - int level = (int)geoHashLong&15; + int level = (int) geoHashLong & 15; geoHashLong >>>= 4; char[] chars = new char[level]; do { - chars[--level] = BASE_32[(int) (geoHashLong&31L)]; - geoHashLong>>>=5; - } while(level > 0); + chars[--level] = BASE_32[(int) (geoHashLong & 31L)]; + geoHashLong >>>= 5; + } while (level > 0); return new String(chars); } @@ -299,9 +328,9 @@ public class Geohash { int level = length - 1; long b; long l = 0L; - for(char c : hash.toCharArray()) { - b = (long)(BASE_32_STRING.indexOf(c)); - l |= (b<<(level--*5)); + for (char c : hash.toCharArray()) { + b = (long) (BASE_32_STRING.indexOf(c)); + l |= (b << (level-- * 5)); if (level < 0) { // We cannot handle more than 12 levels break; @@ -320,12 +349,12 @@ public class Geohash { int level = 11; long b; long l = 0L; - for(char c : hash.toCharArray()) { - b = (long)(BASE_32_STRING.indexOf(c)); + for (char c : hash.toCharArray()) { + b = (long) (BASE_32_STRING.indexOf(c)); if (b < 0) { throw new IllegalArgumentException("unsupported symbol [" + c + "] in geohash [" + hash + "]"); } - l |= (b<<((level--*5) + (MORTON_OFFSET - 2))); + l |= (b << ((level-- * 5) + (MORTON_OFFSET - 2))); if (level < 0) { // We cannot handle more than 12 levels break; @@ -351,7 +380,6 @@ public class Geohash { return BitUtil.interleave(latEnc, lonEnc) >>> 2; } - /** encode latitude to integer */ public static int encodeLatitude(double latitude) { // the maximum possible value cannot be encoded without overflow diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java index 748b9fb12bc..a7482f1bcda 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/StandardValidator.java @@ -53,13 +53,14 @@ public class StandardValidator implements GeometryValidator { private final boolean ignoreZValue; public StandardValidator(boolean ignoreZValue) { - this.ignoreZValue = ignoreZValue; + this.ignoreZValue = ignoreZValue; } protected void checkZ(double zValue) { if (ignoreZValue == false && Double.isNaN(zValue) == false) { - throw new IllegalArgumentException("found Z value [" + zValue + "] but [ignore_z_value] " - + "parameter is [" + ignoreZValue + "]"); + throw new IllegalArgumentException( + "found Z value [" + zValue + "] but [ignore_z_value] " + "parameter is [" + ignoreZValue + "]" + ); } } @@ -138,4 +139,3 @@ public class StandardValidator implements GeometryValidator { } } } - diff --git a/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java b/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java index a92db5155dc..ed1d63e6d4f 100644 --- a/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java +++ b/libs/geo/src/main/java/org/opensearch/geometry/utils/WellKnownText.java @@ -446,8 +446,9 @@ public class WellKnownText { private void closeLinearRingIfCoerced(ArrayList lats, ArrayList lons, ArrayList alts) { if (coerce && lats.isEmpty() == false && lons.isEmpty() == false) { int last = lats.size() - 1; - if (!lats.get(0).equals(lats.get(last)) || !lons.get(0).equals(lons.get(last)) || - (alts.isEmpty() == false && !alts.get(0).equals(alts.get(last)))) { + if (!lats.get(0).equals(lats.get(last)) + || !lons.get(0).equals(lons.get(last)) + || (alts.isEmpty() == false && !alts.get(0).equals(alts.get(last)))) { lons.add(lons.get(0)); lats.add(lats.get(0)); if (alts.isEmpty() == false) { @@ -486,7 +487,6 @@ public class WellKnownText { return new Rectangle(minLon, maxLon, maxLat, minLat); } - private Circle parseCircle(StreamTokenizer stream) throws IOException, ParseException { if (nextEmptyOrOpen(stream).equals(EMPTY)) { return Circle.EMPTY; @@ -561,8 +561,7 @@ public class WellKnownText { if (next.equals(EMPTY) || next.equals(LPAREN)) { return next; } - throw new ParseException("expected " + EMPTY + " or " + LPAREN - + " but found: " + tokenString(stream), stream.lineno()); + throw new ParseException("expected " + EMPTY + " or " + LPAREN + " but found: " + tokenString(stream), stream.lineno()); } private String nextCloser(StreamTokenizer stream) throws IOException, ParseException { @@ -591,8 +590,7 @@ public class WellKnownText { if (token.equals(COMMA) || token.equals(RPAREN)) { return token; } - throw new ParseException("expected " + COMMA + " or " + RPAREN - + " but found: " + tokenString(stream), stream.lineno()); + throw new ParseException("expected " + COMMA + " or " + RPAREN + " but found: " + tokenString(stream), stream.lineno()); } private static String getWKTName(Geometry geometry) { diff --git a/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java b/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java index 4a20b96d70f..64edf0989d2 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/BaseGeometryTestCase.java @@ -87,7 +87,8 @@ abstract class BaseGeometryTestCase extends AbstractWireTest @Override public Object visit(GeometryCollection collection) { - return verify(collection, "GeometryCollection"); } + return verify(collection, "GeometryCollection"); + } @Override public Object visit(Line line) { diff --git a/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java b/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java index 4ddc286b922..1c7c8610623 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/CircleTests.java @@ -44,9 +44,13 @@ public class CircleTests extends BaseGeometryTestCase { @Override protected Circle createTestInstance(boolean hasAlt) { if (hasAlt) { - return new Circle(randomDoubleBetween(-180, 180, true), randomDoubleBetween(-90, 90, true), randomDouble(), - randomDoubleBetween(0, 100, false)); - } else { + return new Circle( + randomDoubleBetween(-180, 180, true), + randomDoubleBetween(-90, 90, true), + randomDouble(), + randomDoubleBetween(0, 100, false) + ); + } else { return new Circle(randomDoubleBetween(-180, 180, true), randomDoubleBetween(-90, 90, true), randomDoubleBetween(0, 100, false)); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java b/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java index 4ae66e83dd2..631b6456a77 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/GeometryCollectionTests.java @@ -50,11 +50,15 @@ public class GeometryCollectionTests extends BaseGeometryTestCase(Arrays.asList(new Point(20, 10), Point.EMPTY)))); + assertEquals( + "GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)", + wkt.toWKT(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY))) + ); - assertEquals(new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)), - wkt.fromWKT("GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)")); + assertEquals( + new GeometryCollection(Arrays.asList(new Point(20, 10), Point.EMPTY)), + wkt.fromWKT("GEOMETRYCOLLECTION (POINT (20.0 10.0),POINT EMPTY)") + ); assertEquals("GEOMETRYCOLLECTION EMPTY", wkt.toWKT(GeometryCollection.EMPTY)); assertEquals(GeometryCollection.EMPTY, wkt.fromWKT("GEOMETRYCOLLECTION EMPTY)")); @@ -68,12 +72,16 @@ public class GeometryCollectionTests extends BaseGeometryTestCase new GeometryCollection<>(null)); assertEquals("the list of shapes cannot be null or empty", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new GeometryCollection<>( - Arrays.asList(new Point(20, 10), new Point(20, 10, 30)))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new GeometryCollection<>(Arrays.asList(new Point(20, 10), new Point(20, 10, 30))) + ); assertEquals("all elements of the collection should have the same number of dimension", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new GeometryCollection(Collections.singletonList(new Point(20, 10, 30))))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new GeometryCollection(Collections.singletonList(new Point(20, 10, 30)))) + ); assertEquals("found Z value [30.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate(new GeometryCollection(Collections.singletonList(new Point(20, 10, 30)))); diff --git a/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java b/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java index f2f76a966e3..6d21340e64b 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/GeometryValidatorTests.java @@ -86,7 +86,8 @@ public class GeometryValidatorTests extends OpenSearchTestCase { protected void checkLatitude(double latitude) { if (Double.isNaN(latitude) || latitude < MIN_LAT_INCL || latitude > MAX_LAT_INCL) { throw new IllegalArgumentException( - "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL); + "invalid latitude " + latitude + "; must be between " + MIN_LAT_INCL + " and " + MAX_LAT_INCL + ); } } @@ -94,7 +95,8 @@ public class GeometryValidatorTests extends OpenSearchTestCase { protected void checkLongitude(double longitude) { if (Double.isNaN(longitude) || longitude < MIN_LON_INCL || longitude > MAX_LON_INCL) { throw new IllegalArgumentException( - "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL); + "invalid longitude " + longitude + "; must be between " + MIN_LON_INCL + " and " + MAX_LON_INCL + ); } } @@ -102,7 +104,8 @@ public class GeometryValidatorTests extends OpenSearchTestCase { protected void checkAltitude(double zValue) { if (Double.isNaN(zValue) == false && (zValue < MIN_ALT_INCL || zValue > MAX_ALT_INCL)) { throw new IllegalArgumentException( - "invalid altitude " + zValue + "; must be between " + MIN_ALT_INCL + " and " + MAX_ALT_INCL); + "invalid altitude " + zValue + "; must be between " + MIN_ALT_INCL + " and " + MAX_ALT_INCL + ); } } } @@ -129,12 +132,13 @@ public class GeometryValidatorTests extends OpenSearchTestCase { assertEquals("invalid altitude 2.0; must be between -1.0 and 1.0", ex.getMessage()); ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("POLYGON ((0.3 0.1, 0.4 0.2, 5 0.3, 0.3 0.1))")); assertEquals("invalid longitude 5.0; must be between -1.0 and 1.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT( - "POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); + ex = expectThrows( + IllegalArgumentException.class, + () -> parser.fromWKT("POLYGON ((0.3 0.1, 0.4 0.2, 0.5 0.3, 0.3 0.1), (0.5 1.5, 2.5 1.5, 2.0 1.0))") + ); assertEquals("invalid latitude 1.5; must be between -1.0 and 1.0", ex.getMessage()); ex = expectThrows(IllegalArgumentException.class, () -> parser.fromWKT("MULTIPOINT (0 1, -2 1)")); assertEquals("invalid longitude -2.0; must be between -1.0 and 1.0", ex.getMessage()); } - } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java b/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java index 7d695dbbb14..846645a99ed 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/LineTests.java @@ -49,13 +49,17 @@ public class LineTests extends BaseGeometryTestCase { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("LINESTRING (3.0 1.0, 4.0 2.0)", wkt.toWKT(new Line(new double[]{3, 4}, new double[]{1, 2}))); - assertEquals(new Line(new double[]{3, 4}, new double[]{1, 2}), wkt.fromWKT("LINESTRING (3 1, 4 2)")); + assertEquals("LINESTRING (3.0 1.0, 4.0 2.0)", wkt.toWKT(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }))); + assertEquals(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }), wkt.fromWKT("LINESTRING (3 1, 4 2)")); - assertEquals("LINESTRING (3.0 1.0 5.0, 4.0 2.0 6.0)", wkt.toWKT(new Line(new double[]{3, 4}, new double[]{1, 2}, - new double[]{5, 6}))); - assertEquals(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}), - wkt.fromWKT("LINESTRING (3 1 6, 4 2 5)")); + assertEquals( + "LINESTRING (3.0 1.0 5.0, 4.0 2.0 6.0)", + wkt.toWKT(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 5, 6 })) + ); + assertEquals( + new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }), + wkt.fromWKT("LINESTRING (3 1 6, 4 2 5)") + ); assertEquals("LINESTRING EMPTY", wkt.toWKT(Line.EMPTY)); assertEquals(Line.EMPTY, wkt.fromWKT("LINESTRING EMPTY)")); @@ -63,28 +67,38 @@ public class LineTests extends BaseGeometryTestCase { public void testInitValidation() { GeometryValidator validator = new GeographyValidator(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3}, new double[]{1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3 }, new double[] { 1 })) + ); assertEquals("at least two points in the line is required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3, 4, 500, 3}, new double[]{1, 2, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3, 4, 500, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("invalid longitude 500.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Line(new double[]{3, 4, 5, 3}, new double[]{1, 100, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new Line(new double[] { 3, 4, 5, 3 }, new double[] { 1, 100, 3, 1 })) + ); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 })) + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - new StandardValidator(true).validate(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5})); + new StandardValidator(true).validate(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 })); } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("linestring (3 1 6, 4 2 5)")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("linestring (3 1 6, 4 2 5)") + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java b/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java index 98d4b455aca..338f470133f 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/LinearRingTests.java @@ -41,46 +41,65 @@ import org.opensearch.test.OpenSearchTestCase; public class LinearRingTests extends OpenSearchTestCase { public void testBasicSerialization() { - UnsupportedOperationException ex = expectThrows(UnsupportedOperationException.class, - () -> new WellKnownText(true, new GeographyValidator(true)) - .toWKT(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}))); + UnsupportedOperationException ex = expectThrows( + UnsupportedOperationException.class, + () -> new WellKnownText(true, new GeographyValidator(true)).toWKT( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }) + ) + ); assertEquals("line ring cannot be serialized using WKT", ex.getMessage()); } public void testInitValidation() { GeometryValidator validator = new GeographyValidator(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 5}, new double[]{1, 2, 3}))); - assertEquals("first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=5.0 y[0]=1.0 " + - "y[2]=3.0", - ex.getMessage()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 5 }, new double[] { 1, 2, 3 })) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=5.0 y[0]=1.0 " + "y[2]=3.0", + ex.getMessage() + ); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 3}, new double[]{1, 2, 1}, new double[]{1, 2, 3}))); - assertEquals("first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=3.0 y[0]=1.0 " + - "y[2]=1.0 z[0]=1.0 z[2]=3.0", - ex.getMessage()); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 3 }, new double[] { 1, 2, 1 }, new double[] { 1, 2, 3 })) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): x[0]=3.0 x[2]=3.0 y[0]=1.0 " + + "y[2]=1.0 z[0]=1.0 z[2]=3.0", + ex.getMessage() + ); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3}, new double[]{1}))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new LinearRing(new double[] { 3 }, new double[] { 1 }))); assertEquals("at least two points in the line is required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 500, 3}, new double[]{1, 2, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 500, 3 }, new double[] { 1, 2, 3, 1 })) + ); assertEquals("invalid longitude 500.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 100, 3, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> validator.validate(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 100, 3, 1 })) + ); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 1, 1, 1}))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 1, 1, 1 }) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - new StandardValidator(true).validate(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 1, 1, 1})); + new StandardValidator(true).validate( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 1, 1, 1 }) + ); } public void testVisitor() { - BaseGeometryTestCase.testVisitor(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})); + BaseGeometryTestCase.testVisitor(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java b/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java index ba045cd7e78..d1979d2ddfa 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/MultiLineTests.java @@ -57,21 +57,30 @@ public class MultiLineTests extends BaseGeometryTestCase { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("MULTILINESTRING ((3.0 1.0, 4.0 2.0))", wkt.toWKT( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}))))); - assertEquals(new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}))), - wkt.fromWKT("MULTILINESTRING ((3 1, 4 2))")); + assertEquals( + "MULTILINESTRING ((3.0 1.0, 4.0 2.0))", + wkt.toWKT(new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 })))) + ); + assertEquals( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }))), + wkt.fromWKT("MULTILINESTRING ((3 1, 4 2))") + ); assertEquals("MULTILINESTRING EMPTY", wkt.toWKT(MultiLine.EMPTY)); assertEquals(MultiLine.EMPTY, wkt.fromWKT("MULTILINESTRING EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5}))))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }))) + ) + ); assertEquals("found Z value [6.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate( - new MultiLine(Collections.singletonList(new Line(new double[]{3, 4}, new double[]{1, 2}, new double[]{6, 5})))); + new MultiLine(Collections.singletonList(new Line(new double[] { 3, 4 }, new double[] { 1, 2 }, new double[] { 6, 5 }))) + ); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java b/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java index 4373bf3400d..ed04823463e 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/MultiPointTests.java @@ -58,28 +58,27 @@ public class MultiPointTests extends BaseGeometryTestCase { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("MULTIPOINT (2.0 1.0)", wkt.toWKT( - new MultiPoint(Collections.singletonList(new Point(2, 1))))); - assertEquals(new MultiPoint(Collections.singletonList(new Point(2, 1))), - wkt.fromWKT("MULTIPOINT (2 1)")); + assertEquals("MULTIPOINT (2.0 1.0)", wkt.toWKT(new MultiPoint(Collections.singletonList(new Point(2, 1))))); + assertEquals(new MultiPoint(Collections.singletonList(new Point(2, 1))), wkt.fromWKT("MULTIPOINT (2 1)")); - assertEquals("MULTIPOINT (2.0 1.0, 3.0 4.0)", - wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))))); - assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))), - wkt.fromWKT("MULTIPOINT (2 1, 3 4)")); + assertEquals("MULTIPOINT (2.0 1.0, 3.0 4.0)", wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))))); + assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1), new Point(3, 4))), wkt.fromWKT("MULTIPOINT (2 1, 3 4)")); - assertEquals("MULTIPOINT (2.0 1.0 10.0, 3.0 4.0 20.0)", - wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))))); - assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))), - wkt.fromWKT("MULTIPOINT (2 1 10, 3 4 20)")); + assertEquals( + "MULTIPOINT (2.0 1.0 10.0, 3.0 4.0 20.0)", + wkt.toWKT(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20)))) + ); + assertEquals(new MultiPoint(Arrays.asList(new Point(2, 1, 10), new Point(3, 4, 20))), wkt.fromWKT("MULTIPOINT (2 1 10, 3 4 20)")); assertEquals("MULTIPOINT EMPTY", wkt.toWKT(MultiPoint.EMPTY)); assertEquals(MultiPoint.EMPTY, wkt.fromWKT("MULTIPOINT EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new MultiPoint(Collections.singletonList(new Point(2, 1, 3))))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new MultiPoint(Collections.singletonList(new Point(2, 1, 3)))) + ); assertEquals("found Z value [3.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate(new MultiPoint(Collections.singletonList(new Point(2, 1, 3)))); diff --git a/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java b/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java index 4944ccbba74..7a72aa76d90 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/MultiPolygonTests.java @@ -57,26 +57,44 @@ public class MultiPolygonTests extends BaseGeometryTestCase { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", - wkt.toWKT(new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))))); - assertEquals(new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))), - wkt.fromWKT("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))")); + assertEquals( + "MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))", + wkt.toWKT( + new MultiPolygon( + Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ) + ) + ); + assertEquals( + new MultiPolygon( + Collections.singletonList(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ), + wkt.fromWKT("MULTIPOLYGON (((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0)))") + ); assertEquals("MULTIPOLYGON EMPTY", wkt.toWKT(MultiPolygon.EMPTY)); assertEquals(MultiPolygon.EMPTY, wkt.fromWKT("MULTIPOLYGON EMPTY)")); } public void testValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1})) - )))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new MultiPolygon( + Collections.singletonList( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate( - new MultiPolygon(Collections.singletonList( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1}))))); + new MultiPolygon( + Collections.singletonList( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ) + ); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java b/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java index 4d783196fa7..f0e71890a7a 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/PointTests.java @@ -74,8 +74,10 @@ public class PointTests extends BaseGeometryTestCase { } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("point (20.0 10.0 100.0)")); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("point (20.0 10.0 100.0)") + ); assertEquals("found Z value [100.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java b/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java index 8954a1ba47f..66a5310121a 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/PolygonTests.java @@ -49,65 +49,106 @@ public class PolygonTests extends BaseGeometryTestCase { public void testBasicSerialization() throws IOException, ParseException { WellKnownText wkt = new WellKnownText(true, new GeographyValidator(true)); - assertEquals("POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", - wkt.toWKT(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})), - wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1))")); + assertEquals( + "POLYGON ((3.0 1.0, 4.0 2.0, 5.0 3.0, 3.0 1.0))", + wkt.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }))) + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), + wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1))") + ); - assertEquals("POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", - wkt.toWKT(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})))); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})), - wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))")); + assertEquals( + "POLYGON ((3.0 1.0 5.0, 4.0 2.0 4.0, 5.0 3.0 3.0, 3.0 1.0 5.0))", + wkt.toWKT(new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 }))) + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), + wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3, 3 1 5))") + ); // Auto closing in coerce mode - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})), - wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3))")); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5})), - wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3))")); - assertEquals(new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}), - Collections.singletonList(new LinearRing(new double[]{0.5, 2.5, 2.0, 0.5}, new double[]{1.5, 1.5, 1.0, 1.5}))), - wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })), + wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3))") + ); + assertEquals( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 })), + wkt.fromWKT("POLYGON ((3 1 5, 4 2 4, 5 3 3))") + ); + assertEquals( + new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), + Collections.singletonList(new LinearRing(new double[] { 0.5, 2.5, 2.0, 0.5 }, new double[] { 1.5, 1.5, 1.0, 1.5 })) + ), + wkt.fromWKT("POLYGON ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))") + ); assertEquals("POLYGON EMPTY", wkt.toWKT(Polygon.EMPTY)); assertEquals(Polygon.EMPTY, wkt.fromWKT("POLYGON EMPTY)")); } public void testInitValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 3}, new double[]{1, 2, 1}))); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon(new LinearRing(new double[] { 3, 4, 3 }, new double[] { 1, 2, 1 })) + ); assertEquals("at least 4 polygon points required", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}), null)); + ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }), null) + ); assertEquals("holes must not be null", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{5, 4, 3, 5}), - Collections.singletonList(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1})))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new Polygon( + new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 5, 4, 3, 5 }), + Collections.singletonList(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 })) + ) + ); assertEquals("holes must have the same number of dimensions as the polygon", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1})))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate( + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ) + ); assertEquals("found Z value [1.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate( - new Polygon(new LinearRing(new double[]{3, 4, 5, 3}, new double[]{1, 2, 3, 1}, new double[]{1, 2, 3, 1}))); + new Polygon(new LinearRing(new double[] { 3, 4, 5, 3 }, new double[] { 1, 2, 3, 1 }, new double[] { 1, 2, 3, 1 })) + ); } public void testWKTValidation() { - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(false, new GeographyValidator(true)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3))")); - assertEquals("first and last points of the linear ring must be the same (it must close itself): " + - "x[0]=3.0 x[2]=5.0 y[0]=1.0 y[2]=3.0 z[0]=5.0 z[2]=3.0", ex.getMessage()); + IllegalArgumentException ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(false, new GeographyValidator(true)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3))") + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): " + + "x[0]=3.0 x[2]=5.0 y[0]=1.0 y[2]=3.0 z[0]=5.0 z[2]=3.0", + ex.getMessage() + ); - ex = expectThrows(IllegalArgumentException.class, - () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3, 3 1 5))")); + ex = expectThrows( + IllegalArgumentException.class, + () -> new WellKnownText(randomBoolean(), new GeographyValidator(false)).fromWKT("polygon ((3 1 5, 4 2 4, 5 3 3, 3 1 5))") + ); assertEquals("found Z value [5.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, + ex = expectThrows( + IllegalArgumentException.class, () -> new WellKnownText(false, new GeographyValidator(randomBoolean())).fromWKT( - "polygon ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))")); - assertEquals("first and last points of the linear ring must be the same (it must close itself): " + - "x[0]=0.5 x[2]=2.0 y[0]=1.5 y[2]=1.0", ex.getMessage()); + "polygon ((3 1, 4 2, 5 3, 3 1), (0.5 1.5, 2.5 1.5, 2.0 1.0))" + ) + ); + assertEquals( + "first and last points of the linear ring must be the same (it must close itself): " + "x[0]=0.5 x[2]=2.0 y[0]=1.5 y[2]=1.0", + ex.getMessage() + ); } } diff --git a/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java b/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java index cb78ebda232..fdc50fce15b 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/RectangleTests.java @@ -59,24 +59,22 @@ public class RectangleTests extends BaseGeometryTestCase { public void testInitValidation() { GeometryValidator validator = new GeographyValidator(true); - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 100, 1))); + IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 100, 1))); assertEquals("invalid latitude 100.0; must be between -90.0 and 90.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(200, 3, 2, 1))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(200, 3, 2, 1))); assertEquals("invalid longitude 200.0; must be between -180.0 and 180.0", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 1, 2))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 1, 2))); assertEquals("max y cannot be less than min x", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, - () -> validator.validate(new Rectangle(2, 3, 2, 1, 5, Double.NaN))); + ex = expectThrows(IllegalArgumentException.class, () -> validator.validate(new Rectangle(2, 3, 2, 1, 5, Double.NaN))); assertEquals("only one z value is specified", ex.getMessage()); - ex = expectThrows(IllegalArgumentException.class, () -> new StandardValidator(false).validate( - new Rectangle(50, 10, 40, 30, 20, 60))); + ex = expectThrows( + IllegalArgumentException.class, + () -> new StandardValidator(false).validate(new Rectangle(50, 10, 40, 30, 20, 60)) + ); assertEquals("found Z value [20.0] but [ignore_z_value] parameter is [false]", ex.getMessage()); new StandardValidator(true).validate(new Rectangle(50, 10, 40, 30, 20, 60)); diff --git a/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java b/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java index 403ff67ff1f..a039574706a 100644 --- a/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java +++ b/libs/geo/src/test/java/org/opensearch/geometry/utils/GeoHashTests.java @@ -42,15 +42,12 @@ public class GeoHashTests extends OpenSearchTestCase { public void testGeohashAsLongRoutines() { final GeoPoint expected = new GeoPoint(); final GeoPoint actual = new GeoPoint(); - //Ensure that for all points at all supported levels of precision + // Ensure that for all points at all supported levels of precision // that the long encoding of a geohash is compatible with its // String based counterpart - for (double lat=-90;lat<90;lat++) - { - for (double lng=-180;lng<180;lng++) - { - for(int p=1;p<=12;p++) - { + for (double lat = -90; lat < 90; lat++) { + for (double lng = -180; lng < 180; lng++) { + for (int p = 1; p <= 12; p++) { long geoAsLong = Geohash.longEncode(lng, lat, p); // string encode from geohashlong encoded location @@ -104,11 +101,11 @@ public class GeoHashTests extends OpenSearchTestCase { // Adding some random geohash characters at the end String extendedGeohash = geohash + randomGeohash(1, 10); GeoPoint actual = GeoPoint.fromGeohash(extendedGeohash); - assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]" , expected, actual); + assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]", expected, actual); Rectangle expectedBbox = Geohash.toBoundingBox(geohash); Rectangle actualBbox = Geohash.toBoundingBox(extendedGeohash); - assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]" , expectedBbox, actualBbox); + assertEquals("Additional data points above 12 should be ignored [" + extendedGeohash + "]", expectedBbox, actualBbox); } } diff --git a/libs/grok/src/main/java/org/opensearch/grok/Grok.java b/libs/grok/src/main/java/org/opensearch/grok/Grok.java index 3bae4b360e3..e80bde02360 100644 --- a/libs/grok/src/main/java/org/opensearch/grok/Grok.java +++ b/libs/grok/src/main/java/org/opensearch/grok/Grok.java @@ -68,20 +68,26 @@ public final class Grok { private static final String SUBNAME_GROUP = "subname"; private static final String PATTERN_GROUP = "pattern"; private static final String DEFINITION_GROUP = "definition"; - private static final String GROK_PATTERN = - "%\\{" + - "(?" + - "(?[A-z0-9]+)" + - "(?::(?[[:alnum:]@\\[\\]_:.-]+))?" + - ")" + - "(?:=(?" + - "(?:[^{}]+|\\.+)+" + - ")" + - ")?" + "\\}"; - private static final Regex GROK_PATTERN_REGEX = new Regex(GROK_PATTERN.getBytes(StandardCharsets.UTF_8), 0, - GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, Option.NONE, UTF8Encoding.INSTANCE, Syntax.DEFAULT); + private static final String GROK_PATTERN = "%\\{" + + "(?" + + "(?[A-z0-9]+)" + + "(?::(?[[:alnum:]@\\[\\]_:.-]+))?" + + ")" + + "(?:=(?" + + "(?:[^{}]+|\\.+)+" + + ")" + + ")?" + + "\\}"; + private static final Regex GROK_PATTERN_REGEX = new Regex( + GROK_PATTERN.getBytes(StandardCharsets.UTF_8), + 0, + GROK_PATTERN.getBytes(StandardCharsets.UTF_8).length, + Option.NONE, + UTF8Encoding.INSTANCE, + Syntax.DEFAULT + ); - private static final int MAX_TO_REGEX_ITERATIONS = 100_000; //sanity limit + private static final int MAX_TO_REGEX_ITERATIONS = 100_000; // sanity limit private final Map patternBank; private final boolean namedCaptures; @@ -101,8 +107,13 @@ public final class Grok { this(patternBank, grokPattern, namedCaptures, MatcherWatchdog.noop(), logCallBack); } - private Grok(Map patternBank, String grokPattern, boolean namedCaptures, MatcherWatchdog matcherWatchdog, - Consumer logCallBack) { + private Grok( + Map patternBank, + String grokPattern, + boolean namedCaptures, + MatcherWatchdog matcherWatchdog, + Consumer logCallBack + ) { this.patternBank = patternBank; this.namedCaptures = namedCaptures; this.matcherWatchdog = matcherWatchdog; @@ -111,8 +122,14 @@ public final class Grok { String expression = toRegex(grokPattern); byte[] expressionBytes = expression.getBytes(StandardCharsets.UTF_8); - this.compiledExpression = new Regex(expressionBytes, 0, expressionBytes.length, Option.DEFAULT, UTF8Encoding.INSTANCE, - message -> logCallBack.accept(message)); + this.compiledExpression = new Regex( + expressionBytes, + 0, + expressionBytes.length, + Option.DEFAULT, + UTF8Encoding.INSTANCE, + message -> logCallBack.accept(message) + ); List captureConfig = new ArrayList<>(); for (Iterator entry = compiledExpression.namedBackrefIterator(); entry.hasNext();) { @@ -141,8 +158,7 @@ public final class Grok { */ private void validatePatternBank(String patternName, Stack path) { String pattern = patternBank.get(patternName); - boolean isSelfReference = pattern.contains("%{" + patternName + "}") || - pattern.contains("%{" + patternName + ":"); + boolean isSelfReference = pattern.contains("%{" + patternName + "}") || pattern.contains("%{" + patternName + ":"); if (isSelfReference) { throwExceptionForCircularReference(patternName, pattern); } else if (path.contains(patternName)) { @@ -156,7 +172,7 @@ public final class Grok { int begin = i + 2; int syntaxEndIndex = pattern.indexOf('}', begin); if (syntaxEndIndex == -1) { - throw new IllegalArgumentException("Malformed pattern [" + patternName + "][" + pattern +"]"); + throw new IllegalArgumentException("Malformed pattern [" + patternName + "][" + pattern + "]"); } int semanticNameIndex = pattern.indexOf(':', begin); int end = syntaxEndIndex; @@ -173,8 +189,12 @@ public final class Grok { throwExceptionForCircularReference(patternName, pattern, null, null); } - private static void throwExceptionForCircularReference(String patternName, String pattern, String originPatterName, - Stack path) { + private static void throwExceptionForCircularReference( + String patternName, + String pattern, + String originPatterName, + Stack path + ) { StringBuilder message = new StringBuilder("circular reference in pattern ["); message.append(patternName).append("][").append(pattern).append("]"); if (originPatterName != null) { @@ -188,8 +208,12 @@ public final class Grok { private String groupMatch(String name, Region region, String pattern) { try { - int number = GROK_PATTERN_REGEX.nameToBackrefNumber(name.getBytes(StandardCharsets.UTF_8), 0, - name.getBytes(StandardCharsets.UTF_8).length, region); + int number = GROK_PATTERN_REGEX.nameToBackrefNumber( + name.getBytes(StandardCharsets.UTF_8), + 0, + name.getBytes(StandardCharsets.UTF_8).length, + region + ); int begin = region.beg[number]; int end = region.end[number]; return new String(pattern.getBytes(StandardCharsets.UTF_8), begin, end - begin, StandardCharsets.UTF_8); @@ -305,8 +329,9 @@ public final class Grok { matcherWatchdog.unregister(matcher); } if (result == Matcher.INTERRUPTED) { - throw new RuntimeException("grok pattern matching was interrupted after [" + - matcherWatchdog.maxExecutionTimeInMillis() + "] ms"); + throw new RuntimeException( + "grok pattern matching was interrupted after [" + matcherWatchdog.maxExecutionTimeInMillis() + "] ms" + ); } if (result == Matcher.FAILED) { return false; @@ -327,14 +352,30 @@ public final class Grok { */ private static Map loadBuiltinPatterns() { String[] patternNames = new String[] { - "aws", "bacula", "bind", "bro", "exim", "firewalls", "grok-patterns", "haproxy", - "java", "junos", "linux-syslog", "maven", "mcollective-patterns", "mongodb", "nagios", - "postgresql", "rails", "redis", "ruby", "squid" - }; + "aws", + "bacula", + "bind", + "bro", + "exim", + "firewalls", + "grok-patterns", + "haproxy", + "java", + "junos", + "linux-syslog", + "maven", + "mcollective-patterns", + "mongodb", + "nagios", + "postgresql", + "rails", + "redis", + "ruby", + "squid" }; Map builtinPatterns = new LinkedHashMap<>(); for (String pattern : patternNames) { try { - try(InputStream is = Grok.class.getResourceAsStream("/patterns/" + pattern)) { + try (InputStream is = Grok.class.getResourceAsStream("/patterns/" + pattern)) { loadPatterns(builtinPatterns, is); } } catch (IOException e) { @@ -361,4 +402,3 @@ public final class Grok { } } - diff --git a/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java b/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java index 9fc20d51fea..70b4570ee69 100644 --- a/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java +++ b/libs/grok/src/main/java/org/opensearch/grok/MatcherWatchdog.java @@ -83,10 +83,12 @@ public interface MatcherWatchdog { * @param relativeTimeSupplier A supplier that returns relative time * @param scheduler A scheduler that is able to execute a command for each fixed interval */ - static MatcherWatchdog newInstance(long interval, - long maxExecutionTime, - LongSupplier relativeTimeSupplier, - BiConsumer scheduler) { + static MatcherWatchdog newInstance( + long interval, + long maxExecutionTime, + LongSupplier relativeTimeSupplier, + BiConsumer scheduler + ) { return new Default(interval, maxExecutionTime, relativeTimeSupplier, scheduler); } @@ -101,12 +103,10 @@ public interface MatcherWatchdog { private static final Noop INSTANCE = new Noop(); - private Noop() { - } + private Noop() {} @Override - public void register(Matcher matcher) { - } + public void register(Matcher matcher) {} @Override public long maxExecutionTimeInMillis() { @@ -114,8 +114,7 @@ public interface MatcherWatchdog { } @Override - public void unregister(Matcher matcher) { - } + public void unregister(Matcher matcher) {} } class Default implements MatcherWatchdog { @@ -128,10 +127,7 @@ public interface MatcherWatchdog { private final AtomicBoolean running = new AtomicBoolean(false); final ConcurrentHashMap registry = new ConcurrentHashMap<>(); - private Default(long interval, - long maxExecutionTime, - LongSupplier relativeTimeSupplier, - BiConsumer scheduler) { + private Default(long interval, long maxExecutionTime, LongSupplier relativeTimeSupplier, BiConsumer scheduler) { this.interval = interval; this.maxExecutionTime = maxExecutionTime; this.relativeTimeSupplier = relativeTimeSupplier; diff --git a/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java b/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java index a9c2d384b62..ed48585cc12 100644 --- a/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java +++ b/libs/grok/src/test/java/org/opensearch/grok/GrokTests.java @@ -62,7 +62,6 @@ import static org.opensearch.grok.GrokCaptureType.INTEGER; import static org.opensearch.grok.GrokCaptureType.LONG; import static org.opensearch.grok.GrokCaptureType.STRING; - public class GrokTests extends OpenSearchTestCase { public void testMatchWithoutCaptures() { Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "value", logger::warn); @@ -130,8 +129,8 @@ public class GrokTests extends OpenSearchTestCase { } public void testSyslog5424Line() { - String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " + - "Hello, syslog."; + String line = "<191>1 2009-06-30T18:30:00+02:00 paxton.local grokdebug 4123 - [id1 foo=\\\"bar\\\"][id2 baz=\\\"something\\\"] " + + "Hello, syslog."; Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "%{SYSLOG5424LINE}", logger::warn); assertCaptureConfig( grok, @@ -186,9 +185,13 @@ public class GrokTests extends OpenSearchTestCase { } public void testUnicodeSyslog() { - Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " + - "%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " + - "%{GREEDYDATA:syslog_message}", logger::warn); + Grok grok = new Grok( + Grok.BUILTIN_PATTERNS, + "<%{POSINT:syslog_pri}>%{SPACE}%{SYSLOGTIMESTAMP:syslog_timestamp} " + + "%{SYSLOGHOST:syslog_hostname} %{PROG:syslog_program}(:?)(?:\\[%{GREEDYDATA:syslog_pid}\\])?(:?) " + + "%{GREEDYDATA:syslog_message}", + logger::warn + ); assertCaptureConfig( grok, org.opensearch.common.collect.Map.ofEntries( @@ -200,9 +203,11 @@ public class GrokTests extends OpenSearchTestCase { org.opensearch.common.collect.Map.entry("syslog_timestamp", STRING) ) ); - Map matches = grok.captures("<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " + - "SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " + - "email@domain.no"); + Map matches = grok.captures( + "<22>Jan 4 07:50:46 mailmaster postfix/policy-spf[9454]: : " + + "SPF permerror (Junk encountered in record 'v=spf1 mx a:mail.domain.no ip4:192.168.0.4 �all'): Envelope-from: " + + "email@domain.no" + ); assertThat(matches.get("syslog_pri"), equalTo("22")); assertThat(matches.get("syslog_program"), equalTo("postfix/policy-spf")); assertThat(matches.get("tags"), nullValue()); @@ -226,21 +231,21 @@ public class GrokTests extends OpenSearchTestCase { Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "^%{TIMESTAMP_ISO8601}$", logger::warn); assertCaptureConfig(grok, org.opensearch.common.collect.Map.of()); List timeMessages = Arrays.asList( - "2001-01-01T00:00:00", - "1974-03-02T04:09:09", - "2010-05-03T08:18:18+00:00", - "2004-07-04T12:27:27-00:00", - "2001-09-05T16:36:36+0000", - "2001-11-06T20:45:45-0000", - "2001-12-07T23:54:54Z", - "2001-01-01T00:00:00.123456", - "1974-03-02T04:09:09.123456", - "2010-05-03T08:18:18.123456+00:00", - "2004-07-04T12:27:27.123456-00:00", - "2001-09-05T16:36:36.123456+0000", - "2001-11-06T20:45:45.123456-0000", - "2001-12-07T23:54:54.123456Z", - "2001-12-07T23:54:60.123456Z" // '60' second is a leap second. + "2001-01-01T00:00:00", + "1974-03-02T04:09:09", + "2010-05-03T08:18:18+00:00", + "2004-07-04T12:27:27-00:00", + "2001-09-05T16:36:36+0000", + "2001-11-06T20:45:45-0000", + "2001-12-07T23:54:54Z", + "2001-01-01T00:00:00.123456", + "1974-03-02T04:09:09.123456", + "2010-05-03T08:18:18.123456+00:00", + "2004-07-04T12:27:27.123456-00:00", + "2001-09-05T16:36:36.123456+0000", + "2001-11-06T20:45:45.123456-0000", + "2001-12-07T23:54:54.123456Z", + "2001-12-07T23:54:60.123456Z" // '60' second is a leap second. ); for (String msg : timeMessages) { assertThat(grok.match(msg), is(true)); @@ -251,28 +256,28 @@ public class GrokTests extends OpenSearchTestCase { Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "^%{TIMESTAMP_ISO8601}$", logger::warn); assertCaptureConfig(grok, org.opensearch.common.collect.Map.of()); List timeMessages = Arrays.asList( - "2001-13-01T00:00:00", // invalid month - "2001-00-01T00:00:00", // invalid month - "2001-01-00T00:00:00", // invalid day - "2001-01-32T00:00:00", // invalid day - "2001-01-aT00:00:00", // invalid day - "2001-01-1aT00:00:00", // invalid day - "2001-01-01Ta0:00:00", // invalid hour - "2001-01-01T0:00:00", // invalid hour - "2001-01-01T25:00:00", // invalid hour - "2001-01-01T01:60:00", // invalid minute - "2001-01-01T00:aa:00", // invalid minute - "2001-01-01T00:00:aa", // invalid second - "2001-01-01T00:00:-1", // invalid second - "2001-01-01T00:00:61", // invalid second - "2001-01-01T00:00:00A", // invalid timezone - "2001-01-01T00:00:00+", // invalid timezone - "2001-01-01T00:00:00+25", // invalid timezone - "2001-01-01T00:00:00+2500", // invalid timezone - "2001-01-01T00:00:00+25:00", // invalid timezone - "2001-01-01T00:00:00-25", // invalid timezone - "2001-01-01T00:00:00-2500", // invalid timezone - "2001-01-01T00:00:00-00:61" // invalid timezone + "2001-13-01T00:00:00", // invalid month + "2001-00-01T00:00:00", // invalid month + "2001-01-00T00:00:00", // invalid day + "2001-01-32T00:00:00", // invalid day + "2001-01-aT00:00:00", // invalid day + "2001-01-1aT00:00:00", // invalid day + "2001-01-01Ta0:00:00", // invalid hour + "2001-01-01T0:00:00", // invalid hour + "2001-01-01T25:00:00", // invalid hour + "2001-01-01T01:60:00", // invalid minute + "2001-01-01T00:aa:00", // invalid minute + "2001-01-01T00:00:aa", // invalid second + "2001-01-01T00:00:-1", // invalid second + "2001-01-01T00:00:61", // invalid second + "2001-01-01T00:00:00A", // invalid timezone + "2001-01-01T00:00:00+", // invalid timezone + "2001-01-01T00:00:00+25", // invalid timezone + "2001-01-01T00:00:00+2500", // invalid timezone + "2001-01-01T00:00:00+25:00", // invalid timezone + "2001-01-01T00:00:00-25", // invalid timezone + "2001-01-01T00:00:00-2500", // invalid timezone + "2001-01-01T00:00:00-00:61" // invalid timezone ); for (String msg : timeMessages) { assertThat(grok.match(msg), is(false)); @@ -344,8 +349,10 @@ public class GrokTests extends OpenSearchTestCase { String pattern = "%{NAME1}"; new Grok(bank, pattern, false, logger::warn); }); - assertEquals("circular reference in pattern [NAME3][!!!%{NAME1}!!!] back to pattern [NAME1] via patterns [NAME1=>NAME2]", - e.getMessage()); + assertEquals( + "circular reference in pattern [NAME3][!!!%{NAME1}!!!] back to pattern [NAME1] via patterns [NAME1=>NAME2]", + e.getMessage() + ); e = expectThrows(IllegalArgumentException.class, () -> { Map bank = new TreeMap<>(); @@ -354,8 +361,7 @@ public class GrokTests extends OpenSearchTestCase { String pattern = "%{NAME1}"; new Grok(bank, pattern, false, logger::warn); }); - assertEquals("circular reference in pattern [NAME2][!!!%{NAME2}!!!]", - e.getMessage()); + assertEquals("circular reference in pattern [NAME2][!!!%{NAME2}!!!]", e.getMessage()); e = expectThrows(IllegalArgumentException.class, () -> { Map bank = new TreeMap<>(); @@ -365,10 +371,12 @@ public class GrokTests extends OpenSearchTestCase { bank.put("NAME4", "!!!%{NAME5}!!!"); bank.put("NAME5", "!!!%{NAME1}!!!"); String pattern = "%{NAME1}"; - new Grok(bank, pattern, false, logger::warn ); + new Grok(bank, pattern, false, logger::warn); }); - assertEquals("circular reference in pattern [NAME5][!!!%{NAME1}!!!] back to pattern [NAME1] " + - "via patterns [NAME1=>NAME2=>NAME3=>NAME4]", e.getMessage()); + assertEquals( + "circular reference in pattern [NAME5][!!!%{NAME1}!!!] back to pattern [NAME1] " + "via patterns [NAME1=>NAME2=>NAME3=>NAME4]", + e.getMessage() + ); } public void testMalformedPattern() { @@ -453,7 +461,7 @@ public class GrokTests extends OpenSearchTestCase { double[] rating = new double[1]; GrokCaptureExtracter ratingExtracter = namedConfig(g, "rating").nativeExtracter(new ThrowingNativeExtracterMap() { - public GrokCaptureExtracter forDouble(java.util.function.Function buildExtracter) { + public GrokCaptureExtracter forDouble(java.util.function.Function buildExtracter) { return buildExtracter.apply(d -> rating[0] = d); } }); @@ -491,9 +499,9 @@ public class GrokTests extends OpenSearchTestCase { } public void testApacheLog() { - String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + - "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; + String logLine = "31.184.238.164 - - [24/Jul/2014:05:35:37 +0530] \"GET /logs/access.log HTTP/1.0\" 200 69849 " + + "\"http://8rursodiol.enjin.com\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/30.0.1599.12785 YaBrowser/13.12.1599.12785 Safari/537.36\" \"www.dlwindianrailways.com\""; Grok grok = new Grok(Grok.BUILTIN_PATTERNS, "%{COMBINEDAPACHELOG}", logger::warn); assertCaptureConfig( grok, @@ -525,15 +533,21 @@ public class GrokTests extends OpenSearchTestCase { assertEquals("69849", matches.get("bytes")); assertEquals("\"http://8rursodiol.enjin.com\"", matches.get("referrer")); assertEquals(null, matches.get("port")); - assertEquals("\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 " + - "YaBrowser/13.12.1599.12785 Safari/537.36\"", matches.get("agent")); + assertEquals( + "\"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.12785 " + + "YaBrowser/13.12.1599.12785 Safari/537.36\"", + matches.get("agent") + ); } public void testComplete() { Map bank = new HashMap<>(); bank.put("MONTHDAY", "(?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9])"); - bank.put("MONTH", "\\b(?:Jan(?:uary|uar)?|Feb(?:ruary|ruar)?|M(?:a|ä)?r(?:ch|z)?|Apr(?:il)?|Ma(?:y|i)?|Jun(?:e|i)" + - "?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|O(?:c|k)?t(?:ober)?|Nov(?:ember)?|De(?:c|z)(?:ember)?)\\b"); + bank.put( + "MONTH", + "\\b(?:Jan(?:uary|uar)?|Feb(?:ruary|ruar)?|M(?:a|ä)?r(?:ch|z)?|Apr(?:il)?|Ma(?:y|i)?|Jun(?:e|i)" + + "?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|O(?:c|k)?t(?:ober)?|Nov(?:ember)?|De(?:c|z)(?:ember)?)\\b" + ); bank.put("MINUTE", "(?:[0-5][0-9])"); bank.put("YEAR", "(?>\\d\\d){1,2}"); bank.put("HOUR", "(?:2[0123]|[01]?[0-9])"); @@ -544,19 +558,25 @@ public class GrokTests extends OpenSearchTestCase { bank.put("WORD", "\\b\\w+\\b"); bank.put("BASE10NUM", "(?[+-]?(?:(?:[0-9]+(?:\\.[0-9]+)?)|(?:\\.[0-9]+)))"); bank.put("NUMBER", "(?:%{BASE10NUM})"); - bank.put("IPV6", "((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]" + - "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4})" + - "{1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:)" + - "{4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\" + - "d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]" + - "\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4})" + - "{1,5})" + - "|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))" + - "|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)" + - "(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}" + - ":((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?"); - bank.put("IPV4", "(?(?\"(?>\\\\.|[^\\\\\"]+)+\"|\"\"|(?>'(?>\\\\.|[^\\\\']+)+')|''|(?>`(?>\\\\.|[^\\\\`]+)+`)|``))"); - String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" + - "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " + - "\"Mozilla" + - "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; - String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " + - "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; + String text = "83.149.9.216 - - [19/Jul/2015:08:13:42 +0000] \"GET /presentations/logstash-monitorama-2013/images/" + + "kibana-dashboard3.png HTTP/1.1\" 200 171717 \"http://semicomplete.com/presentations/logstash-monitorama-2013/\" " + + "\"Mozilla" + + "/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36\""; + String pattern = "%{IPORHOST:clientip} %{USER:ident} %{USER:auth} \\[%{HTTPDATE:timestamp}\\] \"%{WORD:verb} %{DATA:request} " + + "HTTP/%{NUMBER:httpversion}\" %{NUMBER:response:int} (?:-|%{NUMBER:bytes:int}) %{QS:referrer} %{QS:agent}"; Grok grok = new Grok(bank, pattern, logger::warn); assertCaptureConfig( @@ -600,8 +620,11 @@ public class GrokTests extends OpenSearchTestCase { expected.put("response", 200); expected.put("bytes", 171717); expected.put("referrer", "\"http://semicomplete.com/presentations/logstash-monitorama-2013/\""); - expected.put("agent", "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) " + - "Chrome/32.0.1700.77 Safari/537.36\""); + expected.put( + "agent", + "\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) " + + "Chrome/32.0.1700.77 Safari/537.36\"" + ); Map actual = grok.captures(text); @@ -629,10 +652,10 @@ public class GrokTests extends OpenSearchTestCase { public void testExponentialExpressions() { AtomicBoolean run = new AtomicBoolean(true); // to avoid a lingering thread when test has completed - String grokPattern = "Bonsuche mit folgender Anfrage: Belegart->\\[%{WORD:param2},(?(\\s*%{NOTSPACE})*)\\] " + - "Zustand->ABGESCHLOSSEN Kassennummer->%{WORD:param9} Bonnummer->%{WORD:param10} Datum->%{DATESTAMP_OTHER:param11}"; - String logLine = "Bonsuche mit folgender Anfrage: Belegart->[EINGESCHRAENKTER_VERKAUF, VERKAUF, NACHERFASSUNG] " + - "Zustand->ABGESCHLOSSEN Kassennummer->2 Bonnummer->6362 Datum->Mon Jan 08 00:00:00 UTC 2018"; + String grokPattern = "Bonsuche mit folgender Anfrage: Belegart->\\[%{WORD:param2},(?(\\s*%{NOTSPACE})*)\\] " + + "Zustand->ABGESCHLOSSEN Kassennummer->%{WORD:param9} Bonnummer->%{WORD:param10} Datum->%{DATESTAMP_OTHER:param11}"; + String logLine = "Bonsuche mit folgender Anfrage: Belegart->[EINGESCHRAENKTER_VERKAUF, VERKAUF, NACHERFASSUNG] " + + "Zustand->ABGESCHLOSSEN Kassennummer->2 Bonnummer->6362 Datum->Mon Jan 08 00:00:00 UTC 2018"; BiConsumer scheduler = (delay, command) -> { try { Thread.sleep(delay); @@ -646,8 +669,12 @@ public class GrokTests extends OpenSearchTestCase { }); t.start(); }; - Grok grok = new Grok(Grok.BUILTIN_PATTERNS, grokPattern, MatcherWatchdog.newInstance(10, 200, System::currentTimeMillis, scheduler), - logger::warn); + Grok grok = new Grok( + Grok.BUILTIN_PATTERNS, + grokPattern, + MatcherWatchdog.newInstance(10, 200, System::currentTimeMillis, scheduler), + logger::warn + ); Exception e = expectThrows(RuntimeException.class, () -> grok.captures(logLine)); run.set(false); assertThat(e.getMessage(), equalTo("grok pattern matching was interrupted after [200] ms")); @@ -702,11 +729,11 @@ public class GrokTests extends OpenSearchTestCase { assertThat(grok.match("Test Class.java"), is(true)); } - public void testLogCallBack(){ + public void testLogCallBack() { AtomicReference message = new AtomicReference<>(); Grok grok = new Grok(Grok.BUILTIN_PATTERNS, ".*\\[.*%{SPACE}*\\].*", message::set); grok.match("[foo]"); - //this message comes from Joni, so updates to Joni may change the expectation + // this message comes from Joni, so updates to Joni may change the expectation assertThat(message.get(), containsString("regular expression has redundant nested repeat operator")); } diff --git a/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java b/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java index c3ad1ee0133..376db6226c1 100644 --- a/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java +++ b/libs/grok/src/test/java/org/opensearch/grok/MatcherWatchdogTests.java @@ -77,7 +77,8 @@ public class MatcherWatchdogTests extends OpenSearchTestCase { watchdog.register(matcher); verify(matcher, timeout(9999).atLeastOnce()).interrupt(); interrupted.set(true); - while (run.get()) {} // wait here so that the size of the registry can be asserted + while (run.get()) { + } // wait here so that the size of the registry can be asserted watchdog.unregister(matcher); }); thread.start(); @@ -86,16 +87,18 @@ public class MatcherWatchdogTests extends OpenSearchTestCase { assertThat(registry.size(), is(1)); }); run.set(false); - assertBusy(() -> { - assertThat(registry.size(), is(0)); - }); + assertBusy(() -> { assertThat(registry.size(), is(0)); }); } public void testIdleIfNothingRegistered() throws Exception { long interval = 1L; ScheduledExecutorService threadPool = mock(ScheduledExecutorService.class); - MatcherWatchdog watchdog = MatcherWatchdog.newInstance(interval, Long.MAX_VALUE, System::currentTimeMillis, - (delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS)); + MatcherWatchdog watchdog = MatcherWatchdog.newInstance( + interval, + Long.MAX_VALUE, + System::currentTimeMillis, + (delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS) + ); // Periodic action is not scheduled because no thread is registered verifyZeroInteractions(threadPool); CompletableFuture commandFuture = new CompletableFuture<>(); @@ -103,9 +106,7 @@ public class MatcherWatchdogTests extends OpenSearchTestCase { doAnswer(invocationOnMock -> { commandFuture.complete((Runnable) invocationOnMock.getArguments()[0]); return null; - }).when(threadPool).schedule( - any(Runnable.class), eq(interval), eq(TimeUnit.MILLISECONDS) - ); + }).when(threadPool).schedule(any(Runnable.class), eq(interval), eq(TimeUnit.MILLISECONDS)); Matcher matcher = mock(Matcher.class); watchdog.register(matcher); // Registering the first thread should have caused the command to get scheduled again diff --git a/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java b/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java index 03a05d4b6d6..14e027195bd 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java +++ b/libs/nio/src/main/java/org/opensearch/nio/BytesChannelContext.java @@ -37,8 +37,14 @@ import java.util.function.Consumer; public class BytesChannelContext extends SocketChannelContext { - public BytesChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, NioChannelHandler handler, InboundChannelBuffer channelBuffer) { + public BytesChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + NioChannelHandler handler, + InboundChannelBuffer channelBuffer + ) { super(channel, selector, socketConfig, exceptionHandler, handler, channelBuffer); } diff --git a/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java b/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java index a6136c15d3a..b4c3f5a1471 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java +++ b/libs/nio/src/main/java/org/opensearch/nio/BytesWriteHandler.java @@ -42,8 +42,11 @@ public abstract class BytesWriteHandler implements NioChannelHandler { private static final List EMPTY_LIST = Collections.emptyList(); public WriteOperation createWriteOperation(SocketChannelContext context, Object message, BiConsumer listener) { - assert message instanceof ByteBuffer[] : "This channel only supports messages that are of type: " + ByteBuffer[].class - + ". Found type: " + message.getClass() + "."; + assert message instanceof ByteBuffer[] : "This channel only supports messages that are of type: " + + ByteBuffer[].class + + ". Found type: " + + message.getClass() + + "."; return new FlushReadyWrite(context, (ByteBuffer[]) message, listener); } diff --git a/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java b/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java index f87c19e5080..c7a0bb40b42 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java +++ b/libs/nio/src/main/java/org/opensearch/nio/ChannelFactory.java @@ -56,18 +56,43 @@ public abstract class ChannelFactory= internalIndex : "Should never have an index that is greater than the length [length=" + length + ", index=" - + internalIndex + "]"; + assert length >= internalIndex : "Should never have an index that is greater than the length [length=" + + length + + ", index=" + + internalIndex + + "]"; return internalIndex == length; } public void incrementIndex(int delta) { internalIndex += delta; - assert length >= internalIndex : "Should never increment index past length [length=" + length + ", post-increment index=" - + internalIndex + ", delta=" + delta + "]"; + assert length >= internalIndex : "Should never increment index past length [length=" + + length + + ", post-increment index=" + + internalIndex + + ", delta=" + + delta + + "]"; } public ByteBuffer[] getBuffersToWrite() { diff --git a/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java b/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java index 6429fd410a0..38d9c35a22a 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java +++ b/libs/nio/src/main/java/org/opensearch/nio/InboundChannelBuffer.java @@ -135,8 +135,9 @@ public final class InboundChannelBuffer implements AutoCloseable { */ public ByteBuffer[] sliceBuffersTo(long to) { if (to > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters to [" + to + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters to [" + to + "]" + ); } else if (to == 0) { return EMPTY_BYTE_BUFFER_ARRAY; } @@ -174,8 +175,9 @@ public final class InboundChannelBuffer implements AutoCloseable { */ public Page[] sliceAndRetainPagesTo(long to) { if (to > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters to [" + to + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters to [" + to + "]" + ); } else if (to == 0) { return EMPTY_BYTE_PAGE_ARRAY; } @@ -213,8 +215,9 @@ public final class InboundChannelBuffer implements AutoCloseable { */ public ByteBuffer[] sliceBuffersFrom(long from) { if (from > capacity) { - throw new IndexOutOfBoundsException("can't slice a channel buffer with capacity [" + capacity + - "], with slice parameters from [" + from + "]"); + throw new IndexOutOfBoundsException( + "can't slice a channel buffer with capacity [" + capacity + "], with slice parameters from [" + from + "]" + ); } else if (from == capacity) { return EMPTY_BYTE_BUFFER_ARRAY; } @@ -242,8 +245,17 @@ public final class InboundChannelBuffer implements AutoCloseable { long newIndex = delta + internalIndex; if (newIndex > capacity) { - throw new IllegalArgumentException("Cannot increment an index [" + internalIndex + "] with a delta [" + delta + - "] that will result in a new index [" + newIndex + "] that is greater than the capacity [" + capacity + "]."); + throw new IllegalArgumentException( + "Cannot increment an index [" + + internalIndex + + "] with a delta [" + + delta + + "] that will result in a new index [" + + newIndex + + "] that is greater than the capacity [" + + capacity + + "]." + ); } internalIndex = newIndex; } diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java b/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java index cdd36b3f564..a38a33182af 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioSelector.java @@ -128,8 +128,12 @@ public class NioSelector implements Closeable { } public void assertOnSelectorThread() { - assert isOnCurrentThread() : "Must be on selector thread [" + thread.get().getName() + "} to perform this operation. " + - "Currently on thread [" + Thread.currentThread().getName() + "]."; + assert isOnCurrentThread() : "Must be on selector thread [" + + thread.get().getName() + + "} to perform this operation. " + + "Currently on thread [" + + Thread.currentThread().getName() + + "]."; } /** @@ -186,10 +190,10 @@ public class NioSelector implements Closeable { try { processKey(sk); } catch (CancelledKeyException cke) { - eventHandler.genericChannelException((ChannelContext) sk.attachment(), cke); + eventHandler.genericChannelException((ChannelContext) sk.attachment(), cke); } } else { - eventHandler.genericChannelException((ChannelContext) sk.attachment(), new CancelledKeyException()); + eventHandler.genericChannelException((ChannelContext) sk.attachment(), new CancelledKeyException()); } } } @@ -210,8 +214,9 @@ public class NioSelector implements Closeable { cleanupPendingWrites(); channelsToClose.addAll(channelsToRegister); channelsToRegister.clear(); - channelsToClose.addAll(selector.keys().stream() - .map(sk -> (ChannelContext) sk.attachment()).filter(Objects::nonNull).collect(Collectors.toList())); + channelsToClose.addAll( + selector.keys().stream().map(sk -> (ChannelContext) sk.attachment()).filter(Objects::nonNull).collect(Collectors.toList()) + ); closePendingChannels(); } diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java b/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java index 72b31d8c897..47ba253a1bd 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioSelectorGroup.java @@ -57,7 +57,6 @@ import java.util.stream.Stream; */ public class NioSelectorGroup implements NioGroup { - private final List dedicatedAcceptors; private final RoundRobinSupplier acceptorSupplier; @@ -75,8 +74,11 @@ public class NioSelectorGroup implements NioGroup { * @param eventHandlerFunction function for creating event handlers * @throws IOException occurs if there is a problem while opening a java.nio.Selector */ - public NioSelectorGroup(ThreadFactory threadFactory, int selectorCount, - Function, EventHandler> eventHandlerFunction) throws IOException { + public NioSelectorGroup( + ThreadFactory threadFactory, + int selectorCount, + Function, EventHandler> eventHandlerFunction + ) throws IOException { this(null, 0, threadFactory, selectorCount, eventHandlerFunction); } @@ -92,8 +94,13 @@ public class NioSelectorGroup implements NioGroup { * @param eventHandlerFunction function for creating event handlers * @throws IOException occurs if there is a problem while opening a java.nio.Selector */ - public NioSelectorGroup(ThreadFactory acceptorThreadFactory, int dedicatedAcceptorCount, ThreadFactory selectorThreadFactory, - int selectorCount, Function, EventHandler> eventHandlerFunction) throws IOException { + public NioSelectorGroup( + ThreadFactory acceptorThreadFactory, + int dedicatedAcceptorCount, + ThreadFactory selectorThreadFactory, + int selectorCount, + Function, EventHandler> eventHandlerFunction + ) throws IOException { dedicatedAcceptors = new ArrayList<>(dedicatedAcceptorCount); selectors = new ArrayList<>(selectorCount); diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java b/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java index 38b08867d54..72724b72afc 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioServerSocketChannel.java @@ -84,9 +84,7 @@ public class NioServerSocketChannel extends NioChannel { @Override public String toString() { - return "NioServerSocketChannel{" + - "localAddress=" + getLocalAddress() + - '}'; + return "NioServerSocketChannel{" + "localAddress=" + getLocalAddress() + '}'; } private void attemptToSetLocalAddress() { diff --git a/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java b/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java index b91963141a8..233b41a0a21 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java +++ b/libs/nio/src/main/java/org/opensearch/nio/NioSocketChannel.java @@ -88,9 +88,6 @@ public class NioSocketChannel extends NioChannel { @Override public String toString() { - return "NioSocketChannel{" + - "localAddress=" + getLocalAddress() + - ", remoteAddress=" + getRemoteAddress() + - '}'; + return "NioSocketChannel{" + "localAddress=" + getLocalAddress() + ", remoteAddress=" + getRemoteAddress() + '}'; } } diff --git a/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java b/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java index 708e0057089..c054ad19537 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java +++ b/libs/nio/src/main/java/org/opensearch/nio/SelectionKeyUtils.java @@ -100,7 +100,6 @@ public final class SelectionKeyUtils { selectionKey.interestOps(selectionKey.interestOps() | SelectionKey.OP_ACCEPT); } - /** * Checks for an interest in writes for this selection key. * diff --git a/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java b/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java index d6cb708432c..898ce7e4e91 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java +++ b/libs/nio/src/main/java/org/opensearch/nio/ServerChannelContext.java @@ -58,9 +58,14 @@ public class ServerChannelContext extends ChannelContext { private final ChannelFactory channelFactory; private final CompletableContext bindContext = new CompletableContext<>(); - public ServerChannelContext(NioServerSocketChannel channel, ChannelFactory channelFactory, NioSelector selector, - Config.ServerSocket config, Consumer acceptor, - Consumer exceptionHandler) { + public ServerChannelContext( + NioServerSocketChannel channel, + ChannelFactory channelFactory, + NioSelector selector, + Config.ServerSocket config, + Consumer acceptor, + Consumer exceptionHandler + ) { super(channel.getRawChannel(), exceptionHandler); this.channel = channel; this.channelFactory = channelFactory; @@ -77,8 +82,7 @@ public class ServerChannelContext extends ChannelContext { acceptor.accept(nioChannel); } } finally { - if (acceptedChannel != null) - acceptedChannel.close(); + if (acceptedChannel != null) acceptedChannel.close(); } } diff --git a/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java b/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java index 208f0cf7ea8..9cf57910a2b 100644 --- a/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java +++ b/libs/nio/src/main/java/org/opensearch/nio/SocketChannelContext.java @@ -77,9 +77,14 @@ public abstract class SocketChannelContext extends ChannelContext private boolean socketOptionsSet; private Exception connectException; - protected SocketChannelContext(NioSocketChannel channel, NioSelector selector, Config.Socket socketConfig, - Consumer exceptionHandler, NioChannelHandler channelHandler, - InboundChannelBuffer channelBuffer) { + protected SocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Config.Socket socketConfig, + Consumer exceptionHandler, + NioChannelHandler channelHandler, + InboundChannelBuffer channelBuffer + ) { super(channel.getRawChannel(), exceptionHandler); this.selector = selector; this.channel = channel; diff --git a/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java b/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java index 7a62f2dfd7e..11a9a263680 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/BytesChannelContextTests.java @@ -130,7 +130,6 @@ public class BytesChannelContextTests extends OpenSearchTestCase { return bytes.length; }); - when(readConsumer.apply(channelBuffer)).thenReturn(0); assertEquals(messageLength, context.read()); @@ -175,7 +174,7 @@ public class BytesChannelContextTests extends OpenSearchTestCase { public void testQueuedWriteIsFlushedInFlushCall() throws Exception { assertFalse(context.readyForFlush()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); @@ -201,7 +200,7 @@ public class BytesChannelContextTests extends OpenSearchTestCase { assertTrue(context.readyForFlush()); when(flushOperation.isFullyFlushed()).thenReturn(false); - when(flushOperation.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); + when(flushOperation.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); context.flushChannel(); verify(listener, times(0)).accept(null, null); @@ -215,8 +214,8 @@ public class BytesChannelContextTests extends OpenSearchTestCase { BiConsumer listener2 = mock(BiConsumer.class); FlushReadyWrite flushOperation1 = mock(FlushReadyWrite.class); FlushReadyWrite flushOperation2 = mock(FlushReadyWrite.class); - when(flushOperation1.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); - when(flushOperation2.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] {ByteBuffer.allocate(3)}); + when(flushOperation1.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); + when(flushOperation2.getBuffersToWrite(anyInt())).thenReturn(new ByteBuffer[] { ByteBuffer.allocate(3) }); when(flushOperation1.getListener()).thenReturn(listener); when(flushOperation2.getListener()).thenReturn(listener2); @@ -244,7 +243,7 @@ public class BytesChannelContextTests extends OpenSearchTestCase { public void testWhenIOExceptionThrownListenerIsCalled() throws IOException { assertFalse(context.readyForFlush()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); @@ -261,7 +260,7 @@ public class BytesChannelContextTests extends OpenSearchTestCase { } public void testWriteIOExceptionMeansChannelReadyToClose() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushReadyWrite flushOperation = mock(FlushReadyWrite.class); context.queueWriteOperation(flushOperation); diff --git a/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java b/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java index 542512faccb..01837cd2a06 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/ChannelFactoryTests.java @@ -152,8 +152,11 @@ public class ChannelFactoryTests extends OpenSearchTestCase { } @Override - public NioServerSocketChannel createServerChannel(NioSelector selector, ServerSocketChannel channel, - Config.ServerSocket socketConfig) { + public NioServerSocketChannel createServerChannel( + NioSelector selector, + ServerSocketChannel channel, + Config.ServerSocket socketConfig + ) { return new NioServerSocketChannel(channel); } diff --git a/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java b/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java index c84381b8ac0..8ee1439cc63 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/EventHandlerTests.java @@ -234,7 +234,6 @@ public class EventHandlerTests extends OpenSearchTestCase { NioSocketChannel channel = mock(NioSocketChannel.class); when(channel.getContext()).thenReturn(context); - assertEquals(SelectionKey.OP_READ | SelectionKey.OP_WRITE, key.interestOps()); handler.postHandling(context); assertEquals(SelectionKey.OP_READ, key.interestOps()); @@ -254,9 +253,12 @@ public class EventHandlerTests extends OpenSearchTestCase { private class DoNotRegisterSocketContext extends BytesChannelContext { - - DoNotRegisterSocketContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler handler) { + DoNotRegisterSocketContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler handler + ) { super(channel, selector, getSocketConfig(), exceptionHandler, handler, InboundChannelBuffer.allocatingInstance()); } @@ -270,7 +272,6 @@ public class EventHandlerTests extends OpenSearchTestCase { private class DoNotRegisterServerContext extends ServerChannelContext { - @SuppressWarnings("unchecked") DoNotRegisterServerContext(NioServerSocketChannel channel, NioSelector selector, Consumer acceptor) { super(channel, channelFactory, selector, getServerSocketConfig(), acceptor, mock(Consumer.class)); @@ -289,7 +290,17 @@ public class EventHandlerTests extends OpenSearchTestCase { } private static Config.Socket getSocketConfig() { - return new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, mock(InetSocketAddress.class), - randomBoolean()); + return new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + randomBoolean() + ); } } diff --git a/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java b/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java index 276381401cd..2e19cab2cbd 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/FlushOperationTests.java @@ -52,7 +52,7 @@ public class FlushOperationTests extends OpenSearchTestCase { } public void testFullyFlushedMarker() { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(10); @@ -61,7 +61,7 @@ public class FlushOperationTests extends OpenSearchTestCase { } public void testPartiallyFlushedMarker() { - ByteBuffer[] buffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(5); @@ -70,7 +70,7 @@ public class FlushOperationTests extends OpenSearchTestCase { } public void testMultipleFlushesWithCompositeBuffer() throws IOException { - ByteBuffer[] buffers = {ByteBuffer.allocate(10), ByteBuffer.allocate(15), ByteBuffer.allocate(3)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10), ByteBuffer.allocate(15), ByteBuffer.allocate(3) }; FlushOperation writeOp = new FlushOperation(buffers, listener); writeOp.incrementIndex(5); diff --git a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java index e04919db91d..154bc536960 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorGroupTests.java @@ -51,8 +51,13 @@ public class NioSelectorGroupTests extends OpenSearchTestCase { @SuppressWarnings("unchecked") public void setUp() throws Exception { super.setUp(); - nioGroup = new NioSelectorGroup(daemonThreadFactory(Settings.EMPTY, "acceptor"), 1, - daemonThreadFactory(Settings.EMPTY, "selector"), 1, (s) -> new EventHandler(mock(Consumer.class), s)); + nioGroup = new NioSelectorGroup( + daemonThreadFactory(Settings.EMPTY, "acceptor"), + 1, + daemonThreadFactory(Settings.EMPTY, "selector"), + 1, + (s) -> new EventHandler(mock(Consumer.class), s) + ); } @Override @@ -71,11 +76,15 @@ public class NioSelectorGroupTests extends OpenSearchTestCase { public void testCannotOperateAfterClose() throws IOException { nioGroup.close(); - IllegalStateException ise = expectThrows(IllegalStateException.class, - () -> nioGroup.bindServerChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class))); + IllegalStateException ise = expectThrows( + IllegalStateException.class, + () -> nioGroup.bindServerChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class)) + ); assertEquals("NioGroup is closed.", ise.getMessage()); - ise = expectThrows(IllegalStateException.class, - () -> nioGroup.openChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class))); + ise = expectThrows( + IllegalStateException.class, + () -> nioGroup.openChannel(mock(InetSocketAddress.class), mock(ChannelFactory.class)) + ); assertEquals("NioGroup is closed.", ise.getMessage()); } @@ -87,9 +96,13 @@ public class NioSelectorGroupTests extends OpenSearchTestCase { @SuppressWarnings("unchecked") public void testExceptionAtStartIsHandled() throws IOException { RuntimeException ex = new RuntimeException(); - CheckedRunnable ctor = () -> new NioSelectorGroup(r -> {throw ex;}, 1, + CheckedRunnable ctor = () -> new NioSelectorGroup( + r -> { throw ex; }, + 1, daemonThreadFactory(Settings.EMPTY, "selector"), - 1, (s) -> new EventHandler(mock(Consumer.class), s)); + 1, + (s) -> new EventHandler(mock(Consumer.class), s) + ); RuntimeException runtimeException = expectThrows(RuntimeException.class, ctor::run); assertSame(ex, runtimeException); // ctor starts threads. So we are testing that a failure to construct will stop threads. Our thread diff --git a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java index 078c56fc94f..d4d63091d21 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/NioSelectorTests.java @@ -72,7 +72,7 @@ public class NioSelectorTests extends OpenSearchTestCase { private SocketChannelContext channelContext; private ServerChannelContext serverChannelContext; private BiConsumer listener; - private ByteBuffer[] buffers = {ByteBuffer.allocate(1)}; + private ByteBuffer[] buffers = { ByteBuffer.allocate(1) }; private Selector rawSelector; @Before @@ -107,7 +107,7 @@ public class NioSelectorTests extends OpenSearchTestCase { }).when(eventHandler).handleTask(any()); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testQueueChannelForClosed() throws IOException { NioChannel channel = mock(NioChannel.class); ChannelContext context = mock(ChannelContext.class); @@ -121,7 +121,7 @@ public class NioSelectorTests extends OpenSearchTestCase { verify(eventHandler).handleClose(context); } - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) public void testCloseException() throws IOException, InterruptedException { IOException ioException = new IOException(); NioChannel channel = mock(NioChannel.class); @@ -152,9 +152,7 @@ public class NioSelectorTests extends OpenSearchTestCase { public void testTaskExceptionsAreHandled() { RuntimeException taskException = new RuntimeException(); long nanoTime = System.nanoTime() - 1; - Runnable task = () -> { - throw taskException; - }; + Runnable task = () -> { throw taskException; }; selector.getTaskScheduler().scheduleAtRelativeTime(task, nanoTime); doAnswer((a) -> { @@ -168,8 +166,7 @@ public class NioSelectorTests extends OpenSearchTestCase { public void testDefaultSelectorTimeoutIsUsedIfNoTaskSooner() throws IOException { long delay = new TimeValue(15, TimeUnit.MINUTES).nanos(); - selector.getTaskScheduler().scheduleAtRelativeTime(() -> { - }, System.nanoTime() + delay); + selector.getTaskScheduler().scheduleAtRelativeTime(() -> {}, System.nanoTime() + delay); selector.singleLoop(); verify(rawSelector).select(300); @@ -181,8 +178,7 @@ public class NioSelectorTests extends OpenSearchTestCase { assertBusy(() -> { ArgumentCaptor captor = ArgumentCaptor.forClass(Long.class); long delay = new TimeValue(50, TimeUnit.MILLISECONDS).nanos(); - selector.getTaskScheduler().scheduleAtRelativeTime(() -> { - }, System.nanoTime() + delay); + selector.getTaskScheduler().scheduleAtRelativeTime(() -> {}, System.nanoTime() + delay); selector.singleLoop(); verify(rawSelector).select(captor.capture()); assertTrue(captor.getValue() > 0); diff --git a/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java b/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java index 79b5a635042..093bd19ac40 100644 --- a/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java +++ b/libs/nio/src/test/java/org/opensearch/nio/SocketChannelContextTests.java @@ -136,8 +136,18 @@ public class SocketChannelContextTests extends OpenSearchTestCase { boolean tcpReuseAddress = randomBoolean(); int tcpSendBufferSize = randomIntBetween(1000, 2000); int tcpReceiveBufferSize = randomIntBetween(1000, 2000); - config = new Config.Socket(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, tcpReuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize, address, isAccepted); + config = new Config.Socket( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + tcpReuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize, + address, + isAccepted + ); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config); context.register(); @@ -205,8 +215,18 @@ public class SocketChannelContextTests extends OpenSearchTestCase { boolean tcpReuseAddress = randomBoolean(); int tcpSendBufferSize = randomIntBetween(1000, 2000); int tcpReceiveBufferSize = randomIntBetween(1000, 2000); - config = new Config.Socket(tcpNoDelay, tcpKeepAlive, tcpKeepIdle, tcpKeepInterval, tcpKeepCount, tcpReuseAddress, tcpSendBufferSize, - tcpReceiveBufferSize, address, false); + config = new Config.Socket( + tcpNoDelay, + tcpKeepAlive, + tcpKeepIdle, + tcpKeepInterval, + tcpKeepCount, + tcpReuseAddress, + tcpSendBufferSize, + tcpReceiveBufferSize, + address, + false + ); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); TestSocketChannelContext context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, buffer, config); doThrow(new SocketException()).doNothing().when(rawSocket).setReuseAddress(tcpReuseAddress); @@ -229,7 +249,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { public void testWriteFailsIfClosing() { context.closeChannel(); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; context.sendMessage(buffers, listener); verify(listener).accept(isNull(Void.class), any(ClosedChannelException.class)); @@ -240,7 +260,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { when(selector.isOnCurrentThread()).thenReturn(false); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; WriteOperation writeOperation = mock(WriteOperation.class); when(handler.createWriteOperation(context, buffers, listener)).thenReturn(writeOperation); context.sendMessage(buffers, listener); @@ -254,7 +274,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { public void testSendMessageFromSameThreadIsQueuedInChannel() { ArgumentCaptor writeOpCaptor = ArgumentCaptor.forClass(WriteOperation.class); - ByteBuffer[] buffers = {ByteBuffer.wrap(createMessage(10))}; + ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) }; WriteOperation writeOperation = mock(WriteOperation.class); when(handler.createWriteOperation(context, buffers, listener)).thenReturn(writeOperation); context.sendMessage(buffers, listener); @@ -268,7 +288,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { public void testWriteIsQueuedInChannel() { assertFalse(context.readyForFlush()); - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; FlushReadyWrite writeOperation = new FlushReadyWrite(context, buffer, listener); when(handler.writeToBytes(writeOperation)).thenReturn(Collections.singletonList(writeOperation)); context.queueWriteOperation(writeOperation); @@ -284,7 +304,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { assertTrue(context.readyForFlush()); } - @SuppressWarnings({"unchecked", "varargs"}) + @SuppressWarnings({ "unchecked", "varargs" }) public void testFlushOpsClearedOnClose() throws Exception { try (SocketChannel realChannel = SocketChannel.open()) { when(channel.getRawChannel()).thenReturn(realChannel); @@ -293,11 +313,12 @@ public class SocketChannelContextTests extends OpenSearchTestCase { assertFalse(context.readyForFlush()); - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; WriteOperation writeOperation = mock(WriteOperation.class); BiConsumer listener2 = mock(BiConsumer.class); - when(handler.writeToBytes(writeOperation)).thenReturn(Arrays.asList(new FlushOperation(buffer, listener), - new FlushOperation(buffer, listener2))); + when(handler.writeToBytes(writeOperation)).thenReturn( + Arrays.asList(new FlushOperation(buffer, listener), new FlushOperation(buffer, listener2)) + ); context.queueWriteOperation(writeOperation); assertTrue(context.readyForFlush()); @@ -312,21 +333,21 @@ public class SocketChannelContextTests extends OpenSearchTestCase { } } - @SuppressWarnings({"unchecked", "varargs"}) + @SuppressWarnings({ "unchecked", "varargs" }) public void testWillPollForFlushOpsToClose() throws Exception { try (SocketChannel realChannel = SocketChannel.open()) { when(channel.getRawChannel()).thenReturn(realChannel); InboundChannelBuffer channelBuffer = InboundChannelBuffer.allocatingInstance(); context = new TestSocketChannelContext(channel, selector, exceptionHandler, handler, channelBuffer); - - ByteBuffer[] buffer = {ByteBuffer.allocate(10)}; + ByteBuffer[] buffer = { ByteBuffer.allocate(10) }; BiConsumer listener2 = mock(BiConsumer.class); assertFalse(context.readyForFlush()); when(channel.isOpen()).thenReturn(true); - when(handler.pollFlushOperations()).thenReturn(Arrays.asList(new FlushOperation(buffer, listener), - new FlushOperation(buffer, listener2))); + when(handler.pollFlushOperations()).thenReturn( + Arrays.asList(new FlushOperation(buffer, listener), new FlushOperation(buffer, listener2)) + ); context.closeFromSelector(); verify(selector, times(1)).executeFailedListener(same(listener), any(ClosedChannelException.class)); @@ -341,8 +362,14 @@ public class SocketChannelContextTests extends OpenSearchTestCase { when(channel.getRawChannel()).thenReturn(realChannel); when(channel.isOpen()).thenReturn(true); InboundChannelBuffer buffer = InboundChannelBuffer.allocatingInstance(); - BytesChannelContext context = new BytesChannelContext(channel, selector, mock(Config.Socket.class), exceptionHandler, handler, - buffer); + BytesChannelContext context = new BytesChannelContext( + channel, + selector, + mock(Config.Socket.class), + exceptionHandler, + handler, + buffer + ); context.closeFromSelector(); verify(handler).close(); } @@ -371,7 +398,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { assertEquals(ioBuffer.capacity(), channelBuffer.getIndex()); } - public void testReadToChannelBufferHandlesIOException() throws IOException { + public void testReadToChannelBufferHandlesIOException() throws IOException { when(rawChannel.read(any(ByteBuffer.class))).thenThrow(new IOException()); InboundChannelBuffer channelBuffer = InboundChannelBuffer.allocatingInstance(); @@ -392,7 +419,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { public void testFlushBuffersHandlesZeroFlush() throws IOException { when(rawChannel.write(any(ByteBuffer.class))).thenAnswer(consumeBufferAnswer(0)); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertEquals(2, flushOperation.getBuffersToWrite().length); @@ -409,7 +436,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertEquals(1, flushOperation.getBuffersToWrite().length); @@ -426,7 +453,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); context.flushToChannel(flushOperation); assertTrue(flushOperation.isFullyFlushed()); @@ -435,7 +462,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { public void testFlushBuffersHandlesIOException() throws IOException { when(rawChannel.write(any(ByteBuffer.class))).thenThrow(new IOException()); - ByteBuffer[] buffers = {ByteBuffer.allocate(10), ByteBuffer.allocate(10)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(10), ByteBuffer.allocate(10) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); expectThrows(IOException.class, () -> context.flushToChannel(flushOperation)); assertTrue(context.closeNow()); @@ -451,7 +478,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { } }); - ByteBuffer[] buffers = {ByteBuffer.allocate(1023), ByteBuffer.allocate(1023)}; + ByteBuffer[] buffers = { ByteBuffer.allocate(1023), ByteBuffer.allocate(1023) }; FlushOperation flushOperation = new FlushOperation(buffers, listener); expectThrows(IOException.class, () -> context.flushToChannel(flushOperation)); assertTrue(context.closeNow()); @@ -460,19 +487,40 @@ public class SocketChannelContextTests extends OpenSearchTestCase { } private static Config.Socket getSocketConfig() { - return new Config.Socket(randomBoolean(), randomBoolean(), -1, -1, -1, randomBoolean(), -1, -1, mock(InetSocketAddress.class), - randomBoolean()); + return new Config.Socket( + randomBoolean(), + randomBoolean(), + -1, + -1, + -1, + randomBoolean(), + -1, + -1, + mock(InetSocketAddress.class), + randomBoolean() + ); } private static class TestSocketChannelContext extends SocketChannelContext { - private TestSocketChannelContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer) { + private TestSocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler readWriteHandler, + InboundChannelBuffer channelBuffer + ) { this(channel, selector, exceptionHandler, readWriteHandler, channelBuffer, getSocketConfig()); } - private TestSocketChannelContext(NioSocketChannel channel, NioSelector selector, Consumer exceptionHandler, - NioChannelHandler readWriteHandler, InboundChannelBuffer channelBuffer, Config.Socket config) { + private TestSocketChannelContext( + NioSocketChannel channel, + NioSelector selector, + Consumer exceptionHandler, + NioChannelHandler readWriteHandler, + InboundChannelBuffer channelBuffer, + Config.Socket config + ) { super(channel, selector, config, exceptionHandler, readWriteHandler, channelBuffer); } @@ -484,7 +532,7 @@ public class SocketChannelContextTests extends OpenSearchTestCase { @Override public void flushChannel() throws IOException { - ByteBuffer[] byteBuffers = {ByteBuffer.allocate(10)}; + ByteBuffer[] byteBuffers = { ByteBuffer.allocate(10) }; flushToChannel(new FlushOperation(byteBuffers, (v, e) -> {})); } diff --git a/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java b/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java index 25b1f92b8da..4a200a5dfa9 100644 --- a/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java +++ b/libs/plugin-classloader/src/main/java/org/opensearch/plugins/ExtendedPluginsClassLoader.java @@ -66,7 +66,8 @@ public class ExtendedPluginsClassLoader extends ClassLoader { * Return a new classloader across the parent and extended loaders. */ public static ExtendedPluginsClassLoader create(ClassLoader parent, List extendedLoaders) { - return AccessController.doPrivileged((PrivilegedAction) - () -> new ExtendedPluginsClassLoader(parent, extendedLoaders)); + return AccessController.doPrivileged( + (PrivilegedAction) () -> new ExtendedPluginsClassLoader(parent, extendedLoaders) + ); } } diff --git a/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java b/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java index ddb5f824781..08f1efd4dac 100644 --- a/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java +++ b/libs/secure-sm/src/main/java/org/opensearch/secure_sm/SecureSM.java @@ -106,10 +106,10 @@ public class SecureSM extends SecurityManager { *
  • org.eclipse.internal.junit.runner.
  • *
  • com.intellij.rt.execution.junit.
  • * - * - * For testing purposes, the security manager grants network permissions "connect, accept" + * + * For testing purposes, the security manager grants network permissions "connect, accept" * to following classes, granted they only access local network interfaces. - * + * *
      *
    • sun.net.httpserver.ServerImpl
    • *
    • java.net.ServerSocket"
    • @@ -121,31 +121,26 @@ public class SecureSM extends SecurityManager { public static SecureSM createTestSecureSM(final Set trustedHosts) { return new SecureSM(TEST_RUNNER_PACKAGES) { // Trust these callers inside the test suite only - final String[] TRUSTED_CALLERS = new String[] { - "sun.net.httpserver.ServerImpl", - "java.net.ServerSocket", - "java.net.Socket" - }; - + final String[] TRUSTED_CALLERS = new String[] { "sun.net.httpserver.ServerImpl", "java.net.ServerSocket", "java.net.Socket" }; + @Override public void checkConnect(String host, int port) { - // Allow to connect from selected trusted classes to local addresses only + // Allow to connect from selected trusted classes to local addresses only if (!hasTrustedCallerChain() || !trustedHosts.contains(host)) { super.checkConnect(host, port); } } - + @Override public void checkAccept(String host, int port) { - // Allow to accept connections from selected trusted classes to local addresses only + // Allow to accept connections from selected trusted classes to local addresses only if (!hasTrustedCallerChain() || !trustedHosts.contains(host)) { super.checkAccept(host, port); } } - + private boolean hasTrustedCallerChain() { - return Arrays - .stream(getClassContext()) + return Arrays.stream(getClassContext()) .anyMatch(c -> Arrays.stream(TRUSTED_CALLERS).anyMatch(t -> c.getName().startsWith(t))); } }; @@ -161,8 +156,7 @@ public class SecureSM extends SecurityManager { // intellij test runner (before IDEA version 2019.3) "com\\.intellij\\.rt\\.execution\\.junit\\..*", // intellij test runner (since IDEA version 2019.3) - "com\\.intellij\\.rt\\.junit\\..*" - }; + "com\\.intellij\\.rt\\.junit\\..*" }; // java.security.debug support private static final boolean DEBUG = AccessController.doPrivileged(new PrivilegedAction() { @@ -270,15 +264,12 @@ public class SecureSM extends SecurityManager { AccessController.doPrivileged(new PrivilegedAction() { @Override public Void run() { - final String systemClassName = System.class.getName(), - runtimeClassName = Runtime.class.getName(); + final String systemClassName = System.class.getName(), runtimeClassName = Runtime.class.getName(); String exitMethodHit = null; for (final StackTraceElement se : Thread.currentThread().getStackTrace()) { final String className = se.getClassName(), methodName = se.getMethodName(); - if ( - ("exit".equals(methodName) || "halt".equals(methodName)) && - (systemClassName.equals(className) || runtimeClassName.equals(className)) - ) { + if (("exit".equals(methodName) || "halt".equals(methodName)) + && (systemClassName.equals(className) || runtimeClassName.equals(className))) { exitMethodHit = className + '#' + methodName + '(' + status + ')'; continue; } diff --git a/libs/secure-sm/src/test/java/org/opensearch/secure_sm/SecureSMTests.java b/libs/secure-sm/src/test/java/org/opensearch/secure_sm/SecureSMTests.java index d1fa607429d..8f184328957 100644 --- a/libs/secure-sm/src/test/java/org/opensearch/secure_sm/SecureSMTests.java +++ b/libs/secure-sm/src/test/java/org/opensearch/secure_sm/SecureSMTests.java @@ -74,8 +74,8 @@ public class SecureSMTests extends TestCase { assertTrue(SecureSM.classCanExit("com.carrotsearch.ant.tasks.junit4.slave.JvmExit", SecureSM.TEST_RUNNER_PACKAGES)); assertTrue(SecureSM.classCanExit("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner", SecureSM.TEST_RUNNER_PACKAGES)); assertTrue(SecureSM.classCanExit("com.intellij.rt.execution.junit.JUnitStarter", SecureSM.TEST_RUNNER_PACKAGES)); - assertTrue(SecureSM.classCanExit("org.opensearch.Foo", new String[]{"org.opensearch.Foo"})); - assertFalse(SecureSM.classCanExit("org.opensearch.Foo", new String[]{"org.opensearch.Bar"})); + assertTrue(SecureSM.classCanExit("org.opensearch.Foo", new String[] { "org.opensearch.Foo" })); + assertFalse(SecureSM.classCanExit("org.opensearch.Foo", new String[] { "org.opensearch.Bar" })); } public void testCreateThread() throws Exception { diff --git a/libs/secure-sm/src/test/java/org/opensearch/secure_sm/ThreadPermissionTests.java b/libs/secure-sm/src/test/java/org/opensearch/secure_sm/ThreadPermissionTests.java index 072881675b3..774e87f08c6 100644 --- a/libs/secure-sm/src/test/java/org/opensearch/secure_sm/ThreadPermissionTests.java +++ b/libs/secure-sm/src/test/java/org/opensearch/secure_sm/ThreadPermissionTests.java @@ -44,7 +44,8 @@ public class ThreadPermissionTests extends TestCase { public void testEquals() { assertEquals(new ThreadPermission("modifyArbitraryThread"), new ThreadPermission("modifyArbitraryThread")); assertFalse(new ThreadPermission("modifyArbitraryThread").equals(new AllPermission())); - assertFalse(new ThreadPermission("modifyArbitraryThread").equals(new ThreadPermission("modifyArbitraryThreadGroup"))); } + assertFalse(new ThreadPermission("modifyArbitraryThread").equals(new ThreadPermission("modifyArbitraryThreadGroup"))); + } public void testImplies() { assertTrue(new ThreadPermission("modifyArbitraryThread").implies(new ThreadPermission("modifyArbitraryThread"))); diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DerParser.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DerParser.java index 0ab6d5e8e80..ae2f4ed6b7b 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DerParser.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DerParser.java @@ -30,7 +30,6 @@ package org.opensearch.common.ssl; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -64,7 +63,6 @@ final class DerParser { private static final int UNIVERSAL_STRING = 0x1C; private static final int BMP_STRING = 0x1E; - private InputStream derInputStream; private int maxAsnObjectLength; @@ -82,14 +80,16 @@ final class DerParser { // getLength() can return any 32 bit integer, so ensure that a corrupted encoding won't // force us into allocating a very large array if (length > maxAsnObjectLength) { - throw new IOException("Invalid DER: size of ASN.1 object to be parsed appears to be larger than the size of the key file " + - "itself."); + throw new IOException( + "Invalid DER: size of ASN.1 object to be parsed appears to be larger than the size of the key file " + "itself." + ); } byte[] value = new byte[length]; int n = derInputStream.read(value); if (n < length) { - throw new IOException("Invalid DER: stream too short, missing value. " + - "Could only read " + n + " out of " + length + " bytes"); + throw new IOException( + "Invalid DER: stream too short, missing value. " + "Could only read " + n + " out of " + length + " bytes" + ); } return new Asn1Object(tag, length, value); @@ -117,29 +117,23 @@ final class DerParser { private int getLength() throws IOException { int i = derInputStream.read(); - if (i == -1) - throw new IOException("Invalid DER: length missing"); + if (i == -1) throw new IOException("Invalid DER: length missing"); // A single byte short length - if ((i & ~0x7F) == 0) - return i; + if ((i & ~0x7F) == 0) return i; int num = i & 0x7F; // We can't handle length longer than 4 bytes - if (i >= 0xFF || num > 4) - throw new IOException("Invalid DER: length field too big (" - + i + ")"); //$NON-NLS-1$ + if (i >= 0xFF || num > 4) throw new IOException("Invalid DER: length field too big (" + i + ")"); //$NON-NLS-2$ byte[] bytes = new byte[num]; int n = derInputStream.read(bytes); - if (n < num) - throw new IOException("Invalid DER: length too short"); + if (n < num) throw new IOException("Invalid DER: length too short"); return new BigInteger(1, bytes).intValue(); } - /** * An ASN.1 TLV. The object is not parsed. It can * only handle integers. @@ -207,8 +201,7 @@ final class DerParser { * @return A parser for the construct. */ public DerParser getParser() throws IOException { - if (!isConstructed()) - throw new IOException("Invalid DER: can't parse primitive entity"); //$NON-NLS-1$ + if (!isConstructed()) throw new IOException("Invalid DER: can't parse primitive entity"); //$NON-NLS-1$ return new DerParser(value); } @@ -219,8 +212,7 @@ final class DerParser { * @return BigInteger */ public BigInteger getInteger() throws IOException { - if (type != DerParser.INTEGER) - throw new IOException("Invalid DER: object is not integer"); //$NON-NLS-1$ + if (type != DerParser.INTEGER) throw new IOException("Invalid DER: object is not integer"); //$NON-NLS-1$ return new BigInteger(value); } @@ -295,6 +287,7 @@ final class DerParser { } private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray(); + private static String toHexString(byte[] bytes) { Objects.requireNonNull(bytes); StringBuilder sb = new StringBuilder(2 * bytes.length); diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DiagnosticTrustManager.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DiagnosticTrustManager.java index 8047139ae6b..75b0b831940 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DiagnosticTrustManager.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/DiagnosticTrustManager.java @@ -52,7 +52,6 @@ import static org.opensearch.common.ssl.SslDiagnostics.getTrustDiagnosticFailure public final class DiagnosticTrustManager extends X509ExtendedTrustManager { - /** * This interface exists because the ssl-config library does not depend on log4j, however the whole purpose of this class is to log * diagnostic messages, so it must be provided with a function by which it can do that. @@ -62,7 +61,6 @@ public final class DiagnosticTrustManager extends X509ExtendedTrustManager { void warning(String message, GeneralSecurityException cause); } - private final X509ExtendedTrustManager delegate; private final Supplier contextName; private final DiagnosticLogger logger; @@ -78,13 +76,18 @@ public final class DiagnosticTrustManager extends X509ExtendedTrustManager { this.contextName = contextName; this.logger = logger; this.issuers = Stream.of(delegate.getAcceptedIssuers()) - .collect(Collectors.toMap(cert -> cert.getSubjectX500Principal().getName(), Collections::singletonList, - (List a, List b) -> { - final ArrayList list = new ArrayList<>(a.size() + b.size()); - list.addAll(a); - list.addAll(b); - return list; - })); + .collect( + Collectors.toMap( + cert -> cert.getSubjectX500Principal().getName(), + Collections::singletonList, + (List a, List b) -> { + final ArrayList list = new ArrayList<>(a.size() + b.size()); + list.addAll(a); + list.addAll(b); + return list; + } + ) + ); } @Override diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/KeyStoreUtil.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/KeyStoreUtil.java index d27ba65d679..b2d57320222 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/KeyStoreUtil.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/KeyStoreUtil.java @@ -83,8 +83,9 @@ final class KeyStoreUtil { */ static KeyStore readKeyStore(Path path, String type, char[] password) throws GeneralSecurityException { if (Files.notExists(path)) { - throw new SslConfigException("cannot read a [" + type + "] keystore from [" + path.toAbsolutePath() - + "] because the file does not exist"); + throw new SslConfigException( + "cannot read a [" + type + "] keystore from [" + path.toAbsolutePath() + "] because the file does not exist" + ); } try { KeyStore keyStore = KeyStore.getInstance(type); @@ -93,8 +94,10 @@ final class KeyStoreUtil { } return keyStore; } catch (IOException e) { - throw new SslConfigException("cannot read a [" + type + "] keystore from [" + path.toAbsolutePath() + "] - " + e.getMessage(), - e); + throw new SslConfigException( + "cannot read a [" + type + "] keystore from [" + path.toAbsolutePath() + "] - " + e.getMessage(), + e + ); } } @@ -151,15 +154,16 @@ final class KeyStoreUtil { return (X509ExtendedKeyManager) keyManager; } } - throw new SslConfigException("failed to find a X509ExtendedKeyManager in the key manager factory for [" + algorithm - + "] and keystore [" + keyStore + "]"); + throw new SslConfigException( + "failed to find a X509ExtendedKeyManager in the key manager factory for [" + algorithm + "] and keystore [" + keyStore + "]" + ); } /** * Creates a {@link X509ExtendedTrustManager} based on the trust material in the provided {@link KeyStore} */ - static X509ExtendedTrustManager createTrustManager(@Nullable KeyStore trustStore, String algorithm) - throws NoSuchAlgorithmException, KeyStoreException { + static X509ExtendedTrustManager createTrustManager(@Nullable KeyStore trustStore, String algorithm) throws NoSuchAlgorithmException, + KeyStoreException { TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm); tmf.init(trustStore); TrustManager[] trustManagers = tmf.getTrustManagers(); @@ -168,9 +172,13 @@ final class KeyStoreUtil { return (X509ExtendedTrustManager) trustManager; } } - throw new SslConfigException("failed to find a X509ExtendedTrustManager in the trust manager factory for [" + algorithm - + "] and truststore [" + trustStore + "]"); + throw new SslConfigException( + "failed to find a X509ExtendedTrustManager in the trust manager factory for [" + + algorithm + + "] and truststore [" + + trustStore + + "]" + ); } - } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemKeyConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemKeyConfig.java index 3967c942b5c..038299fd6d1 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemKeyConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemKeyConfig.java @@ -101,7 +101,7 @@ public final class PemKeyConfig implements SslKeyConfig { } catch (FileNotFoundException | NoSuchFileException e) { throw new SslConfigException("the configured ssl certificate file [" + certificate.toAbsolutePath() + "] does not exist", e); } catch (IOException e) { - throw new SslConfigException("the configured ssl certificate file [" + certificate .toAbsolutePath()+ "] cannot be read", e); + throw new SslConfigException("the configured ssl certificate file [" + certificate.toAbsolutePath() + "] cannot be read", e); } catch (GeneralSecurityException e) { throw new SslConfigException("cannot load ssl certificate from [" + certificate.toAbsolutePath() + "]", e); } @@ -121,9 +121,9 @@ public final class PemKeyConfig implements SslKeyConfig { return false; } final PemKeyConfig that = (PemKeyConfig) o; - return Objects.equals(this.certificate, that.certificate) && - Objects.equals(this.key, that.key) && - Arrays.equals(this.keyPassword, that.keyPassword); + return Objects.equals(this.certificate, that.certificate) + && Objects.equals(this.key, that.key) + && Arrays.equals(this.keyPassword, that.keyPassword); } @Override diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemTrustConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemTrustConfig.java index 6577dcbe6b8..236ea6cd6f4 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemTrustConfig.java @@ -95,11 +95,15 @@ public final class PemTrustConfig implements SslTrustConfig { try { return PemUtils.readCertificates(this.certificateAuthorities); } catch (FileNotFoundException | NoSuchFileException e) { - throw new SslConfigException("cannot configure trust using PEM certificates [" + caPathsAsString() - + "] because one or more files do not exist", e); + throw new SslConfigException( + "cannot configure trust using PEM certificates [" + caPathsAsString() + "] because one or more files do not exist", + e + ); } catch (IOException e) { - throw new SslConfigException("cannot configure trust using PEM certificates [" + caPathsAsString() - + "] because one or more files cannot be read", e); + throw new SslConfigException( + "cannot configure trust using PEM certificates [" + caPathsAsString() + "] because one or more files cannot be read", + e + ); } } @@ -126,10 +130,7 @@ public final class PemTrustConfig implements SslTrustConfig { } private String caPathsAsString() { - return certificateAuthorities.stream() - .map(Path::toAbsolutePath) - .map(Object::toString) - .collect(Collectors.joining(",")); + return certificateAuthorities.stream().map(Path::toAbsolutePath).map(Object::toString).collect(Collectors.joining(",")); } } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemUtils.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemUtils.java index f4313f39d52..533c8bf429e 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemUtils.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/PemUtils.java @@ -81,8 +81,8 @@ final class PemUtils { private static final String PKCS1_FOOTER = "-----END RSA PRIVATE KEY-----"; private static final String OPENSSL_DSA_HEADER = "-----BEGIN DSA PRIVATE KEY-----"; private static final String OPENSSL_DSA_FOOTER = "-----END DSA PRIVATE KEY-----"; - private static final String OPENSSL_DSA_PARAMS_HEADER ="-----BEGIN DSA PARAMETERS-----"; - private static final String OPENSSL_DSA_PARAMS_FOOTER ="-----END DSA PARAMETERS-----"; + private static final String OPENSSL_DSA_PARAMS_HEADER = "-----BEGIN DSA PARAMETERS-----"; + private static final String OPENSSL_DSA_PARAMS_FOOTER = "-----END DSA PARAMETERS-----"; private static final String PKCS8_HEADER = "-----BEGIN PRIVATE KEY-----"; private static final String PKCS8_FOOTER = "-----END PRIVATE KEY-----"; private static final String PKCS8_ENCRYPTED_HEADER = "-----BEGIN ENCRYPTED PRIVATE KEY-----"; @@ -133,8 +133,9 @@ final class PemUtils { } else if (OPENSSL_EC_PARAMS_HEADER.equals(line.trim())) { return parseOpenSslEC(removeECHeaders(bReader), passwordSupplier); } else { - throw new SslConfigException("error parsing Private Key [" + keyPath.toAbsolutePath() - + "], file does not contain a supported key format"); + throw new SslConfigException( + "error parsing Private Key [" + keyPath.toAbsolutePath() + "], file does not contain a supported key format" + ); } } catch (FileNotFoundException | NoSuchFileException e) { throw new SslConfigException("private key file [" + keyPath.toAbsolutePath() + "] does not exist", e); @@ -344,8 +345,7 @@ final class PemUtils { * @throws IOException if the file can't be read * @throws GeneralSecurityException if the private key can't be generated from the {@link PKCS8EncodedKeySpec} */ - private static PrivateKey parsePKCS8Encrypted(BufferedReader bReader, char[] keyPassword) throws IOException, - GeneralSecurityException { + private static PrivateKey parsePKCS8Encrypted(BufferedReader bReader, char[] keyPassword) throws IOException, GeneralSecurityException { StringBuilder sb = new StringBuilder(); String line = bReader.readLine(); while (line != null) { @@ -386,10 +386,10 @@ final class PemUtils { byte[] keyBytes = Base64.getDecoder().decode(keyContents); String procType = pemHeaders.get("Proc-Type"); if ("4,ENCRYPTED".equals(procType)) { - //We only handle PEM encryption + // We only handle PEM encryption String encryptionParameters = pemHeaders.get("DEK-Info"); if (null == encryptionParameters) { - //malformed pem + // malformed pem throw new IOException("Malformed PEM File, DEK-Info header is missing"); } char[] password = passwordSupplier.get(); @@ -415,8 +415,7 @@ final class PemUtils { * for the cipher * @throws IOException if the DEK-Info PEM header is invalid */ - private static Cipher getCipherFromParameters(String dekHeaderValue, char[] password) throws - GeneralSecurityException, IOException { + private static Cipher getCipherFromParameters(String dekHeaderValue, char[] password) throws GeneralSecurityException, IOException { final String padding = "PKCS5Padding"; final SecretKey encryptionKey; final String[] valueTokens = dekHeaderValue.split(","); @@ -501,8 +500,9 @@ final class PemUtils { } return data; } else { - throw new IllegalStateException("Hexadecimal string [" + hexString + - "] has odd length and cannot be converted to a byte array"); + throw new IllegalStateException( + "Hexadecimal string [" + hexString + "] has odd length and cannot be converted to a byte array" + ); } } @@ -513,8 +513,7 @@ final class PemUtils { * @return {@link ECPrivateKeySpec} * @throws IOException if the DER encoded key can't be parsed */ - private static ECPrivateKeySpec parseEcDer(byte[] keyBytes) throws IOException, - GeneralSecurityException { + private static ECPrivateKeySpec parseEcDer(byte[] keyBytes) throws IOException, GeneralSecurityException { DerParser parser = new DerParser(keyBytes); DerParser.Asn1Object sequence = parser.readAsn1Object(); parser = sequence.getParser(); @@ -598,8 +597,9 @@ final class PemUtils { case "1.2.840.10045.2.1": return "EC"; } - throw new GeneralSecurityException("Error parsing key algorithm identifier. Algorithm with OID [" + oidString + - "] is not żsupported"); + throw new GeneralSecurityException( + "Error parsing key algorithm identifier. Algorithm with OID [" + oidString + "] is not żsupported" + ); } static List readCertificates(Collection certPaths) throws CertificateException, IOException { @@ -651,8 +651,9 @@ final class PemUtils { case "1.3.132.0.39": return "sect571r1"; } - throw new GeneralSecurityException("Error parsing EC named curve identifier. Named curve with OID: " + oidString - + " is not supported"); + throw new GeneralSecurityException( + "Error parsing EC named curve identifier. Named curve with OID: " + oidString + " is not supported" + ); } } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslClientAuthenticationMode.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslClientAuthenticationMode.java index 71b08eb95b2..e22ae45afd5 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslClientAuthenticationMode.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslClientAuthenticationMode.java @@ -106,8 +106,9 @@ public enum SslClientAuthenticationMode { final SslClientAuthenticationMode mode = LOOKUP.get(value.toLowerCase(Locale.ROOT)); if (mode == null) { final String allowedValues = LOOKUP.keySet().stream().collect(Collectors.joining(",")); - throw new SslConfigException("could not resolve ssl client authentication, unknown value [" - + value + "], recognised values are [" + allowedValues + "]"); + throw new SslConfigException( + "could not resolve ssl client authentication, unknown value [" + value + "], recognised values are [" + allowedValues + "]" + ); } return mode; } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfiguration.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfiguration.java index d257dedea8c..27c3918ed6d 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfiguration.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfiguration.java @@ -87,8 +87,14 @@ public class SslConfiguration { private final List ciphers; private final List supportedProtocols; - public SslConfiguration(SslTrustConfig trustConfig, SslKeyConfig keyConfig, SslVerificationMode verificationMode, - SslClientAuthenticationMode clientAuth, List ciphers, List supportedProtocols) { + public SslConfiguration( + SslTrustConfig trustConfig, + SslKeyConfig keyConfig, + SslVerificationMode verificationMode, + SslClientAuthenticationMode clientAuth, + List ciphers, + List supportedProtocols + ) { if (ciphers == null || ciphers.isEmpty()) { throw new SslConfigException("cannot configure SSL/TLS without any supported cipher suites"); } @@ -169,20 +175,28 @@ public class SslConfiguration { return entry.getValue(); } } - throw new SslConfigException("no supported SSL/TLS protocol was found in the configured supported protocols: " - + supportedProtocols); + throw new SslConfigException( + "no supported SSL/TLS protocol was found in the configured supported protocols: " + supportedProtocols + ); } @Override public String toString() { - return getClass().getSimpleName() + '{' + - "trustConfig=" + trustConfig + - ", keyConfig=" + keyConfig + - ", verificationMode=" + verificationMode + - ", clientAuth=" + clientAuth + - ", ciphers=" + ciphers + - ", supportedProtocols=" + supportedProtocols + - '}'; + return getClass().getSimpleName() + + '{' + + "trustConfig=" + + trustConfig + + ", keyConfig=" + + keyConfig + + ", verificationMode=" + + verificationMode + + ", clientAuth=" + + clientAuth + + ", ciphers=" + + ciphers + + ", supportedProtocols=" + + supportedProtocols + + '}'; } @Override @@ -190,12 +204,12 @@ public class SslConfiguration { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final SslConfiguration that = (SslConfiguration) o; - return Objects.equals(this.trustConfig, that.trustConfig) && - Objects.equals(this.keyConfig, that.keyConfig) && - this.verificationMode == that.verificationMode && - this.clientAuth == that.clientAuth && - Objects.equals(this.ciphers, that.ciphers) && - Objects.equals(this.supportedProtocols, that.supportedProtocols); + return Objects.equals(this.trustConfig, that.trustConfig) + && Objects.equals(this.keyConfig, that.keyConfig) + && this.verificationMode == that.verificationMode + && this.clientAuth == that.clientAuth + && Objects.equals(this.ciphers, that.ciphers) + && Objects.equals(this.supportedProtocols, that.supportedProtocols); } @Override diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationKeys.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationKeys.java index cf32e4ae6e0..eb801bd124a 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationKeys.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationKeys.java @@ -162,10 +162,20 @@ public class SslConfigurationKeys { */ public static List getStringKeys() { return Arrays.asList( - VERIFICATION_MODE, CLIENT_AUTH, - TRUSTSTORE_PATH, TRUSTSTORE_LEGACY_PASSWORD, TRUSTSTORE_TYPE, TRUSTSTORE_TYPE, - KEYSTORE_PATH, KEYSTORE_LEGACY_PASSWORD, KEYSTORE_LEGACY_KEY_PASSWORD, KEYSTORE_TYPE, KEYSTORE_ALGORITHM, - CERTIFICATE, KEY, KEY_LEGACY_PASSPHRASE + VERIFICATION_MODE, + CLIENT_AUTH, + TRUSTSTORE_PATH, + TRUSTSTORE_LEGACY_PASSWORD, + TRUSTSTORE_TYPE, + TRUSTSTORE_TYPE, + KEYSTORE_PATH, + KEYSTORE_LEGACY_PASSWORD, + KEYSTORE_LEGACY_KEY_PASSWORD, + KEYSTORE_TYPE, + KEYSTORE_ALGORITHM, + CERTIFICATE, + KEY, + KEY_LEGACY_PASSPHRASE ); } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationLoader.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationLoader.java index 36b73c16794..6f2670d285e 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationLoader.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslConfigurationLoader.java @@ -86,8 +86,10 @@ import static org.opensearch.common.ssl.SslConfigurationKeys.VERIFICATION_MODE; public abstract class SslConfigurationLoader { static final List DEFAULT_PROTOCOLS = Collections.unmodifiableList( - ORDERED_PROTOCOL_ALGORITHM_MAP.containsKey("TLSv1.3") ? - Arrays.asList("TLSv1.3", "TLSv1.2", "TLSv1.1") : Arrays.asList("TLSv1.2", "TLSv1.1")); + ORDERED_PROTOCOL_ALGORITHM_MAP.containsKey("TLSv1.3") + ? Arrays.asList("TLSv1.3", "TLSv1.2", "TLSv1.1") + : Arrays.asList("TLSv1.2", "TLSv1.1") + ); static final List DEFAULT_CIPHERS = loadDefaultCiphers(); private static final char[] EMPTY_PASSWORD = new char[0]; @@ -235,8 +237,9 @@ public abstract class SslConfigurationLoader { final Path trustStorePath = resolveSetting(TRUSTSTORE_PATH, basePath::resolve, null); if (certificateAuthorities != null && trustStorePath != null) { - throw new SslConfigException("cannot specify both [" + settingPrefix + CERTIFICATE_AUTHORITIES + "] and [" + - settingPrefix + TRUSTSTORE_PATH + "]"); + throw new SslConfigException( + "cannot specify both [" + settingPrefix + CERTIFICATE_AUTHORITIES + "] and [" + settingPrefix + TRUSTSTORE_PATH + "]" + ); } if (verificationMode.isCertificateVerificationEnabled() == false) { return TrustEverythingConfig.TRUST_EVERYTHING; @@ -259,18 +262,21 @@ public abstract class SslConfigurationLoader { final Path keyStorePath = resolveSetting(KEYSTORE_PATH, basePath::resolve, null); if (certificatePath != null && keyStorePath != null) { - throw new SslConfigException("cannot specify both [" + settingPrefix + CERTIFICATE + "] and [" + - settingPrefix + KEYSTORE_PATH + "]"); + throw new SslConfigException( + "cannot specify both [" + settingPrefix + CERTIFICATE + "] and [" + settingPrefix + KEYSTORE_PATH + "]" + ); } if (certificatePath != null || keyPath != null) { if (keyPath == null) { - throw new SslConfigException("cannot specify [" + settingPrefix + CERTIFICATE + "] without also setting [" + - settingPrefix + KEY + "]"); + throw new SslConfigException( + "cannot specify [" + settingPrefix + CERTIFICATE + "] without also setting [" + settingPrefix + KEY + "]" + ); } if (certificatePath == null) { - throw new SslConfigException("cannot specify [" + settingPrefix + KEYSTORE_PATH + "] without also setting [" + - settingPrefix + CERTIFICATE + "]"); + throw new SslConfigException( + "cannot specify [" + settingPrefix + KEYSTORE_PATH + "] without also setting [" + settingPrefix + CERTIFICATE + "]" + ); } final char[] password = resolvePasswordSetting(KEY_SECURE_PASSPHRASE, KEY_LEGACY_PASSPHRASE); return new PemKeyConfig(certificatePath, keyPath, password); @@ -301,8 +307,9 @@ public abstract class SslConfigurationLoader { } } else { if (legacyPassword != null) { - throw new SslConfigException("cannot specify both [" + settingPrefix + secureSettingKey + "] and [" - + settingPrefix + legacySettingKey + "]"); + throw new SslConfigException( + "cannot specify both [" + settingPrefix + secureSettingKey + "] and [" + settingPrefix + legacySettingKey + "]" + ); } else { return securePassword; } @@ -365,8 +372,14 @@ public abstract class SslConfigurationLoader { } if (useGCM) { // PFS, AEAD, hardware support if (has256BitAES) { - ciphers.addAll(Arrays.asList("TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", - "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")); + ciphers.addAll( + Arrays.asList( + "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" + ) + ); } else { ciphers.addAll(Arrays.asList("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256")); } @@ -374,13 +387,27 @@ public abstract class SslConfigurationLoader { // PFS, hardware support if (has256BitAES) { - ciphers.addAll(Arrays.asList("TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", - "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", - "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA")); + ciphers.addAll( + Arrays.asList( + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" + ) + ); } else { - ciphers.addAll(Arrays.asList("TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", - "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA")); + ciphers.addAll( + Arrays.asList( + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", + "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", + "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" + ) + ); } // AEAD, hardware support @@ -394,8 +421,14 @@ public abstract class SslConfigurationLoader { // hardware support if (has256BitAES) { - ciphers.addAll(Arrays.asList("TLS_RSA_WITH_AES_256_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA256", - "TLS_RSA_WITH_AES_256_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA")); + ciphers.addAll( + Arrays.asList( + "TLS_RSA_WITH_AES_256_CBC_SHA256", + "TLS_RSA_WITH_AES_128_CBC_SHA256", + "TLS_RSA_WITH_AES_256_CBC_SHA", + "TLS_RSA_WITH_AES_128_CBC_SHA" + ) + ); } else { ciphers.addAll(Arrays.asList("TLS_RSA_WITH_AES_128_CBC_SHA256", "TLS_RSA_WITH_AES_128_CBC_SHA")); } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslDiagnostics.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslDiagnostics.java index 047757e7304..29de6f6d140 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslDiagnostics.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslDiagnostics.java @@ -78,7 +78,8 @@ public class SslDiagnostics { } public enum PeerType { - CLIENT, SERVER + CLIENT, + SERVER } private static class IssuerTrust { @@ -169,12 +170,16 @@ public class SslDiagnostics { * @param trustedIssuers A Map of DN to Certificate, for the issuers that were trusted in the context in which this failure occurred * (see {@link javax.net.ssl.X509TrustManager#getAcceptedIssuers()}) */ - public static String getTrustDiagnosticFailure(X509Certificate[] chain, PeerType peerType, SSLSession session, - String contextName, @Nullable Map> trustedIssuers) { + public static String getTrustDiagnosticFailure( + X509Certificate[] chain, + PeerType peerType, + SSLSession session, + String contextName, + @Nullable Map> trustedIssuers + ) { final String peerAddress = Optional.ofNullable(session).map(SSLSession::getPeerHost).orElse(""); - final StringBuilder message = new StringBuilder("failed to establish trust with ") - .append(peerType.name().toLowerCase(Locale.ROOT)) + final StringBuilder message = new StringBuilder("failed to establish trust with ").append(peerType.name().toLowerCase(Locale.ROOT)) .append(" at [") .append(peerAddress) .append("]; "); @@ -214,8 +219,7 @@ public class SslDiagnostics { } if (isSelfIssued(peerCert)) { - message.append("; the certificate is ") - .append(describeSelfIssuedCertificate(peerCert, contextName, trustedIssuers)); + message.append("; the certificate is ").append(describeSelfIssuedCertificate(peerCert, contextName, trustedIssuers)); } else { final String issuerName = peerCert.getIssuerX500Principal().getName(); message.append("; the certificate is issued by [").append(issuerName).append("]"); @@ -223,7 +227,7 @@ public class SslDiagnostics { message.append(" but the ") .append(peerType.name().toLowerCase(Locale.ROOT)) .append(" did not provide a copy of the issuing certificate in the certificate chain") - .append(describeIssuerTrust(contextName, trustedIssuers, peerCert, issuerName)); + .append(describeIssuerTrust(contextName, trustedIssuers, peerCert, issuerName)); } } @@ -258,8 +262,12 @@ public class SslDiagnostics { return message.toString(); } - private static CharSequence describeIssuerTrust(String contextName, @Nullable Map> trustedIssuers, - X509Certificate certificate, String issuerName) { + private static CharSequence describeIssuerTrust( + String contextName, + @Nullable Map> trustedIssuers, + X509Certificate certificate, + String issuerName + ) { if (trustedIssuers == null) { return ""; } @@ -267,11 +275,11 @@ public class SslDiagnostics { final IssuerTrust trust = checkIssuerTrust(trustedIssuers, certificate); if (trust.isVerified()) { message.append("; the issuing ") - .append(trust.issuerCerts.size() == 1 ? "certificate": "certificates") + .append(trust.issuerCerts.size() == 1 ? "certificate" : "certificates") .append(" with ") .append(fingerprintDescription(trust.issuerCerts)) .append(" ") - .append(trust.issuerCerts.size() == 1 ? "is": "are") + .append(trust.issuerCerts.size() == 1 ? "is" : "are") .append(" trusted in this ssl context ([") .append(contextName) .append("])"); @@ -280,27 +288,33 @@ public class SslDiagnostics { .append(contextName) .append("]) trusts [") .append(trust.issuerCerts.size()) - .append("] ").append(trust.issuerCerts.size() == 1 ? "certificate" : "certificates") + .append("] ") + .append(trust.issuerCerts.size() == 1 ? "certificate" : "certificates") .append(" with subject name [") .append(issuerName) .append("] and ") .append(fingerprintDescription(trust.issuerCerts)) .append(" but the signatures do not match"); } else { - message.append("; this ssl context ([") - .append(contextName) - .append("]) is not configured to trust that issuer"); + message.append("; this ssl context ([").append(contextName).append("]) is not configured to trust that issuer"); } return message; } - private static CharSequence describeSelfIssuedCertificate(X509Certificate certificate, String contextName, - @Nullable Map> trustedIssuers) { + private static CharSequence describeSelfIssuedCertificate( + X509Certificate certificate, + String contextName, + @Nullable Map> trustedIssuers + ) { final StringBuilder message = new StringBuilder(); final CertificateTrust trust = resolveCertificateTrust(trustedIssuers, certificate); - message.append("self-issued; the [").append(certificate.getIssuerX500Principal().getName()).append("] certificate ") + message.append("self-issued; the [") + .append(certificate.getIssuerX500Principal().getName()) + .append("] certificate ") .append(trust.isTrusted() ? "is" : "is not") - .append(" trusted in this ssl context ([").append(contextName).append("])"); + .append(" trusted in this ssl context ([") + .append(contextName) + .append("])"); if (trust.isTrusted()) { if (trust.isSameCertificate() == false) { if (trust.trustedCertificates.size() == 1) { diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslKeyConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslKeyConfig.java index adb021362de..cb87965e837 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslKeyConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslKeyConfig.java @@ -56,4 +56,3 @@ public interface SslKeyConfig { X509ExtendedKeyManager createKeyManager(); } - diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslTrustConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslTrustConfig.java index a0140168e32..38e51778da2 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslTrustConfig.java @@ -56,4 +56,3 @@ public interface SslTrustConfig { X509ExtendedTrustManager createTrustManager(); } - diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslVerificationMode.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslVerificationMode.java index f2bb14c75e0..3721c2c85aa 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslVerificationMode.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/SslVerificationMode.java @@ -109,8 +109,13 @@ public enum SslVerificationMode { final SslVerificationMode mode = LOOKUP.get(value.toLowerCase(Locale.ROOT)); if (mode == null) { final String allowedValues = LOOKUP.keySet().stream().collect(Collectors.joining(",")); - throw new SslConfigException("could not resolve ssl client verification mode, unknown value [" - + value + "], recognised values are [" + allowedValues + "]"); + throw new SslConfigException( + "could not resolve ssl client verification mode, unknown value [" + + value + + "], recognised values are [" + + allowedValues + + "]" + ); } return mode; } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreKeyConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreKeyConfig.java index 4b7930e4cde..42246d91819 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreKeyConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreKeyConfig.java @@ -83,7 +83,8 @@ public class StoreKeyConfig implements SslKeyConfig { checkKeyStore(keyStore); return KeyStoreUtil.createKeyManager(keyStore, keyPassword, algorithm); } catch (UnrecoverableKeyException e) { - String message = "failed to load a KeyManager for keystore [" + path.toAbsolutePath() + String message = "failed to load a KeyManager for keystore [" + + path.toAbsolutePath() + "], this is usually caused by an incorrect key-password"; if (keyPassword.length == 0) { message += " (no key-password was provided)"; diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreTrustConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreTrustConfig.java index eb8b3d8761e..d509956368a 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreTrustConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/StoreTrustConfig.java @@ -75,8 +75,16 @@ final class StoreTrustConfig implements SslTrustConfig { checkTrustStore(store); return KeyStoreUtil.createTrustManager(store, algorithm); } catch (GeneralSecurityException e) { - throw new SslConfigException("cannot create trust manager for path=[" + (path == null ? null : path.toAbsolutePath()) - + "] type=[" + type + "] password=[" + (password.length == 0 ? "" : "") + "]", e); + throw new SslConfigException( + "cannot create trust manager for path=[" + + (path == null ? null : path.toAbsolutePath()) + + "] type=[" + + type + + "] password=[" + + (password.length == 0 ? "" : "") + + "]", + e + ); } } diff --git a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/TrustEverythingConfig.java b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/TrustEverythingConfig.java index f54407e8037..c8b6ab2407e 100644 --- a/libs/ssl-config/src/main/java/org/opensearch/common/ssl/TrustEverythingConfig.java +++ b/libs/ssl-config/src/main/java/org/opensearch/common/ssl/TrustEverythingConfig.java @@ -59,28 +59,22 @@ final class TrustEverythingConfig implements SslTrustConfig { */ private static final X509ExtendedTrustManager TRUST_MANAGER = new X509ExtendedTrustManager() { @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) { - } + public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) {} @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) { - } + public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) {} @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) { - } + public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) {} @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) { - } + public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) {} @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) { - } + public void checkClientTrusted(X509Certificate[] x509Certificates, String s) {} @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) { - } + public void checkServerTrusted(X509Certificate[] x509Certificates, String s) {} @Override public X509Certificate[] getAcceptedIssuers() { diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemKeyConfigTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemKeyConfigTests.java index 1bcf55c4d26..59d6a36b222 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemKeyConfigTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemKeyConfigTests.java @@ -155,10 +155,10 @@ public class PemKeyConfigTests extends OpenSearchTestCase { assertThat(certificate.getIssuerDN().getName(), is("CN=Test CA 1")); assertThat(certificate.getSubjectDN().getName(), is(expectedDN)); assertThat(certificate.getSubjectAlternativeNames(), iterableWithSize(2)); - assertThat(certificate.getSubjectAlternativeNames(), containsInAnyOrder( - Arrays.asList(DNS_NAME, "localhost"), - Arrays.asList(IP_NAME, "127.0.0.1") - )); + assertThat( + certificate.getSubjectAlternativeNames(), + containsInAnyOrder(Arrays.asList(DNS_NAME, "localhost"), Arrays.asList(IP_NAME, "127.0.0.1")) + ); } private void assertPasswordIsIncorrect(PemKeyConfig keyConfig, Path key) { diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemTrustConfigTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemTrustConfigTests.java index fced27cd11d..4617727d838 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemTrustConfigTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemTrustConfigTests.java @@ -172,7 +172,7 @@ public class PemTrustConfigTests extends OpenSearchTestCase { * ArrayIndexOutOfBoundsException. This check ensures that when we create random stream of bytes we do not create ASN.1 SEQUENCE * followed by zero length which fails the test intermittently. */ - while(checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound(bytes)) { + while (checkRandomGeneratedBytesRepresentZeroLengthDerSequenceCausingArrayIndexOutOfBound(bytes)) { bytes = randomByteArrayOfLength(length); } return bytes; diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemUtilsTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemUtilsTests.java index a84562c1575..c7ca19bb679 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemUtilsTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/PemUtilsTests.java @@ -107,8 +107,7 @@ public class PemUtilsTests extends OpenSearchTestCase { Key key = getKeyFromKeystore("RSA"); assertThat(key, notNullValue()); assertThat(key, instanceOf(PrivateKey.class)); - PrivateKey privateKey = PemUtils.readPrivateKey(getDataPath - ("/certs/pem-utils/key_pkcs8_encrypted.pem"), TESTNODE_PASSWORD); + PrivateKey privateKey = PemUtils.readPrivateKey(getDataPath("/certs/pem-utils/key_pkcs8_encrypted.pem"), TESTNODE_PASSWORD); assertThat(privateKey, notNullValue()); assertThat(privateKey, equalTo(key)); } @@ -157,8 +156,10 @@ public class PemUtilsTests extends OpenSearchTestCase { Key key = getKeyFromKeystore("DSA"); assertThat(key, notNullValue()); assertThat(key, instanceOf(PrivateKey.class)); - PrivateKey privateKey = PemUtils.readPrivateKey(getDataPath("/certs/pem-utils/dsa_key_openssl_plain_with_params.pem"), - EMPTY_PASSWORD); + PrivateKey privateKey = PemUtils.readPrivateKey( + getDataPath("/certs/pem-utils/dsa_key_openssl_plain_with_params.pem"), + EMPTY_PASSWORD + ); assertThat(privateKey, notNullValue()); assertThat(privateKey, equalTo(key)); @@ -188,8 +189,10 @@ public class PemUtilsTests extends OpenSearchTestCase { Key key = getKeyFromKeystore("EC"); assertThat(key, notNullValue()); assertThat(key, instanceOf(PrivateKey.class)); - PrivateKey privateKey = PemUtils.readPrivateKey(getDataPath("/certs/pem-utils/ec_key_openssl_plain_with_params.pem"), - EMPTY_PASSWORD); + PrivateKey privateKey = PemUtils.readPrivateKey( + getDataPath("/certs/pem-utils/ec_key_openssl_plain_with_params.pem"), + EMPTY_PASSWORD + ); assertThat(privateKey, notNullValue()); assertThat(privateKey, equalTo(key)); diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationLoaderTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationLoaderTests.java index 4e87571c13f..19f4a851152 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationLoaderTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationLoaderTests.java @@ -101,14 +101,14 @@ public class SslConfigurationLoaderTests extends OpenSearchTestCase { } public void testLoadTrustFromPemCAs() { - settings = Settings.builder() - .putList("test.ssl.certificate_authorities", "ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt") - .build(); + settings = Settings.builder().putList("test.ssl.certificate_authorities", "ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt").build(); final SslConfiguration configuration = loader.load(certRoot); final SslTrustConfig trustConfig = configuration.getTrustConfig(); assertThat(trustConfig, instanceOf(PemTrustConfig.class)); - assertThat(trustConfig.getDependentFiles(), - containsInAnyOrder(getDataPath("/certs/ca1/ca.crt"), getDataPath("/certs/ca2/ca.crt"), getDataPath("/certs/ca3/ca.crt"))); + assertThat( + trustConfig.getDependentFiles(), + containsInAnyOrder(getDataPath("/certs/ca1/ca.crt"), getDataPath("/certs/ca2/ca.crt"), getDataPath("/certs/ca3/ca.crt")) + ); assertThat(trustConfig.createTrustManager(), notNullValue()); } @@ -174,14 +174,18 @@ public class SslConfigurationLoaderTests extends OpenSearchTestCase { final SslConfiguration configuration = loader.load(certRoot); final SslKeyConfig keyConfig = configuration.getKeyConfig(); assertThat(keyConfig, instanceOf(PemKeyConfig.class)); - assertThat(keyConfig.getDependentFiles(), containsInAnyOrder( - getDataPath("/certs/" + certName + "/" + certName + ".crt"), getDataPath("/certs/" + certName + "/" + certName + ".key"))); + assertThat( + keyConfig.getDependentFiles(), + containsInAnyOrder( + getDataPath("/certs/" + certName + "/" + certName + ".crt"), + getDataPath("/certs/" + certName + "/" + certName + ".key") + ) + ); assertThat(keyConfig.createKeyManager(), notNullValue()); } public void testLoadKeysFromPKCS12() { - final Settings.Builder builder = Settings.builder() - .put("test.ssl.keystore.path", "cert-all/certs.p12"); + final Settings.Builder builder = Settings.builder().put("test.ssl.keystore.path", "cert-all/certs.p12"); if (randomBoolean()) { builder.put("test.ssl.keystore.password", "p12-pass"); } else { @@ -204,8 +208,7 @@ public class SslConfigurationLoaderTests extends OpenSearchTestCase { public void testLoadKeysFromJKS() { assumeFalse("Can't use JKS/PKCS12 keystores in a FIPS JVM", inFipsJvm()); - final Settings.Builder builder = Settings.builder() - .put("test.ssl.keystore.path", "cert-all/certs.jks"); + final Settings.Builder builder = Settings.builder().put("test.ssl.keystore.path", "cert-all/certs.jks"); if (randomBoolean()) { builder.put("test.ssl.keystore.password", "jks-pass"); } else { diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationTests.java index 478d18ae8a0..a629afc7f0d 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslConfigurationTests.java @@ -61,8 +61,14 @@ public class SslConfigurationTests extends OpenSearchTestCase { final SslClientAuthenticationMode clientAuth = randomFrom(SslClientAuthenticationMode.values()); final List ciphers = randomSubsetOf(randomIntBetween(1, DEFAULT_CIPHERS.size()), DEFAULT_CIPHERS); final List protocols = randomSubsetOf(randomIntBetween(1, 4), VALID_PROTOCOLS); - final SslConfiguration configuration = - new SslConfiguration(trustConfig, keyConfig, verificationMode, clientAuth, ciphers, protocols); + final SslConfiguration configuration = new SslConfiguration( + trustConfig, + keyConfig, + verificationMode, + clientAuth, + ciphers, + protocols + ); assertThat(configuration.getTrustConfig(), is(trustConfig)); assertThat(configuration.getKeyConfig(), is(keyConfig)); @@ -86,39 +92,80 @@ public class SslConfigurationTests extends OpenSearchTestCase { final SslClientAuthenticationMode clientAuth = randomFrom(SslClientAuthenticationMode.values()); final List ciphers = randomSubsetOf(randomIntBetween(1, DEFAULT_CIPHERS.size() - 1), DEFAULT_CIPHERS); final List protocols = randomSubsetOf(randomIntBetween(1, VALID_PROTOCOLS.length - 1), VALID_PROTOCOLS); - final SslConfiguration configuration = - new SslConfiguration(trustConfig, keyConfig, verificationMode, clientAuth, ciphers, protocols); + final SslConfiguration configuration = new SslConfiguration( + trustConfig, + keyConfig, + verificationMode, + clientAuth, + ciphers, + protocols + ); - EqualsHashCodeTestUtils.checkEqualsAndHashCode(configuration, - orig -> new SslConfiguration(orig.getTrustConfig(), orig.getKeyConfig(), orig.getVerificationMode(), orig.getClientAuth(), - orig.getCipherSuites(), orig.getSupportedProtocols()), + EqualsHashCodeTestUtils.checkEqualsAndHashCode( + configuration, + orig -> new SslConfiguration( + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), + orig.getClientAuth(), + orig.getCipherSuites(), + orig.getSupportedProtocols() + ), orig -> { switch (randomIntBetween(1, 4)) { case 1: - return new SslConfiguration(orig.getTrustConfig(), orig.getKeyConfig(), + return new SslConfiguration( + orig.getTrustConfig(), + orig.getKeyConfig(), randomValueOtherThan(orig.getVerificationMode(), () -> randomFrom(SslVerificationMode.values())), - orig.getClientAuth(), orig.getCipherSuites(), orig.getSupportedProtocols()); + orig.getClientAuth(), + orig.getCipherSuites(), + orig.getSupportedProtocols() + ); case 2: - return new SslConfiguration(orig.getTrustConfig(), orig.getKeyConfig(), orig.getVerificationMode(), + return new SslConfiguration( + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), randomValueOtherThan(orig.getClientAuth(), () -> randomFrom(SslClientAuthenticationMode.values())), - orig.getCipherSuites(), orig.getSupportedProtocols()); + orig.getCipherSuites(), + orig.getSupportedProtocols() + ); case 3: - return new SslConfiguration(orig.getTrustConfig(), orig.getKeyConfig(), - orig.getVerificationMode(), orig.getClientAuth(), DEFAULT_CIPHERS, orig.getSupportedProtocols()); + return new SslConfiguration( + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), + orig.getClientAuth(), + DEFAULT_CIPHERS, + orig.getSupportedProtocols() + ); case 4: default: - return new SslConfiguration(orig.getTrustConfig(), orig.getKeyConfig(), orig.getVerificationMode(), - orig.getClientAuth(), orig.getCipherSuites(), Arrays.asList(VALID_PROTOCOLS)); + return new SslConfiguration( + orig.getTrustConfig(), + orig.getKeyConfig(), + orig.getVerificationMode(), + orig.getClientAuth(), + orig.getCipherSuites(), + Arrays.asList(VALID_PROTOCOLS) + ); } - }); + } + ); } public void testDependentFiles() { final SslTrustConfig trustConfig = Mockito.mock(SslTrustConfig.class); final SslKeyConfig keyConfig = Mockito.mock(SslKeyConfig.class); - final SslConfiguration configuration = new SslConfiguration(trustConfig, keyConfig, - randomFrom(SslVerificationMode.values()), randomFrom(SslClientAuthenticationMode.values()), - DEFAULT_CIPHERS, SslConfigurationLoader.DEFAULT_PROTOCOLS); + final SslConfiguration configuration = new SslConfiguration( + trustConfig, + keyConfig, + randomFrom(SslVerificationMode.values()), + randomFrom(SslClientAuthenticationMode.values()), + DEFAULT_CIPHERS, + SslConfigurationLoader.DEFAULT_PROTOCOLS + ); final Path dir = createTempDir(); final Path file1 = dir.resolve(randomAlphaOfLength(1) + ".pem"); @@ -136,9 +183,14 @@ public class SslConfigurationTests extends OpenSearchTestCase { final SslTrustConfig trustConfig = Mockito.mock(SslTrustConfig.class); final SslKeyConfig keyConfig = Mockito.mock(SslKeyConfig.class); final String protocol = randomFrom(SslConfigurationLoader.DEFAULT_PROTOCOLS); - final SslConfiguration configuration = new SslConfiguration(trustConfig, keyConfig, - randomFrom(SslVerificationMode.values()), randomFrom(SslClientAuthenticationMode.values()), - DEFAULT_CIPHERS, Collections.singletonList(protocol)); + final SslConfiguration configuration = new SslConfiguration( + trustConfig, + keyConfig, + randomFrom(SslVerificationMode.values()), + randomFrom(SslClientAuthenticationMode.values()), + DEFAULT_CIPHERS, + Collections.singletonList(protocol) + ); Mockito.when(trustConfig.createTrustManager()).thenReturn(null); Mockito.when(keyConfig.createKeyManager()).thenReturn(null); diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslDiagnosticsTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslDiagnosticsTests.java index 96d178d4a6a..38ec0c3b5ff 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslDiagnosticsTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/SslDiagnosticsTests.java @@ -72,89 +72,149 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by" + - " (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + - " which is self-issued; the [CN=Test CA 1] certificate is trusted in this ssl context ([foo.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by" + + " (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + + " which is self-issued; the [CN=Test CA 1] certificate is trusted in this ssl context ([foo.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesAFullCertChainThatIsntTrusted() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + - " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + + " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerFullCertChainIsntTrustedButMimicIssuerExists() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt", "ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1-b/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + - " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl]);" + - " this ssl context does trust a certificate with subject [CN=Test CA 1]" + - " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1] fingerprint [2b7b0416391bdf86502505c23149022d2213dadc])" + + " which is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl]);" + + " this ssl context does trust a certificate with subject [CN=Test CA 1]" + + " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateOnlyAndTheCertAuthIsTrusted() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + - " is trusted in this ssl context ([foo.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + + " is trusted in this ssl context ([foo.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateOnlyButTheCertAuthIsNotTrusted() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([foo.http.ssl]) is not configured to trust that issuer")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([foo.http.ssl]) is not configured to trust that issuer" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateOnlyWithMimicIssuer() throws Exception { X509Certificate[] chain = loadCertChain("cert1/cert1.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1-b/ca.crt", "ca2/ca.crt", "ca3/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([foo.http.ssl]) trusts [1] certificate with subject name [CN=Test CA 1]" + - " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663] but the signatures do not match")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([foo.http.ssl]) trusts [1] certificate with subject name [CN=Test CA 1]" + + " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663] but the signatures do not match" + ) + ); } public void testDiagnosticMessageWhenServerProvidesEndCertificateWithMultipleMimicIssuers() throws Exception { @@ -162,142 +222,273 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { final SSLSession session = session("192.168.1.9"); final X509Certificate ca1b = loadCertificate("ca1-b/ca.crt"); final Map> trustIssuers = trust(ca1b, cloneCertificateAsMock(ca1b)); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.9];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the server did not provide a copy of the issuing certificate in the certificate chain;" + - " this ssl context ([foo.http.ssl]) trusts [2] certificates with subject name [CN=Test CA 1]" + - " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663], fingerprint [" + MOCK_FINGERPRINT_1 + "]" + - " but the signatures do not match")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.9];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the server did not provide a copy of the issuing certificate in the certificate chain;" + + " this ssl context ([foo.http.ssl]) trusts [2] certificates with subject name [CN=Test CA 1]" + + " and fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663], fingerprint [" + + MOCK_FINGERPRINT_1 + + "]" + + " but the signatures do not match" + ) + ); } public void testDiagnosticMessageWhenServerProvidePartialChainFromTrustedCA() throws Exception { - final X509Certificate rootCA = mockCertificateWithIssuer("CN=root-ca,DC=example,DC=com", MOCK_ENCODING_1, - Collections.emptyList(), null); - final X509Certificate issuingCA = mockCertificateWithIssuer("CN=issuing-ca,DC=example,DC=com", MOCK_ENCODING_2, - Collections.emptyList(), rootCA); - final X509Certificate localCA = mockCertificateWithIssuer("CN=ca,OU=windows,DC=example,DC=com", MOCK_ENCODING_3, - Collections.emptyList(), issuingCA); - final X509Certificate endCert = mockCertificateWithIssuer("CN=elastic1,OU=windows,DC=example,DC=com", MOCK_ENCODING_4, - Collections.emptyList(), localCA); + final X509Certificate rootCA = mockCertificateWithIssuer( + "CN=root-ca,DC=example,DC=com", + MOCK_ENCODING_1, + Collections.emptyList(), + null + ); + final X509Certificate issuingCA = mockCertificateWithIssuer( + "CN=issuing-ca,DC=example,DC=com", + MOCK_ENCODING_2, + Collections.emptyList(), + rootCA + ); + final X509Certificate localCA = mockCertificateWithIssuer( + "CN=ca,OU=windows,DC=example,DC=com", + MOCK_ENCODING_3, + Collections.emptyList(), + issuingCA + ); + final X509Certificate endCert = mockCertificateWithIssuer( + "CN=elastic1,OU=windows,DC=example,DC=com", + MOCK_ENCODING_4, + Collections.emptyList(), + localCA + ); final X509Certificate[] chain = { endCert, localCA, issuingCA }; final SSLSession session = session("192.168.1.5"); final Map> trustIssuers = trust(issuingCA, rootCA); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.security.authc.realms.ldap.ldap1.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.5];" + - " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + - " and fingerprint [" + MOCK_FINGERPRINT_4 + "];" + - " the certificate does not have any subject alternative names;" + - " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + - " the certificate is" + - " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_3 + "])" + - " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_2 + "] {trusted issuer})" + - " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + - " the issuing certificate with fingerprint [" + MOCK_FINGERPRINT_1 + "]" + - " is trusted in this ssl context ([foo.security.authc.realms.ldap.ldap1.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.security.authc.realms.ldap.ldap1.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.5];" + + " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + + " and fingerprint [" + + MOCK_FINGERPRINT_4 + + "];" + + " the certificate does not have any subject alternative names;" + + " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + + " the certificate is" + + " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_3 + + "])" + + " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_2 + + "] {trusted issuer})" + + " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + + " the issuing certificate with fingerprint [" + + MOCK_FINGERPRINT_1 + + "]" + + " is trusted in this ssl context ([foo.security.authc.realms.ldap.ldap1.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidePartialChainFromUntrustedCA() throws Exception { - final X509Certificate rootCA = mockCertificateWithIssuer("CN=root-ca,DC=example,DC=com", MOCK_ENCODING_1, - Collections.emptyList(), null); - final X509Certificate issuingCA = mockCertificateWithIssuer("CN=issuing-ca,DC=example,DC=com", MOCK_ENCODING_2, - Collections.emptyList(), rootCA); - final X509Certificate localCA = mockCertificateWithIssuer("CN=ca,OU=windows,DC=example,DC=com", MOCK_ENCODING_3, - Collections.emptyList(), issuingCA); - final X509Certificate endCert = mockCertificateWithIssuer("CN=elastic1,OU=windows,DC=example,DC=com", MOCK_ENCODING_4, - Collections.emptyList(), localCA); + final X509Certificate rootCA = mockCertificateWithIssuer( + "CN=root-ca,DC=example,DC=com", + MOCK_ENCODING_1, + Collections.emptyList(), + null + ); + final X509Certificate issuingCA = mockCertificateWithIssuer( + "CN=issuing-ca,DC=example,DC=com", + MOCK_ENCODING_2, + Collections.emptyList(), + rootCA + ); + final X509Certificate localCA = mockCertificateWithIssuer( + "CN=ca,OU=windows,DC=example,DC=com", + MOCK_ENCODING_3, + Collections.emptyList(), + issuingCA + ); + final X509Certificate endCert = mockCertificateWithIssuer( + "CN=elastic1,OU=windows,DC=example,DC=com", + MOCK_ENCODING_4, + Collections.emptyList(), + localCA + ); final X509Certificate[] chain = { endCert, localCA, issuingCA }; final SSLSession session = session("192.168.1.6"); final Map> trustIssuers = trust(Collections.emptyList()); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.security.authc.realms.ldap.ldap1.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.6];" + - " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + - " and fingerprint [" + MOCK_FINGERPRINT_4 + "];" + - " the certificate does not have any subject alternative names;" + - " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + - " the certificate is" + - " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_3 + "])" + - " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + MOCK_FINGERPRINT_2 + "])" + - " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + - " this ssl context ([foo.security.authc.realms.ldap.ldap1.ssl]) is not configured to trust that issuer")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.security.authc.realms.ldap.ldap1.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.6];" + + " the server provided a certificate with subject name [CN=elastic1,OU=windows,DC=example,DC=com]" + + " and fingerprint [" + + MOCK_FINGERPRINT_4 + + "];" + + " the certificate does not have any subject alternative names;" + + " the certificate is issued by [CN=ca,OU=windows,DC=example,DC=com];" + + " the certificate is" + + " signed by (subject [CN=ca,OU=windows,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_3 + + "])" + + " signed by (subject [CN=issuing-ca,DC=example,DC=com] fingerprint [" + + MOCK_FINGERPRINT_2 + + "])" + + " which is issued by [CN=root-ca,DC=example,DC=com] (but that issuer certificate was not provided in the chain);" + + " this ssl context ([foo.security.authc.realms.ldap.ldap1.ssl]) is not configured to trust that issuer" + ) + ); } public void testDiagnosticMessageWhenServerProvidesASelfSignedCertThatIsDirectlyTrusted() throws Exception { X509Certificate[] chain = loadCertChain("ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1/ca.crt", "ca2/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=Test CA 1]" + - " and fingerprint [2b7b0416391bdf86502505c23149022d2213dadc];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued; the [CN=Test CA 1] certificate is trusted in this ssl context ([foo.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=Test CA 1]" + + " and fingerprint [2b7b0416391bdf86502505c23149022d2213dadc];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued; the [CN=Test CA 1] certificate is trusted in this ssl context ([foo.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesASelfSignedCertThatIsNotTrusted() throws Exception { X509Certificate[] chain = loadCertChain("ca1/ca.crt"); final SSLSession session = session("192.168.10.10"); final Map> trustIssuers = Collections.emptyMap(); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.10.10];" + - " the server provided a certificate with subject name [CN=Test CA 1]" + - " and fingerprint [2b7b0416391bdf86502505c23149022d2213dadc];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.10.10];" + + " the server provided a certificate with subject name [CN=Test CA 1]" + + " and fingerprint [2b7b0416391bdf86502505c23149022d2213dadc];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl])" + ) + ); } public void testDiagnosticMessageWhenServerProvidesASelfSignedCertWithMimicName() throws Exception { X509Certificate[] chain = loadCertChain("ca1/ca.crt"); final SSLSession session = session("192.168.1.1"); final Map> trustIssuers = trust("ca1-b/ca.crt", "ca2/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.1];" + - " the server provided a certificate with subject name [CN=Test CA 1]" + - " and fingerprint [2b7b0416391bdf86502505c23149022d2213dadc];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl]);" + - " this ssl context does trust a certificate with subject [CN=Test CA 1]" + - " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.1];" + + " the server provided a certificate with subject name [CN=Test CA 1]" + + " and fingerprint [2b7b0416391bdf86502505c23149022d2213dadc];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued; the [CN=Test CA 1] certificate is not trusted in this ssl context ([foo.http.ssl]);" + + " this ssl context does trust a certificate with subject [CN=Test CA 1]" + + " but the trusted certificate has fingerprint [b095bf2526be20783e1f26dfd69c7aae910e3663]" + ) + ); } public void testDiagnosticMessageWithEmptyChain() throws Exception { X509Certificate[] chain = new X509Certificate[0]; final SSLSession session = session("192.168.1.2"); final Map> trustIssuers = Collections.emptyMap(); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.http.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.2];" + - " the server did not provide a certificate")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.http.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo("failed to establish trust with server at [192.168.1.2];" + " the server did not provide a certificate") + ); } public void testDiagnosticMessageWhenServerProvidesAnEmailSubjAltName() throws Exception { final String subjectName = "CN=foo,DC=example,DC=com"; - final X509Certificate certificate = mockCertificateWithIssuer(subjectName, - MOCK_ENCODING_1, Collections.singletonList(Arrays.asList(1, "foo@example.com")), null); + final X509Certificate certificate = mockCertificateWithIssuer( + subjectName, + MOCK_ENCODING_1, + Collections.singletonList(Arrays.asList(1, "foo@example.com")), + null + ); X509Certificate[] chain = new X509Certificate[] { certificate }; final SSLSession session = session("192.168.1.3"); final Map> trustIssuers = trust(certificate); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.monitoring.exporters.opensearch.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.3];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - " and fingerprint [" + MOCK_FINGERPRINT_1 + "];" + - " the certificate does not have any DNS/IP subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is trusted in this ssl context ([foo.monitoring.exporters.opensearch.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.monitoring.exporters.opensearch.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.3];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + " and fingerprint [" + + MOCK_FINGERPRINT_1 + + "];" + + " the certificate does not have any DNS/IP subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is trusted in this ssl context ([foo.monitoring.exporters.opensearch.ssl])" + ) + ); } public void testDiagnosticMessageWhenACertificateHasAnInvalidEncoding() throws Exception { @@ -308,14 +499,24 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { final SSLSession session = session("192.168.1.6"); final Map> trustIssuers = trust(Collections.emptyList()); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.security.transport.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.6];" + - " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + - " and invalid encoding [java.security.cert.CertificateEncodingException: MOCK INVALID ENCODING];" + - " the certificate does not have any subject alternative names;" + - " the certificate is self-issued;" + - " the [CN=foo,DC=example,DC=com] certificate is not trusted in this ssl context ([foo.security.transport.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.security.transport.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.6];" + + " the server provided a certificate with subject name [CN=foo,DC=example,DC=com]" + + " and invalid encoding [java.security.cert.CertificateEncodingException: MOCK INVALID ENCODING];" + + " the certificate does not have any subject alternative names;" + + " the certificate is self-issued;" + + " the [CN=foo,DC=example,DC=com] certificate is not trusted in this ssl context ([foo.security.transport.ssl])" + ) + ); } public void testDiagnosticMessageForClientCertificate() throws Exception { @@ -323,15 +524,25 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { final SSLSession session = session("192.168.1.7"); final Map> trustIssuers = trust("ca1/ca.crt"); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.CLIENT, session, - "foo.security.transport.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with client at [192.168.1.7];" + - " the client provided a certificate with subject name [CN=cert1]" + - " and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate is issued by [CN=Test CA 1]" + - " but the client did not provide a copy of the issuing certificate in the certificate chain;" + - " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + - " is trusted in this ssl context ([foo.security.transport.ssl])")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.CLIENT, + session, + "foo.security.transport.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with client at [192.168.1.7];" + + " the client provided a certificate with subject name [CN=cert1]" + + " and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate is issued by [CN=Test CA 1]" + + " but the client did not provide a copy of the issuing certificate in the certificate chain;" + + " the issuing certificate with fingerprint [2b7b0416391bdf86502505c23149022d2213dadc]" + + " is trusted in this ssl context ([foo.security.transport.ssl])" + ) + ); } public void testDiagnosticMessageWhenCaHasNewIssuingCertificate() throws Exception { @@ -347,18 +558,28 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { final SSLSession session = session("192.168.1.4"); final Map> trustIssuers = trust(oldCaCert); - final String message = SslDiagnostics.getTrustDiagnosticFailure(chain, SslDiagnostics.PeerType.SERVER, session, - "foo.security.authc.realms.saml.saml1.ssl", trustIssuers); - assertThat(message, Matchers.equalTo("failed to establish trust with server at [192.168.1.4];" + - " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + - " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + - " the certificate is issued by [CN=Test CA 1];" + - " the certificate is signed by (subject [CN=Test CA 1]" + - " fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + - " which is self-issued;" + - " the [CN=Test CA 1] certificate is trusted in this ssl context ([foo.security.authc.realms.saml.saml1.ssl])" + - " because we trust a certificate with fingerprint [1f8ac10f23c5b5bc1167bda84b833e5c057a77d2]" + - " for the same public key")); + final String message = SslDiagnostics.getTrustDiagnosticFailure( + chain, + SslDiagnostics.PeerType.SERVER, + session, + "foo.security.authc.realms.saml.saml1.ssl", + trustIssuers + ); + assertThat( + message, + Matchers.equalTo( + "failed to establish trust with server at [192.168.1.4];" + + " the server provided a certificate with subject name [CN=cert1] and fingerprint [3bebe388a66362784afd6c51a9000961a4e10050];" + + " the certificate has subject alternative names [DNS:localhost,IP:127.0.0.1];" + + " the certificate is issued by [CN=Test CA 1];" + + " the certificate is signed by (subject [CN=Test CA 1]" + + " fingerprint [2b7b0416391bdf86502505c23149022d2213dadc] {trusted issuer})" + + " which is self-issued;" + + " the [CN=Test CA 1] certificate is trusted in this ssl context ([foo.security.authc.realms.saml.saml1.ssl])" + + " because we trust a certificate with fingerprint [1f8ac10f23c5b5bc1167bda84b833e5c057a77d2]" + + " for the same public key" + ) + ); } public X509Certificate cloneCertificateAsMock(X509Certificate clone) throws CertificateParsingException, CertificateEncodingException { @@ -372,8 +593,12 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { return cert; } - public X509Certificate mockCertificateWithIssuer(String principal, byte[] encoding, List> subjAltNames, - @Nullable X509Certificate issuer) throws CertificateException { + public X509Certificate mockCertificateWithIssuer( + String principal, + byte[] encoding, + List> subjAltNames, + @Nullable X509Certificate issuer + ) throws CertificateException { final X509Certificate cert = Mockito.mock(X509Certificate.class); final X500Principal x500Principal = new X500Principal(principal); @@ -400,8 +625,9 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { if (certificates.size() == 1) { return (X509Certificate) certificates.get(0); } else { - throw new IllegalStateException("Expected 1 certificate in [" + path.toAbsolutePath() - + "] but found [" + certificates.size() + "] - " + certificates); + throw new IllegalStateException( + "Expected 1 certificate in [" + path.toAbsolutePath() + "] but found [" + certificates.size() + "] - " + certificates + ); } } @@ -417,13 +643,18 @@ public class SslDiagnosticsTests extends OpenSearchTestCase { private Map> trust(Collection caCerts) { return caCerts.stream() .map(X509Certificate.class::cast) - .collect(Collectors.toMap(x -> x.getSubjectX500Principal().getName(), Collections::singletonList, - (List a, List b) -> { - List merge = new ArrayList<>(); - merge.addAll(a); - merge.addAll(b); - return merge; - })); + .collect( + Collectors.toMap( + x -> x.getSubjectX500Principal().getName(), + Collections::singletonList, + (List a, List b) -> { + List merge = new ArrayList<>(); + merge.addAll(a); + merge.addAll(b); + return merge; + } + ) + ); } private SSLSession session(String peerHost) { diff --git a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/StoreKeyConfigTests.java b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/StoreKeyConfigTests.java index 52681984fb6..de7420d27c1 100644 --- a/libs/ssl-config/src/test/java/org/opensearch/common/ssl/StoreKeyConfigTests.java +++ b/libs/ssl-config/src/test/java/org/opensearch/common/ssl/StoreKeyConfigTests.java @@ -83,8 +83,13 @@ public class StoreKeyConfigTests extends OpenSearchTestCase { public void testLoadMultipleKeyJksWithSeparateKeyPassword() throws Exception { assumeFalse("Can't use JKS/PKCS12 keystores in a FIPS JVM", inFipsJvm()); final Path jks = getDataPath("/certs/cert-all/certs.jks"); - final StoreKeyConfig keyConfig = new StoreKeyConfig(jks, JKS_PASS, "jks", "key-pass".toCharArray(), - KeyManagerFactory.getDefaultAlgorithm()); + final StoreKeyConfig keyConfig = new StoreKeyConfig( + jks, + JKS_PASS, + "jks", + "key-pass".toCharArray(), + KeyManagerFactory.getDefaultAlgorithm() + ); assertThat(keyConfig.getDependentFiles(), Matchers.containsInAnyOrder(jks)); assertKeysLoaded(keyConfig, "cert1", "cert2"); } @@ -92,8 +97,13 @@ public class StoreKeyConfigTests extends OpenSearchTestCase { public void testKeyManagerFailsWithIncorrectStorePassword() throws Exception { assumeFalse("Can't use JKS/PKCS12 keystores in a FIPS JVM", inFipsJvm()); final Path jks = getDataPath("/certs/cert-all/certs.jks"); - final StoreKeyConfig keyConfig = new StoreKeyConfig(jks, P12_PASS, "jks", "key-pass".toCharArray(), - KeyManagerFactory.getDefaultAlgorithm()); + final StoreKeyConfig keyConfig = new StoreKeyConfig( + jks, + P12_PASS, + "jks", + "key-pass".toCharArray(), + KeyManagerFactory.getDefaultAlgorithm() + ); assertThat(keyConfig.getDependentFiles(), Matchers.containsInAnyOrder(jks)); assertPasswordIsIncorrect(keyConfig, jks); } @@ -175,10 +185,10 @@ public class StoreKeyConfigTests extends OpenSearchTestCase { assertThat(certificate.getIssuerDN().getName(), is("CN=Test CA 1")); assertThat(certificate.getSubjectDN().getName(), is("CN=" + name)); assertThat(certificate.getSubjectAlternativeNames(), iterableWithSize(2)); - assertThat(certificate.getSubjectAlternativeNames(), containsInAnyOrder( - Arrays.asList(DNS_NAME, "localhost"), - Arrays.asList(IP_NAME, "127.0.0.1") - )); + assertThat( + certificate.getSubjectAlternativeNames(), + containsInAnyOrder(Arrays.asList(DNS_NAME, "localhost"), Arrays.asList(IP_NAME, "127.0.0.1")) + ); } } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/AbstractObjectParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/AbstractObjectParser.java index 25aa4212cde..ee6221a4f94 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/AbstractObjectParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/AbstractObjectParser.java @@ -53,8 +53,12 @@ public abstract class AbstractObjectParser { * Declare some field. Usually it is easier to use {@link #declareString(BiConsumer, ParseField)} or * {@link #declareObject(BiConsumer, ContextParser, ParseField)} rather than call this directly. */ - public abstract void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, - ValueType type); + public abstract void declareField( + BiConsumer consumer, + ContextParser parser, + ParseField parseField, + ValueType type + ); /** * Declares a single named object. @@ -77,9 +81,11 @@ public abstract class AbstractObjectParser { * @param parseField * the field to parse */ - public abstract void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, - ParseField parseField); - + public abstract void declareNamedObject( + BiConsumer consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ); /** * Declares named objects in the style of aggregations. These are named @@ -111,8 +117,11 @@ public abstract class AbstractObjectParser { * @param parseField * the field to parse */ - public abstract void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - ParseField parseField); + public abstract void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ); /** * Declares named objects in the style of highlighting's field element. @@ -166,13 +175,21 @@ public abstract class AbstractObjectParser { * @param parseField * the field to parse */ - public abstract void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField parseField); + public abstract void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField parseField + ); public abstract String getName(); - public void declareField(BiConsumer consumer, CheckedFunction parser, - ParseField parseField, ValueType type) { + public void declareField( + BiConsumer consumer, + CheckedFunction parser, + ParseField parseField, + ValueType type + ) { if (parser == null) { throw new IllegalArgumentException("[parser] is required"); } @@ -186,10 +203,18 @@ public abstract class AbstractObjectParser { /** * Declare an object field that parses explicit {@code null}s in the json to a default value. */ - public void declareObjectOrNull(BiConsumer consumer, ContextParser objectParser, T nullValue, - ParseField field) { - declareField(consumer, (p, c) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : objectParser.parse(p, c), - field, ValueType.OBJECT_OR_NULL); + public void declareObjectOrNull( + BiConsumer consumer, + ContextParser objectParser, + T nullValue, + ParseField field + ) { + declareField( + consumer, + (p, c) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : objectParser.parse(p, c), + field, + ValueType.OBJECT_OR_NULL + ); } public void declareFloat(BiConsumer consumer, ParseField field) { @@ -201,8 +226,12 @@ public abstract class AbstractObjectParser { * Declare a float field that parses explicit {@code null}s in the json to a default value. */ public void declareFloatOrNull(BiConsumer consumer, float nullValue, ParseField field) { - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.floatValue(), - field, ValueType.FLOAT_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.floatValue(), + field, + ValueType.FLOAT_OR_NULL + ); } public void declareDouble(BiConsumer consumer, ParseField field) { @@ -214,8 +243,12 @@ public abstract class AbstractObjectParser { * Declare a double field that parses explicit {@code null}s in the json to a default value. */ public void declareDoubleOrNull(BiConsumer consumer, double nullValue, ParseField field) { - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.doubleValue(), - field, ValueType.DOUBLE_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.doubleValue(), + field, + ValueType.DOUBLE_OR_NULL + ); } public void declareLong(BiConsumer consumer, ParseField field) { @@ -225,8 +258,12 @@ public abstract class AbstractObjectParser { public void declareLongOrNull(BiConsumer consumer, long nullValue, ParseField field) { // Using a method reference here angers some compilers - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.longValue(), - field, ValueType.LONG_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.longValue(), + field, + ValueType.LONG_OR_NULL + ); } public void declareInt(BiConsumer consumer, ParseField field) { @@ -238,8 +275,12 @@ public abstract class AbstractObjectParser { * Declare a double field that parses explicit {@code null}s in the json to a default value. */ public void declareIntOrNull(BiConsumer consumer, int nullValue, ParseField field) { - declareField(consumer, p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.intValue(), - field, ValueType.INT_OR_NULL); + declareField( + consumer, + p -> p.currentToken() == XContentParser.Token.VALUE_NULL ? nullValue : p.intValue(), + field, + ValueType.INT_OR_NULL + ); } public void declareString(BiConsumer consumer, ParseField field) { @@ -255,8 +296,12 @@ public abstract class AbstractObjectParser { } public void declareStringOrNull(BiConsumer consumer, ParseField field) { - declareField(consumer, (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.text(), field, - ValueType.STRING_OR_NULL); + declareField( + consumer, + (p) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : p.text(), + field, + ValueType.STRING_OR_NULL + ); } public void declareBoolean(BiConsumer consumer, ParseField field) { @@ -271,8 +316,7 @@ public abstract class AbstractObjectParser { * like {@link #declareObjectArray(BiConsumer, ContextParser, ParseField)}, but can also handle single null values, * in which case the consumer isn't called */ - public < - T> void declareObjectArrayOrNull( + public void declareObjectArrayOrNull( BiConsumer> consumer, ContextParser objectParser, ParseField field @@ -308,8 +352,12 @@ public abstract class AbstractObjectParser { /** * Declares a field that can contain an array of elements listed in the type ValueType enum */ - public void declareFieldArray(BiConsumer> consumer, ContextParser itemParser, - ParseField field, ValueType type) { + public void declareFieldArray( + BiConsumer> consumer, + ContextParser itemParser, + ParseField field, + ValueType type + ) { declareField(consumer, (p, c) -> parseArray(p, () -> itemParser.parse(p, c)), field, type); } @@ -384,14 +432,14 @@ public abstract class AbstractObjectParser { private static List parseArray(XContentParser parser, IOSupplier supplier) throws IOException { List list = new ArrayList<>(); if (parser.currentToken().isValue() - || parser.currentToken() == XContentParser.Token.VALUE_NULL - || parser.currentToken() == XContentParser.Token.START_OBJECT) { + || parser.currentToken() == XContentParser.Token.VALUE_NULL + || parser.currentToken() == XContentParser.Token.START_OBJECT) { list.add(supplier.get()); // single value } else { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { if (parser.currentToken().isValue() - || parser.currentToken() == XContentParser.Token.VALUE_NULL - || parser.currentToken() == XContentParser.Token.START_OBJECT) { + || parser.currentToken() == XContentParser.Token.VALUE_NULL + || parser.currentToken() == XContentParser.Token.START_OBJECT) { list.add(supplier.get()); } else { throw new IllegalStateException("expected value but got [" + parser.currentToken() + "]"); diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/ConstructingObjectParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/ConstructingObjectParser.java index 1cf2122dcc9..232977b34c6 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/ConstructingObjectParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/ConstructingObjectParser.java @@ -86,8 +86,10 @@ import java.util.function.Function; * Note: if optional constructor arguments aren't specified then the number of allocations is always the worst case. *

      */ -public final class ConstructingObjectParser extends AbstractObjectParser implements - BiFunction, ContextParser{ +public final class ConstructingObjectParser extends AbstractObjectParser + implements + BiFunction, + ContextParser { /** * Consumer that marks a field as a required constructor argument instead of a real object field. @@ -175,7 +177,7 @@ public final class ConstructingObjectParser extends AbstractObje try { return parse(parser, context); } catch (IOException e) { - throw new XContentParseException(parser.getTokenLocation(), "[" + objectParser.getName() + "] failed to parse object", e); + throw new XContentParseException(parser.getTokenLocation(), "[" + objectParser.getName() + "] failed to parse object", e); } } @@ -238,8 +240,11 @@ public final class ConstructingObjectParser extends AbstractObje } @Override - public void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, - ParseField parseField) { + public void declareNamedObject( + BiConsumer consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); } @@ -267,8 +272,11 @@ public final class ConstructingObjectParser extends AbstractObje } @Override - public void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + ParseField parseField + ) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); @@ -297,8 +305,12 @@ public final class ConstructingObjectParser extends AbstractObje } @Override - public void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField parseField + ) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); } @@ -321,12 +333,20 @@ public final class ConstructingObjectParser extends AbstractObje * or expensive lookups whenever the constructor args come in. */ int position = addConstructorArg(consumer, parseField); - objectParser.declareNamedObjects((target, v) -> target.constructorArg(position, v), namedObjectParser, - wrapOrderedModeCallBack(orderedModeCallback), parseField); + objectParser.declareNamedObjects( + (target, v) -> target.constructorArg(position, v), + namedObjectParser, + wrapOrderedModeCallBack(orderedModeCallback), + parseField + ); } else { numberOfFields += 1; - objectParser.declareNamedObjects(queueingConsumer(consumer, parseField), namedObjectParser, - wrapOrderedModeCallBack(orderedModeCallback), parseField); + objectParser.declareNamedObjects( + queueingConsumer(consumer, parseField), + namedObjectParser, + wrapOrderedModeCallBack(orderedModeCallback), + parseField + ); } } @@ -406,8 +426,11 @@ public final class ConstructingObjectParser extends AbstractObje try { consumer.accept(targetObject, v); } catch (Exception e) { - throw new XContentParseException(location, - "[" + objectParser.getName() + "] failed to parse field [" + parseField.getPreferredName() + "]", e); + throw new XContentParseException( + location, + "[" + objectParser.getName() + "] failed to parse field [" + parseField.getPreferredName() + "]", + e + ); } }); }; @@ -479,9 +502,9 @@ public final class ConstructingObjectParser extends AbstractObje * should have just applied the consumer immediately. */ private void queue(Consumer queueMe) { - assert targetObject == null: "Don't queue after the targetObject has been built! Just apply the consumer directly."; + assert targetObject == null : "Don't queue after the targetObject has been built! Just apply the consumer directly."; if (queuedFields == null) { - @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({ "unchecked", "rawtypes" }) Consumer[] queuedFields = new Consumer[numberOfFields]; this.queuedFields = queuedFields; } @@ -521,9 +544,11 @@ public final class ConstructingObjectParser extends AbstractObje * use of ConstructingObjectParser. You should be using ObjectParser instead. Since this is more of a programmer error and the * parser ought to still work we just assert this. */ - assert false == constructorArgInfos.isEmpty() : "[" + objectParser.getName() + "] must configure at least one constructor " - + "argument. If it doesn't have any it should use ObjectParser instead of ConstructingObjectParser. This is a bug " - + "in the parser declaration."; + assert false == constructorArgInfos.isEmpty() : "[" + + objectParser.getName() + + "] must configure at least one constructor " + + "argument. If it doesn't have any it should use ObjectParser instead of ConstructingObjectParser. This is a bug " + + "in the parser declaration."; // All missing constructor arguments were optional. Just build the target and return it. buildTarget(); return targetObject; @@ -540,11 +565,17 @@ public final class ConstructingObjectParser extends AbstractObje queuedFields[queuedFieldsCount].accept(targetObject); } } catch (XContentParseException e) { - throw new XContentParseException(e.getLocation(), - "failed to build [" + objectParser.getName() + "] after last required field arrived", e); + throw new XContentParseException( + e.getLocation(), + "failed to build [" + objectParser.getName() + "] after last required field arrived", + e + ); } catch (Exception e) { - throw new XContentParseException(null, - "Failed to build [" + objectParser.getName() + "] after last required field arrived", e); + throw new XContentParseException( + null, + "Failed to build [" + objectParser.getName() + "] after last required field arrived", + e + ); } } } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/DeprecationHandler.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/DeprecationHandler.java index 991dd3f50b1..e30a21c2f90 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/DeprecationHandler.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/DeprecationHandler.java @@ -49,32 +49,61 @@ public interface DeprecationHandler { @Override public void usedDeprecatedField(String parserName, Supplier location, String usedName, String replacedWith) { if (parserName != null) { - throw new UnsupportedOperationException("deprecated fields not supported in [" + parserName + "] but got [" - + usedName + "] at [" + location.get() + "] which is a deprecated name for [" + replacedWith + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported in [" + + parserName + + "] but got [" + + usedName + + "] at [" + + location.get() + + "] which is a deprecated name for [" + + replacedWith + + "]" + ); } else { - throw new UnsupportedOperationException("deprecated fields not supported here but got [" - + usedName + "] which is a deprecated name for [" + replacedWith + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported here but got [" + usedName + "] which is a deprecated name for [" + replacedWith + "]" + ); } } + @Override public void usedDeprecatedName(String parserName, Supplier location, String usedName, String modernName) { if (parserName != null) { - throw new UnsupportedOperationException("deprecated fields not supported in [" + parserName + "] but got [" - + usedName + "] at [" + location.get() + "] which has been replaced with [" + modernName + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported in [" + + parserName + + "] but got [" + + usedName + + "] at [" + + location.get() + + "] which has been replaced with [" + + modernName + + "]" + ); } else { - throw new UnsupportedOperationException("deprecated fields not supported here but got [" - + usedName + "] which has been replaced with [" + modernName + "]"); + throw new UnsupportedOperationException( + "deprecated fields not supported here but got [" + usedName + "] which has been replaced with [" + modernName + "]" + ); } } @Override public void usedDeprecatedField(String parserName, Supplier location, String usedName) { if (parserName != null) { - throw new UnsupportedOperationException("deprecated fields not supported in [" + parserName + "] but got [" - + usedName + "] at [" + location.get() + "] which has been deprecated entirely"); + throw new UnsupportedOperationException( + "deprecated fields not supported in [" + + parserName + + "] but got [" + + usedName + + "] at [" + + location.get() + + "] which has been deprecated entirely" + ); } else { - throw new UnsupportedOperationException("deprecated fields not supported here but got [" - + usedName + "] which has been deprecated entirely"); + throw new UnsupportedOperationException( + "deprecated fields not supported here but got [" + usedName + "] which has been deprecated entirely" + ); } } }; diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/InstantiatingObjectParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/InstantiatingObjectParser.java index ed4c53c635a..f583fb87816 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/InstantiatingObjectParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/InstantiatingObjectParser.java @@ -75,7 +75,9 @@ import java.util.function.Consumer; * } */ public class InstantiatingObjectParser - implements BiFunction, ContextParser { + implements + BiFunction, + ContextParser { public static Builder builder(String name, boolean ignoreUnknownFields, Class valueClass) { return new Builder<>(name, ignoreUnknownFields, valueClass); @@ -110,12 +112,16 @@ public class InstantiatingObjectParser for (Constructor c : valueClass.getConstructors()) { if (c.getAnnotation(ParserConstructor.class) != null) { if (constructor != null) { - throw new IllegalArgumentException("More then one public constructor with @ParserConstructor annotation exist in " + - "the class " + valueClass.getName()); + throw new IllegalArgumentException( + "More then one public constructor with @ParserConstructor annotation exist in " + + "the class " + + valueClass.getName() + ); } if (c.getParameterCount() != neededArguments) { - throw new IllegalArgumentException("Annotated constructor doesn't have " + neededArguments + - " arguments in the class " + valueClass.getName()); + throw new IllegalArgumentException( + "Annotated constructor doesn't have " + neededArguments + " arguments in the class " + valueClass.getName() + ); } constructor = c; } @@ -125,43 +131,61 @@ public class InstantiatingObjectParser for (Constructor c : valueClass.getConstructors()) { if (c.getParameterCount() == neededArguments) { if (constructor != null) { - throw new IllegalArgumentException("More then one public constructor with " + neededArguments + - " arguments found. The use of @ParserConstructor annotation is required for class " + valueClass.getName()); + throw new IllegalArgumentException( + "More then one public constructor with " + + neededArguments + + " arguments found. The use of @ParserConstructor annotation is required for class " + + valueClass.getName() + ); } constructor = c; } } } if (constructor == null) { - throw new IllegalArgumentException("No public constructors with " + neededArguments + " parameters exist in the class " + - valueClass.getName()); + throw new IllegalArgumentException( + "No public constructors with " + neededArguments + " parameters exist in the class " + valueClass.getName() + ); } this.constructor = (Constructor) constructor; return new InstantiatingObjectParser<>(constructingObjectParser); } @Override - public void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, - ObjectParser.ValueType type) { + public void declareField( + BiConsumer consumer, + ContextParser parser, + ParseField parseField, + ObjectParser.ValueType type + ) { constructingObjectParser.declareField(consumer, parser, parseField, type); } @Override - public void declareNamedObject(BiConsumer consumer, ObjectParser.NamedObjectParser namedObjectParser, - ParseField parseField) { + public void declareNamedObject( + BiConsumer consumer, + ObjectParser.NamedObjectParser namedObjectParser, + ParseField parseField + ) { constructingObjectParser.declareNamedObject(consumer, namedObjectParser, parseField); } @Override - public void declareNamedObjects(BiConsumer> consumer, - ObjectParser.NamedObjectParser namedObjectParser, ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + ObjectParser.NamedObjectParser namedObjectParser, + ParseField parseField + ) { constructingObjectParser.declareNamedObjects(consumer, namedObjectParser, parseField); } @Override - public void declareNamedObjects(BiConsumer> consumer, - ObjectParser.NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField parseField) { + public void declareNamedObjects( + BiConsumer> consumer, + ObjectParser.NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField parseField + ) { constructingObjectParser.declareNamedObjects(consumer, namedObjectParser, orderedModeCallback, parseField); } @@ -182,8 +206,9 @@ public class InstantiatingObjectParser private Value build(Object[] args) { if (constructor == null) { - throw new IllegalArgumentException("InstantiatingObjectParser for type " + valueClass.getName() + " has to be finalized " + - "before the first use"); + throw new IllegalArgumentException( + "InstantiatingObjectParser for type " + valueClass.getName() + " has to be finalized " + "before the first use" + ); } try { return constructor.newInstance(args); @@ -193,7 +218,6 @@ public class InstantiatingObjectParser } } - private final ConstructingObjectParser constructingObjectParser; private InstantiatingObjectParser(ConstructingObjectParser constructingObjectParser) { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/NamedXContentRegistry.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/NamedXContentRegistry.java index 94a29d14ef7..a3195debf58 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/NamedXContentRegistry.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/NamedXContentRegistry.java @@ -75,6 +75,7 @@ public class NamedXContentRegistry { this.name = Objects.requireNonNull(name); this.parser = Objects.requireNonNull((p, c) -> parser.apply(p)); } + /** * Creates a new entry which can be stored by the registry. * Prefer {@link Entry#Entry(Class, ParseField, CheckedFunction)} unless you need a context to carry around while parsing. @@ -112,9 +113,19 @@ public class NamedXContentRegistry { for (String name : entry.name.getAllNamesIncludedDeprecated()) { Object old = parsers.put(name, entry); if (old != null) { - throw new IllegalArgumentException("NamedXContent [" + currentCategory.getName() + "][" + entry.name + "]" + - " is already registered for [" + old.getClass().getName() + "]," + - " cannot register [" + entry.parser.getClass().getName() + "]"); + throw new IllegalArgumentException( + "NamedXContent [" + + currentCategory.getName() + + "][" + + entry.name + + "]" + + " is already registered for [" + + old.getClass().getName() + + "]," + + " cannot register [" + + entry.parser.getClass().getName() + + "]" + ); } } } @@ -147,8 +158,10 @@ public class NamedXContentRegistry { if (false == entry.name.match(name, parser.getDeprecationHandler())) { /* Note that this shouldn't happen because we already looked up the entry using the names but we need to call `match` anyway * because it is responsible for logging deprecation warnings. */ - throw new XContentParseException(parser.getTokenLocation(), - "unable to parse " + categoryClass.getSimpleName() + " with name [" + name + "]: parser didn't match"); + throw new XContentParseException( + parser.getTokenLocation(), + "unable to parse " + categoryClass.getSimpleName() + " with name [" + name + "]: parser didn't match" + ); } return categoryClass.cast(entry.parser.parse(parser, context)); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/ObjectParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/ObjectParser.java index ac3e395361b..71c2dec930e 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/ObjectParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/ObjectParser.java @@ -84,7 +84,9 @@ import static org.opensearch.common.xcontent.XContentParser.Token.VALUE_STRING; * {@link #declareField} which can be used to implement exceptional parsing operations not covered by the high level methods. */ public final class ObjectParser extends AbstractObjectParser - implements BiFunction, ContextParser{ + implements + BiFunction, + ContextParser { private final List requiredFieldSets = new ArrayList<>(); private final List exclusiveFieldSets = new ArrayList<>(); @@ -92,8 +94,10 @@ public final class ObjectParser extends AbstractObjectParser BiConsumer> fromList(Class c, - BiConsumer consumer) { + public static BiConsumer> fromList( + Class c, + BiConsumer consumer + ) { return (Value v, List l) -> { @SuppressWarnings("unchecked") ElementValue[] array = (ElementValue[]) Array.newInstance(c, l.size()); @@ -102,12 +106,18 @@ public final class ObjectParser extends AbstractObjectParser { - void acceptUnknownField(ObjectParser objectParser, String field, XContentLocation location, XContentParser parser, - Value value, Context context) throws IOException; + void acceptUnknownField( + ObjectParser objectParser, + String field, + XContentLocation location, + XContentParser parser, + Value value, + Context context + ) throws IOException; } private static UnknownFieldParser ignoreUnknown() { - return (op, f, l, p, v, c) -> p.skipChildren(); + return (op, f, l, p, v, c) -> p.skipChildren(); } private static UnknownFieldParser errorOnUnknown() { @@ -146,8 +156,10 @@ public final class ObjectParser extends AbstractObjectParser extends AbstractObjectParser unknownFieldParser, - @Nullable Function valueBuilder) { + private ObjectParser( + String name, + UnknownFieldParser unknownFieldParser, + @Nullable Function valueBuilder + ) { this.name = name; this.unknownFieldParser = unknownFieldParser; this.valueBuilder = valueBuilder; @@ -284,7 +299,7 @@ public final class ObjectParser extends AbstractObjectParser extends AbstractObjectParser extends AbstractObjectParser { void parse(XContentParser parser, Value value, Context context) throws IOException; } + public void declareField(Parser p, ParseField parseField, ValueType type) { if (parseField == null) { throw new IllegalArgumentException("[parseField] is required"); @@ -388,8 +404,7 @@ public final class ObjectParser extends AbstractObjectParser void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, - ValueType type) { + public void declareField(BiConsumer consumer, ContextParser parser, ParseField parseField, ValueType type) { if (consumer == null) { throw new IllegalArgumentException("[consumer] is required"); } @@ -399,8 +414,12 @@ public final class ObjectParser extends AbstractObjectParser consumer.accept(v, parser.parse(p, c)), parseField, type); } - public void declareObjectOrDefault(BiConsumer consumer, BiFunction objectParser, - Supplier defaultValue, ParseField field) { + public void declareObjectOrDefault( + BiConsumer consumer, + BiFunction objectParser, + Supplier defaultValue, + ParseField field + ) { declareField((p, v, c) -> { if (p.currentToken() == XContentParser.Token.VALUE_BOOLEAN) { if (p.booleanValue()) { @@ -413,8 +432,7 @@ public final class ObjectParser extends AbstractObjectParser void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, - ParseField field) { + public void declareNamedObject(BiConsumer consumer, NamedObjectParser namedObjectParser, ParseField field) { BiFunction objectParser = (XContentParser p, Context c) -> { try { @@ -439,13 +457,22 @@ public final class ObjectParser extends AbstractObjectParser void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - Consumer orderedModeCallback, ParseField field) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + Consumer orderedModeCallback, + ParseField field + ) { // This creates and parses the named object BiFunction objectParser = (XContentParser p, Context c) -> { if (p.currentToken() != XContentParser.Token.FIELD_NAME) { - throw new XContentParseException(p.getTokenLocation(), "[" + field + "] can be a single object with any number of " - + "fields or an array where each entry is an object with a single field"); + throw new XContentParseException( + p.getTokenLocation(), + "[" + + field + + "] can be a single object with any number of " + + "fields or an array where each entry is an object with a single field" + ); } // This messy exception nesting has the nice side effect of telling the user which field failed to parse try { @@ -472,15 +499,25 @@ public final class ObjectParser extends AbstractObjectParser extends AbstractObjectParser void declareNamedObjects(BiConsumer> consumer, NamedObjectParser namedObjectParser, - ParseField field) { + public void declareNamedObjects( + BiConsumer> consumer, + NamedObjectParser namedObjectParser, + ParseField field + ) { Consumer orderedModeCallback = (v) -> { throw new IllegalArgumentException("[" + field + "] doesn't support arrays. Use a single object with multiple fields."); }; @@ -531,23 +571,26 @@ public final class ObjectParser extends AbstractObjectParser extends AbstractObjectParser extends AbstractObjectParser 0) { - deprecated = ", deprecated_names=" + Arrays.toString(deprecatedNames); + deprecated = ", deprecated_names=" + Arrays.toString(deprecatedNames); } - return "FieldParser{" + - "preferred_name=" + parseField.getPreferredName() + - ", supportedTokens=" + supportedTokens + - deprecated + - (allReplacedWith == null ? "" : ", replaced_with=" + allReplacedWith) + - ", type=" + type.name() + - '}'; + return "FieldParser{" + + "preferred_name=" + + parseField.getPreferredName() + + ", supportedTokens=" + + supportedTokens + + deprecated + + (allReplacedWith == null ? "" : ", replaced_with=" + allReplacedWith) + + ", type=" + + type.name() + + '}'; } } @@ -678,9 +728,6 @@ public final class ObjectParser extends AbstractObjectParser Objects.requireNonNull(value, - "invalid null xcontent transformer for human readable class " + key)); - dateTransformers.forEach((key, value) -> Objects.requireNonNull(value, - "invalid null xcontent date transformer for class " + key)); + addlWriters.forEach((key, value) -> Objects.requireNonNull(value, "invalid null xcontent writer for class " + key)); + addlTransformers.forEach( + (key, value) -> Objects.requireNonNull(value, "invalid null xcontent transformer for human readable class " + key) + ); + dateTransformers.forEach( + (key, value) -> Objects.requireNonNull(value, "invalid null xcontent date transformer for class " + key) + ); writers.putAll(addlWriters); humanReadableTransformer.putAll(addlTransformers); @@ -599,7 +600,6 @@ public final class XContentBuilder implements Closeable, Flushable { return this; } - //////////////////////////////////////////////////////////////////////////// // BigDecimal ////////////////////////////////// @@ -717,7 +717,6 @@ public final class XContentBuilder implements Closeable, Flushable { return this; } - //////////////////////////////////////////////////////////////////////////// // Date ////////////////////////////////// @@ -738,8 +737,7 @@ public final class XContentBuilder implements Closeable, Flushable { * {@link Long} class. */ public XContentBuilder timeField(String name, String readableName, long value) throws IOException { - assert name.equals(readableName) == false : - "expected raw and readable field names to differ, but they were both: " + name; + assert name.equals(readableName) == false : "expected raw and readable field names to differ, but they were both: " + name; if (humanReadable) { Function longTransformer = DATE_TRANSFORMERS.get(Long.class); if (longTransformer == null) { @@ -829,7 +827,7 @@ public final class XContentBuilder implements Closeable, Flushable { if (writer != null) { writer.write(this, value); } else if (value instanceof Path) { - //Path implements Iterable and causes endless recursion and a StackOverFlow if treated as an Iterable here + // Path implements Iterable and causes endless recursion and a StackOverFlow if treated as an Iterable here value((Path) value); } else if (value instanceof Map) { @SuppressWarnings("unchecked") @@ -925,7 +923,7 @@ public final class XContentBuilder implements Closeable, Flushable { } if (values instanceof Path) { - //treat as single value + // treat as single value value((Path) values); } else { // checks that the iterable does not contain references to itself because @@ -952,8 +950,8 @@ public final class XContentBuilder implements Closeable, Flushable { ////////////////////////////////// public XContentBuilder humanReadableField(String rawFieldName, String readableFieldName, Object value) throws IOException { - assert rawFieldName.equals(readableFieldName) == false : - "expected raw and readable field names to differ, but they were both: " + rawFieldName; + assert rawFieldName.equals(readableFieldName) == false : "expected raw and readable field names to differ, but they were both: " + + rawFieldName; if (humanReadable) { field(readableFieldName, Objects.toString(value)); } @@ -971,10 +969,9 @@ public final class XContentBuilder implements Closeable, Flushable { // Misc. ////////////////////////////////// - public XContentBuilder percentageField(String rawFieldName, String readableFieldName, double percentage) throws IOException { - assert rawFieldName.equals(readableFieldName) == false : - "expected raw and readable field names to differ, but they were both: " + rawFieldName; + assert rawFieldName.equals(readableFieldName) == false : "expected raw and readable field names to differ, but they were both: " + + rawFieldName; if (humanReadable) { field(readableFieldName, String.format(Locale.ROOT, "%1.1f%%", percentage)); } @@ -1057,7 +1054,7 @@ public final class XContentBuilder implements Closeable, Flushable { return null; } if (value instanceof Map) { - return ((Map) value).values(); + return ((Map) value).values(); } else if ((value instanceof Iterable) && (value instanceof Path == false)) { return (Iterable) value; } else if (value instanceof Object[]) { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java index 05e3ab31e3c..63b1ef6af75 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentFactory.java @@ -167,9 +167,9 @@ public class XContentFactory { } // Should we throw a failure here? Smile idea is to use it in bytes.... if (length > 2 - && first == SmileConstants.HEADER_BYTE_1 - && content.charAt(1) == SmileConstants.HEADER_BYTE_2 - && content.charAt(2) == SmileConstants.HEADER_BYTE_3) { + && first == SmileConstants.HEADER_BYTE_1 + && content.charAt(1) == SmileConstants.HEADER_BYTE_2 + && content.charAt(2) == SmileConstants.HEADER_BYTE_3) { return XContentType.SMILE; } if (length > 2 && first == '-' && content.charAt(1) == '-' && content.charAt(2) == '-') { @@ -311,9 +311,9 @@ public class XContentFactory { return XContentType.JSON; } if (length > 2 - && first == SmileConstants.HEADER_BYTE_1 - && bytes[offset + 1] == SmileConstants.HEADER_BYTE_2 - && bytes[offset + 2] == SmileConstants.HEADER_BYTE_3) { + && first == SmileConstants.HEADER_BYTE_1 + && bytes[offset + 1] == SmileConstants.HEADER_BYTE_2 + && bytes[offset + 2] == SmileConstants.HEADER_BYTE_3) { return XContentType.SMILE; } if (length > 2 && first == '-' && bytes[offset + 1] == '-' && bytes[offset + 2] == '-') { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentParser.java index 9c70f7c4c3d..4c924cd8a0b 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentParser.java @@ -130,7 +130,12 @@ public interface XContentParser extends Closeable { } enum NumberType { - INT, BIG_INTEGER, LONG, FLOAT, DOUBLE, BIG_DECIMAL + INT, + BIG_INTEGER, + LONG, + FLOAT, + DOUBLE, + BIG_DECIMAL } XContentType contentType(); @@ -158,8 +163,8 @@ public interface XContentParser extends Closeable { * @param map value type * @return {@link Map} object */ - Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException; + Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException; List list() throws IOException; diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentSubParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentSubParser.java index f79b659f210..62d7661892e 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentSubParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentSubParser.java @@ -122,8 +122,8 @@ public class XContentSubParser implements XContentParser { } @Override - public Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException { + public Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException { return parser.map(mapFactory, mapValueParser); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java index 25e8b71a42c..b0986d603ef 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentType.java @@ -163,8 +163,7 @@ public enum XContentType { */ private static String removeVersionInMediaType(String mediaType) { if (mediaType.contains("vnd.opensearch")) { - return mediaType.replaceAll("vnd.opensearch\\+", "") - .replaceAll("\\s*;\\s*compatible-with=\\d+", ""); + return mediaType.replaceAll("vnd.opensearch\\+", "").replaceAll("\\s*;\\s*compatible-with=\\d+", ""); } return mediaType; } @@ -190,9 +189,9 @@ public enum XContentType { } private static boolean isSameMediaTypeOrFormatAs(String stringType, XContentType type) { - return type.mediaTypeWithoutParameters().equalsIgnoreCase(stringType) || - stringType.toLowerCase(Locale.ROOT).startsWith(type.mediaTypeWithoutParameters().toLowerCase(Locale.ROOT) + ";") || - type.shortName().equalsIgnoreCase(stringType); + return type.mediaTypeWithoutParameters().equalsIgnoreCase(stringType) + || stringType.toLowerCase(Locale.ROOT).startsWith(type.mediaTypeWithoutParameters().toLowerCase(Locale.ROOT) + ";") + || type.shortName().equalsIgnoreCase(stringType); } private int index; diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentUtils.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentUtils.java index 1c270ae2cee..6bb6c9ad099 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentUtils.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/XContentUtils.java @@ -36,8 +36,7 @@ import java.io.IOException; public final class XContentUtils { - private XContentUtils() { - } + private XContentUtils() {} /** * Convert a {@link XContentParser.Token} to a value diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java index 301cbafd569..674b6566bde 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContent.java @@ -72,8 +72,7 @@ public class CborXContent implements XContent { cborXContent = new CborXContent(); } - private CborXContent() { - } + private CborXContent() {} @Override public XContentType type() { @@ -91,32 +90,37 @@ public class CborXContent implements XContent { } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(is)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(data)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(data, offset, length)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new CborXContentParser(xContentRegistry, deprecationHandler, cborFactory.createParser(reader)); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContentParser.java index 4882dc5ab8c..d36273a46df 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/cbor/CborXContentParser.java @@ -40,8 +40,7 @@ import org.opensearch.common.xcontent.json.JsonXContentParser; public class CborXContentParser extends JsonXContentParser { - public CborXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public CborXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, parser); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java index 79faa9b1569..a7872904b11 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContent.java @@ -58,6 +58,7 @@ public class JsonXContent implements XContent { public static XContentBuilder contentBuilder() throws IOException { return XContentBuilder.builder(jsonXContent); } + private static final JsonFactory jsonFactory; public static final JsonXContent jsonXContent; @@ -73,8 +74,7 @@ public class JsonXContent implements XContent { jsonXContent = new JsonXContent(); } - private JsonXContent() { - } + private JsonXContent() {} @Override public XContentType type() { @@ -92,32 +92,37 @@ public class JsonXContent implements XContent { } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(is)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(data)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(data, offset, length)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new JsonXContentParser(xContentRegistry, deprecationHandler, jsonFactory.createParser(reader)); } } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java index 5fc80e06742..b972f52c572 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentGenerator.java @@ -180,7 +180,6 @@ public class JsonXContentGenerator implements XContentGenerator { generator.writeEndObject(); } - @Override public void writeStartArray() throws IOException { generator.writeStartArray(); @@ -350,11 +349,13 @@ public class JsonXContentGenerator implements XContentGenerator { public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException { if (mayWriteRawData(contentType) == false) { // EMPTY is safe here because we never call namedObject when writing raw data - try (XContentParser parser = XContentFactory.xContent(contentType) + try ( + XContentParser parser = XContentFactory.xContent(contentType) // It's okay to pass the throwing deprecation handler // because we should not be writing raw fields when // generating JSON - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content)) { + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content) + ) { parser.nextToken(); writeFieldName(name); copyCurrentStructure(parser); @@ -387,10 +388,7 @@ public class JsonXContentGenerator implements XContentGenerator { // or the content is in a different format than the current generator, // we need to copy the whole structure so that it will be correctly // filtered or converted - return supportsRawWrites() - && isFiltered() == false - && contentType == contentType() - && prettyPrint == false; + return supportsRawWrites() && isFiltered() == false && contentType == contentType() && prettyPrint == false; } /** Whether this generator supports writing raw data directly */ @@ -400,10 +398,12 @@ public class JsonXContentGenerator implements XContentGenerator { protected void copyRawValue(InputStream stream, XContent xContent) throws IOException { // EMPTY is safe here because we never call namedObject - try (XContentParser parser = xContent - // It's okay to pass the throwing deprecation handler because we - // should not be writing raw fields when generating JSON - .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream)) { + try ( + XContentParser parser = xContent + // It's okay to pass the throwing deprecation handler because we + // should not be writing raw fields when generating JSON + .createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, stream) + ) { copyCurrentStructure(parser); } } @@ -465,7 +465,7 @@ public class JsonXContentGenerator implements XContentGenerator { return; } JsonStreamContext context = generator.getOutputContext(); - if ((context != null) && (context.inRoot() == false)) { + if ((context != null) && (context.inRoot() == false)) { throw new IOException("Unclosed object or array found"); } if (writeLineFeedAtEnd) { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentParser.java index 7370ec52600..8639e50214d 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/json/JsonXContentParser.java @@ -49,8 +49,7 @@ public class JsonXContentParser extends AbstractXContentParser { final JsonParser parser; - public JsonXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public JsonXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler); this.parser = parser; } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java index f70f7332a5d..82925c5f84f 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContent.java @@ -74,8 +74,7 @@ public class SmileXContent implements XContent { smileXContent = new SmileXContent(); } - private SmileXContent() { - } + private SmileXContent() {} @Override public XContentType type() { @@ -93,32 +92,37 @@ public class SmileXContent implements XContent { } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(is)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(data)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(data, offset, length)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new SmileXContentParser(xContentRegistry, deprecationHandler, smileFactory.createParser(reader)); } } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContentParser.java index 43b2f7fbc5e..84c4d1a2112 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/smile/SmileXContentParser.java @@ -40,8 +40,7 @@ import org.opensearch.common.xcontent.json.JsonXContentParser; public class SmileXContentParser extends JsonXContentParser { - public SmileXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public SmileXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, parser); } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/AbstractXContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/AbstractXContentParser.java index fd45a85486b..369e8edc829 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/AbstractXContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/AbstractXContentParser.java @@ -62,8 +62,8 @@ public abstract class AbstractXContentParser implements XContentParser { private static void checkCoerceString(boolean coerce, Class clazz) { if (!coerce) { - //Need to throw type IllegalArgumentException as current catch logic in - //NumberFieldMapper.parseCreateField relies on this for "malformed" value detection + // Need to throw type IllegalArgumentException as current catch logic in + // NumberFieldMapper.parseCreateField relies on this for "malformed" value detection throw new IllegalArgumentException(clazz.getSimpleName() + " value passed as String"); } } @@ -77,7 +77,7 @@ public abstract class AbstractXContentParser implements XContentParser { } // The 3rd party parsers we rely on are known to silently truncate fractions: see - // http://fasterxml.github.io/jackson-core/javadoc/2.3.0/com/fasterxml/jackson/core/JsonParser.html#getShortValue() + // http://fasterxml.github.io/jackson-core/javadoc/2.3.0/com/fasterxml/jackson/core/JsonParser.html#getShortValue() // If this behaviour is flagged as undesirable and any truncation occurs // then this method is called to trigger the"malformed" handling logic void ensureNumberConversion(boolean coerce, long result, Class clazz) throws IOException { @@ -185,8 +185,8 @@ public abstract class AbstractXContentParser implements XContentParser { final BigInteger bigIntegerValue; try { final BigDecimal bigDecimalValue = new BigDecimal(stringValue); - if (bigDecimalValue.compareTo(BIGDECIMAL_GREATER_THAN_LONG_MAX_VALUE) >= 0 || - bigDecimalValue.compareTo(BIGDECIMAL_LESS_THAN_LONG_MIN_VALUE) <= 0) { + if (bigDecimalValue.compareTo(BIGDECIMAL_GREATER_THAN_LONG_MAX_VALUE) >= 0 + || bigDecimalValue.compareTo(BIGDECIMAL_LESS_THAN_LONG_MIN_VALUE) <= 0) { throw new IllegalArgumentException("Value [" + stringValue + "] is out of range for a long"); } bigIntegerValue = coerce ? bigDecimalValue.toBigInteger() : bigDecimalValue.toBigIntegerExact(); @@ -240,7 +240,6 @@ public abstract class AbstractXContentParser implements XContentParser { protected abstract float doFloatValue() throws IOException; - @Override public double doubleValue() throws IOException { return doubleValue(DEFAULT_NUMBER_COERCE_POLICY); @@ -290,8 +289,8 @@ public abstract class AbstractXContentParser implements XContentParser { } @Override - public Map map( - Supplier> mapFactory, CheckedFunction mapValueParser) throws IOException { + public Map map(Supplier> mapFactory, CheckedFunction mapValueParser) + throws IOException { final Map map = mapFactory.get(); if (findNonEmptyMapStart(this) == false) { return map; @@ -330,8 +329,11 @@ public abstract class AbstractXContentParser implements XContentParser { } // Read a map without bounds checks from a parser that is assumed to be at the map's first field's name token - private static Map readMapEntries(XContentParser parser, Supplier> mapFactory, - Map map) throws IOException { + private static Map readMapEntries( + XContentParser parser, + Supplier> mapFactory, + Map map + ) throws IOException { assert parser.currentToken() == Token.FIELD_NAME : "Expected field name but saw [" + parser.currentToken() + "]"; do { // Must point to field name @@ -372,8 +374,10 @@ public abstract class AbstractXContentParser implements XContentParser { token = parser.nextToken(); } if (token != XContentParser.Token.START_ARRAY) { - throw new XContentParseException(parser.getTokenLocation(), "Failed to parse list: expecting " - + XContentParser.Token.START_ARRAY + " but got " + token); + throw new XContentParseException( + parser.getTokenLocation(), + "Failed to parse list: expecting " + XContentParser.Token.START_ARRAY + " but got " + token + ); } } @@ -398,22 +402,31 @@ public abstract class AbstractXContentParser implements XContentParser { * @param parser parser to read from * @param mapFactory map factory to use for reading objects */ - private static Object readValueUnsafe(Token currentToken, XContentParser parser, - Supplier> mapFactory) throws IOException { - assert currentToken == parser.currentToken() : "Supplied current token [" + currentToken + - "] is different from actual parser current token [" + parser.currentToken() + "]"; + private static Object readValueUnsafe(Token currentToken, XContentParser parser, Supplier> mapFactory) + throws IOException { + assert currentToken == parser.currentToken() : "Supplied current token [" + + currentToken + + "] is different from actual parser current token [" + + parser.currentToken() + + "]"; switch (currentToken) { - case VALUE_STRING: return parser.text(); - case VALUE_NUMBER: return parser.numberValue(); - case VALUE_BOOLEAN: return parser.booleanValue(); + case VALUE_STRING: + return parser.text(); + case VALUE_NUMBER: + return parser.numberValue(); + case VALUE_BOOLEAN: + return parser.booleanValue(); case START_OBJECT: { final Map map = mapFactory.get(); return parser.nextToken() != Token.FIELD_NAME ? map : readMapEntries(parser, mapFactory, map); } - case START_ARRAY: return readListUnsafe(parser, mapFactory); - case VALUE_EMBEDDED_OBJECT: return parser.binaryValue(); + case START_ARRAY: + return readListUnsafe(parser, mapFactory); + case VALUE_EMBEDDED_OBJECT: + return parser.binaryValue(); case VALUE_NULL: - default: return null; + default: + return null; } } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/MapXContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/MapXContentParser.java index 6b2bf2a963c..b96a3f1db40 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/MapXContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/MapXContentParser.java @@ -54,14 +54,17 @@ public class MapXContentParser extends AbstractXContentParser { private TokenIterator iterator; private boolean closed; - public MapXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Map map, - XContentType xContentType) { + public MapXContentParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + Map map, + XContentType xContentType + ) { super(xContentRegistry, deprecationHandler); this.xContentType = xContentType; this.iterator = new MapIterator(null, null, map); } - @Override protected boolean doBooleanValue() throws IOException { if (iterator != null && iterator.currentValue() instanceof Boolean) { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPath.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPath.java index 2c201777f33..be7778097b4 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPath.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPath.java @@ -25,7 +25,6 @@ * under the License. */ - /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. @@ -108,7 +107,7 @@ public class FilterPath { private static FilterPath parse(final String filter, final String segment) { int end = segment.length(); - for (int i = 0; i < end; ) { + for (int i = 0; i < end;) { char c = segment.charAt(i); if (c == '.') { diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPathBasedFilter.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPathBasedFilter.java index fb0b418cb13..0463caaa931 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPathBasedFilter.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/support/filtering/FilterPathBasedFilter.java @@ -101,7 +101,6 @@ public class FilterPathBasedFilter extends TokenFilter { return NO_MATCHING; } - @Override public TokenFilter includeProperty(String name) { TokenFilter filter = evaluate(name, filters); diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java index 9d60bc4efd2..8ebdeca71c0 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContent.java @@ -67,8 +67,7 @@ public class YamlXContent implements XContent { yamlXContent = new YamlXContent(); } - private YamlXContent() { - } + private YamlXContent() {} @Override public XContentType type() { @@ -86,32 +85,37 @@ public class YamlXContent implements XContent { } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, String content) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, String content) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(content)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, InputStream is) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, InputStream is) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(is)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, byte[] data) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(data)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, byte[] data, int offset, int length) throws IOException { + public XContentParser createParser( + NamedXContentRegistry xContentRegistry, + DeprecationHandler deprecationHandler, + byte[] data, + int offset, + int length + ) throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(data, offset, length)); } @Override - public XContentParser createParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, Reader reader) throws IOException { + public XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, Reader reader) + throws IOException { return new YamlXContentParser(xContentRegistry, deprecationHandler, yamlFactory.createParser(reader)); } } diff --git a/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContentParser.java b/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContentParser.java index fe8606bee64..cc523dc6f97 100644 --- a/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContentParser.java +++ b/libs/x-content/src/main/java/org/opensearch/common/xcontent/yaml/YamlXContentParser.java @@ -40,8 +40,7 @@ import org.opensearch.common.xcontent.json.JsonXContentParser; public class YamlXContentParser extends JsonXContentParser { - public YamlXContentParser(NamedXContentRegistry xContentRegistry, - DeprecationHandler deprecationHandler, JsonParser parser) { + public YamlXContentParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, JsonParser parser) { super(xContentRegistry, deprecationHandler, parser); } diff --git a/libs/x-content/src/test/java/org/opensearch/common/ParseFieldTests.java b/libs/x-content/src/test/java/org/opensearch/common/ParseFieldTests.java index 6507fea43f2..d0d3c123624 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/ParseFieldTests.java +++ b/libs/x-content/src/test/java/org/opensearch/common/ParseFieldTests.java @@ -43,7 +43,7 @@ public class ParseFieldTests extends OpenSearchTestCase { public void testParse() { String name = "foo_bar"; ParseField field = new ParseField(name); - String[] deprecated = new String[]{"barFoo", "bar_foo", "Foobar"}; + String[] deprecated = new String[] { "barFoo", "bar_foo", "Foobar" }; ParseField withDeprecations = field.withDeprecation(deprecated); assertThat(field, not(sameInstance(withDeprecations))); assertThat(field.match(name, LoggingDeprecationHandler.INSTANCE), is(true)); @@ -62,7 +62,7 @@ public class ParseFieldTests extends OpenSearchTestCase { public void testAllDeprecated() { String name = "like_text"; - String[] deprecated = new String[]{"text", "same_as_text"}; + String[] deprecated = new String[] { "text", "same_as_text" }; ParseField field = new ParseField(name).withDeprecation(deprecated).withAllDeprecated("like"); assertFalse(field.match("not a field name", LoggingDeprecationHandler.INSTANCE)); assertTrue(field.match("text", LoggingDeprecationHandler.INSTANCE)); @@ -75,7 +75,7 @@ public class ParseFieldTests extends OpenSearchTestCase { public void testDeprecatedWithNoReplacement() { String name = "dep"; - String[] alternatives = new String[]{"old_dep", "new_dep"}; + String[] alternatives = new String[] { "old_dep", "new_dep" }; ParseField field = new ParseField(name).withDeprecation(alternatives).withAllDeprecated(); assertFalse(field.match("not a field name", LoggingDeprecationHandler.INSTANCE)); assertTrue(field.match("dep", LoggingDeprecationHandler.INSTANCE)); @@ -85,7 +85,6 @@ public class ParseFieldTests extends OpenSearchTestCase { assertTrue(field.match("new_dep", LoggingDeprecationHandler.INSTANCE)); assertWarnings("Deprecated field [new_dep] used, this field is unused and will be removed entirely"); - } public void testGetAllNamesIncludedDeprecated() { diff --git a/libs/x-content/src/test/java/org/opensearch/common/xcontent/ConstructingObjectParserTests.java b/libs/x-content/src/test/java/org/opensearch/common/xcontent/ConstructingObjectParserTests.java index a675283e826..d555f3c60bd 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/xcontent/ConstructingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/opensearch/common/xcontent/ConstructingObjectParserTests.java @@ -57,22 +57,40 @@ import static org.hamcrest.Matchers.nullValue; public class ConstructingObjectParserTests extends OpenSearchTestCase { public void testNullDeclares() { ConstructingObjectParser objectParser = new ConstructingObjectParser<>("foo", a -> null); - Exception e = expectThrows(IllegalArgumentException.class, - () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING)); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING) + ); assertEquals("[consumer] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (ContextParser) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (ContextParser) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (CheckedFunction) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (CheckedFunction) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING) + ); assertEquals("[parseField] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, new ParseField("test"), null)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, new ParseField("test"), null) + ); assertEquals("[type] is required", e.getMessage()); } @@ -107,8 +125,9 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { public void testMissingAllConstructorArgs() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1 }"); - ConstructingObjectParser objectParser = randomBoolean() ? HasCtorArguments.PARSER - : HasCtorArguments.PARSER_VEGETABLE_OPTIONAL; + ConstructingObjectParser objectParser = randomBoolean() + ? HasCtorArguments.PARSER + : HasCtorArguments.PARSER_VEGETABLE_OPTIONAL; IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null)); if (objectParser == HasCtorArguments.PARSER) { assertEquals("Required [animal, vegetable]", e.getMessage()); @@ -125,16 +144,17 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { public void testMissingSecondConstructorArg() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"animal\": \"cat\" }"); - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> HasCtorArguments.PARSER.apply(parser, null)); + IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> HasCtorArguments.PARSER.apply(parser, null)); assertEquals("Required [vegetable]", e.getMessage()); } public void testMissingSecondConstructorArgButNotRequired() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"animal\": \"cat\" }"); @SuppressWarnings("unchecked") - HasCtorArguments parsed = randomFrom(HasCtorArguments.PARSER_VEGETABLE_OPTIONAL, HasCtorArguments.PARSER_ALL_OPTIONAL).apply(parser, - null); + HasCtorArguments parsed = randomFrom(HasCtorArguments.PARSER_VEGETABLE_OPTIONAL, HasCtorArguments.PARSER_ALL_OPTIONAL).apply( + parser, + null + ); assertEquals(1, parsed.mineral); assertEquals("cat", parsed.animal); } @@ -142,8 +162,10 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { public void testMissingFirstConstructorArg() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"mineral\": 1, \"vegetable\": 2 }"); @SuppressWarnings("unchecked") - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, - () -> randomFrom(HasCtorArguments.PARSER, HasCtorArguments.PARSER_VEGETABLE_OPTIONAL).apply(parser, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> randomFrom(HasCtorArguments.PARSER, HasCtorArguments.PARSER_VEGETABLE_OPTIONAL).apply(parser, null) + ); assertEquals("Required [animal]", e.getMessage()); } @@ -161,12 +183,16 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { // included in the exception "{\n" + " \"animal\": \"cat\",\n" + " \"vegetable\": 2,\n" + " \"a\": \"supercalifragilisticexpialidocious\"\n" + "}" ); - XContentParseException e = expectThrows(XContentParseException.class, - () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null) + ); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [a]")); assertEquals(4, e.getLineNumber()); - assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", - e.getCause().getMessage()); + assertEquals( + "[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", + e.getCause().getMessage() + ); } public void testBadParamBeforeObjectBuilt() throws IOException { @@ -176,8 +202,10 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { // included in the exception "{\n" + " \"a\": \"supercalifragilisticexpialidocious\",\n" + " \"animal\": \"cat\"\n," + " \"vegetable\": 2\n" + "}" ); - XContentParseException e = expectThrows(XContentParseException.class, - () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null)); + XContentParseException e = expectThrows( + XContentParseException.class, + () -> randomFrom(HasCtorArguments.ALL_PARSERS).apply(parser, null) + ); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [vegetable]")); assertEquals(4, e.getLineNumber()); e = (XContentParseException) e.getCause(); @@ -186,21 +214,27 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { e = (XContentParseException) e.getCause(); assertThat(e.getMessage(), containsString("[has_required_arguments] failed to parse field [a]")); assertEquals(2, e.getLineNumber()); - assertEquals("[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", - e.getCause().getMessage()); + assertEquals( + "[a] must be less than 10 characters in length but was [supercalifragilisticexpialidocious]", + e.getCause().getMessage() + ); } public void testConstructorArgsMustBeConfigured() throws IOException { - class NoConstructorArgs { - } + class NoConstructorArgs {} ConstructingObjectParser parser = new ConstructingObjectParser<>( - "constructor_args_required", (a) -> new NoConstructorArgs()); + "constructor_args_required", + (a) -> new NoConstructorArgs() + ); try { parser.apply(createParser(JsonXContent.jsonXContent, "{}"), null); fail("Expected AssertionError"); } catch (AssertionError e) { - assertEquals("[constructor_args_required] must configure at least one constructor argument. If it doesn't have any it should " - + "use ObjectParser instead of ConstructingObjectParser. This is a bug in the parser declaration.", e.getMessage()); + assertEquals( + "[constructor_args_required] must configure at least one constructor argument. If it doesn't have any it should " + + "use ObjectParser instead of ConstructingObjectParser. This is a bug in the parser declaration.", + e.getMessage() + ); } } @@ -220,13 +254,16 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { } boolean fooSet = false; + void setFoo(String foo) { assertFalse(fooSet); fooSet = true; } } - ConstructingObjectParser parser = new ConstructingObjectParser<>("one_time_test", - (a) -> new CalledOneTime((String) a[0])); + ConstructingObjectParser parser = new ConstructingObjectParser<>( + "one_time_test", + (a) -> new CalledOneTime((String) a[0]) + ); parser.declareString(CalledOneTime::setFoo, new ParseField("foo")); parser.declareString(ctorArgOptional ? optionalConstructorArg() : constructorArg(), new ParseField("yeah")); @@ -252,12 +289,16 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { XContentParser parser = createParser(JsonXContent.jsonXContent, "{ \"test\" : \"foo\", \"junk\" : 2 }"); class TestStruct { public final String test; + TestStruct(String test) { this.test = test; } } - ConstructingObjectParser objectParser = new ConstructingObjectParser<>("foo", true, a -> - new TestStruct((String) a[0])); + ConstructingObjectParser objectParser = new ConstructingObjectParser<>( + "foo", + true, + a -> new TestStruct((String) a[0]) + ); objectParser.declareString(constructorArg(), new ParseField("test")); TestStruct s = objectParser.apply(parser, null); assertEquals(s.test, "foo"); @@ -351,14 +392,16 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { public static final ConstructingObjectParser PARSER_ALL_OPTIONAL = buildParser(false, false); public static final List> ALL_PARSERS = unmodifiableList( - Arrays.asList(PARSER, PARSER_VEGETABLE_OPTIONAL, PARSER_ALL_OPTIONAL)); + Arrays.asList(PARSER, PARSER_VEGETABLE_OPTIONAL, PARSER_ALL_OPTIONAL) + ); public static final ConstructingObjectParser PARSER_INT_CONTEXT = buildContextParser(); - private static ConstructingObjectParser buildParser(boolean animalRequired, - boolean vegetableRequired) { + private static ConstructingObjectParser buildParser(boolean animalRequired, boolean vegetableRequired) { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "has_required_arguments", a -> new HasCtorArguments((String) a[0], (Integer) a[1])); + "has_required_arguments", + a -> new HasCtorArguments((String) a[0], (Integer) a[1]) + ); parser.declareString(animalRequired ? constructorArg() : optionalConstructorArg(), new ParseField("animal")); parser.declareInt(vegetableRequired ? constructorArg() : optionalConstructorArg(), new ParseField("vegetable")); declareSetters(parser); @@ -367,7 +410,10 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { private static ConstructingObjectParser buildContextParser() { ConstructingObjectParser parser = new ConstructingObjectParser<>( - "has_required_arguments", false, (args, ctx) -> new HasCtorArguments((String) args[0], ctx)); + "has_required_arguments", + false, + (args, ctx) -> new HasCtorArguments((String) args[0], ctx) + ); parser.declareString(constructorArg(), new ParseField("animal")); declareSetters(parser); return parser; @@ -416,9 +462,13 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectTwoFieldsInArrayConstructorArg() throws IOException { @@ -428,27 +478,39 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] can be a single object with any number of fields " - + "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named_in_constructor] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectNoFieldsInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {} ], \"named_in_constructor\": [ {\"a\": {}} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectNoFieldsInArrayConstructorArg() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ], \"named_in_constructor\": [ {} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] can be a single object with any number of fields " - + "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named_in_constructor] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectJunkInArray() throws IOException { @@ -458,9 +520,13 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectJunkInArrayConstructorArg() throws IOException { @@ -470,9 +536,13 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { ); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] can be a single object with any number of fields " - + "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named_in_constructor] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectInOrderNotSupported() throws IOException { @@ -483,10 +553,15 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above @SuppressWarnings("unchecked") - ConstructingObjectParser objectParser = new ConstructingObjectParser<>("named_object_holder", - a -> new NamedObjectHolder(((List) a[0]))); - objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, - new ParseField("named_in_constructor")); + ConstructingObjectParser objectParser = new ConstructingObjectParser<>( + "named_object_holder", + a -> new NamedObjectHolder(((List) a[0])) + ); + objectParser.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + NamedObject.PARSER, + new ParseField("named_in_constructor") + ); objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named")); // Now firing the xml through it fails @@ -503,28 +578,45 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above @SuppressWarnings("unchecked") - ConstructingObjectParser objectParser = new ConstructingObjectParser<>("named_object_holder", - a -> new NamedObjectHolder(((List) a[0]))); - objectParser.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, - new ParseField("named_in_constructor")); + ConstructingObjectParser objectParser = new ConstructingObjectParser<>( + "named_object_holder", + a -> new NamedObjectHolder(((List) a[0])) + ); + objectParser.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + NamedObject.PARSER, + new ParseField("named_in_constructor") + ); objectParser.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named")); // Now firing the xml through it fails XContentParseException e = expectThrows(XContentParseException.class, () -> objectParser.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_object_holder] failed to parse field [named_in_constructor]")); - assertThat(e.getCause().getMessage(), - containsString("[named_in_constructor] doesn't support arrays. Use a single object with multiple fields.")); + assertThat( + e.getCause().getMessage(), + containsString("[named_in_constructor] doesn't support arrays. Use a single object with multiple fields.") + ); } static class NamedObjectHolder { @SuppressWarnings("unchecked") - public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>("named_object_holder", - a -> new NamedObjectHolder(((List) a[0]))); + public static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "named_object_holder", + a -> new NamedObjectHolder(((List) a[0])) + ); static { - PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), NamedObject.PARSER, NamedObjectHolder::keepNamedInOrder, - new ParseField("named_in_constructor")); - PARSER.declareNamedObjects(NamedObjectHolder::setNamed, NamedObject.PARSER, NamedObjectHolder::keepNamedInOrder, - new ParseField("named")); + PARSER.declareNamedObjects( + ConstructingObjectParser.constructorArg(), + NamedObject.PARSER, + NamedObjectHolder::keepNamedInOrder, + new ParseField("named_in_constructor") + ); + PARSER.declareNamedObjects( + NamedObjectHolder::setNamed, + NamedObject.PARSER, + NamedObjectHolder::keepNamedInOrder, + new ParseField("named") + ); } private List named; @@ -549,10 +641,12 @@ public class ConstructingObjectParserTests extends OpenSearchTestCase { class TestStruct { final String a; final long b; + TestStruct(String a) { this.a = a; this.b = 0; } + TestStruct(long b) { this.a = null; this.b = b; diff --git a/libs/x-content/src/test/java/org/opensearch/common/xcontent/InstantiatingObjectParserTests.java b/libs/x-content/src/test/java/org/opensearch/common/xcontent/InstantiatingObjectParserTests.java index a3474cde93b..3c34453a636 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/xcontent/InstantiatingObjectParserTests.java +++ b/libs/x-content/src/test/java/org/opensearch/common/xcontent/InstantiatingObjectParserTests.java @@ -77,9 +77,7 @@ public class InstantiatingObjectParserTests extends OpenSearchTestCase { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; NoAnnotations that = (NoAnnotations) o; - return a == that.a && - c == that.c && - Objects.equals(b, that.b); + return a == that.a && c == that.c && Objects.equals(b, that.b); } @Override @@ -114,9 +112,10 @@ public class InstantiatingObjectParserTests extends OpenSearchTestCase { builder.declareInt(constructorArg(), new ParseField("a")); builder.declareString(constructorArg(), new ParseField("b")); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, builder::build); - assertThat(e.getMessage(), containsString( - "More then one public constructor with 2 arguments found. The use of @ParserConstructor annotation is required" - )); + assertThat( + e.getMessage(), + containsString("More then one public constructor with 2 arguments found. The use of @ParserConstructor annotation is required") + ); } public void testPrivateConstructor() { @@ -149,8 +148,7 @@ public class InstantiatingObjectParserTests extends OpenSearchTestCase { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; LonelyArgument that = (LonelyArgument) o; - return a == that.a && - Objects.equals(b, that.b); + return a == that.a && Objects.equals(b, that.b); } @Override @@ -211,9 +209,7 @@ public class InstantiatingObjectParserTests extends OpenSearchTestCase { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Annotations that = (Annotations) o; - return a == that.a && - c == that.c && - Objects.equals(b, that.b); + return a == that.a && c == that.c && Objects.equals(b, that.b); } @Override diff --git a/libs/x-content/src/test/java/org/opensearch/common/xcontent/MapXContentParserTests.java b/libs/x-content/src/test/java/org/opensearch/common/xcontent/MapXContentParserTests.java index 5748faeeb78..380af6ff264 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/xcontent/MapXContentParserTests.java +++ b/libs/x-content/src/test/java/org/opensearch/common/xcontent/MapXContentParserTests.java @@ -75,13 +75,12 @@ public class MapXContentParserTests extends OpenSearchTestCase { builder.endObject(); } builder.endObject(); - builder.field("bytes", new byte[]{1, 2, 3}); + builder.field("bytes", new byte[] { 1, 2, 3 }); builder.nullField("nothing"); builder.endObject(); }); } - public void testRandomObject() throws IOException { compareTokens(builder -> generateRandomObject(builder, randomIntBetween(0, 10))); } @@ -102,8 +101,14 @@ public class MapXContentParserTests extends OpenSearchTestCase { } try (XContentParser parser = createParser(xContentType.xContent(), BytesReference.bytes(builder))) { - try (XContentParser mapParser = new MapXContentParser( - xContentRegistry(), LoggingDeprecationHandler.INSTANCE, map, xContentType)) { + try ( + XContentParser mapParser = new MapXContentParser( + xContentRegistry(), + LoggingDeprecationHandler.INSTANCE, + map, + xContentType + ) + ) { assertEquals(parser.contentType(), mapParser.contentType()); XContentParser.Token token; assertEquals(parser.currentToken(), mapParser.currentToken()); @@ -128,8 +133,8 @@ public class MapXContentParserTests extends OpenSearchTestCase { case VALUE_NUMBER: assertEquals(parser.numberType(), mapParser.numberType()); assertEquals(parser.numberValue(), mapParser.numberValue()); - if (parser.numberType() == XContentParser.NumberType.LONG || - parser.numberType() == XContentParser.NumberType.INT) { + if (parser.numberType() == XContentParser.NumberType.LONG + || parser.numberType() == XContentParser.NumberType.INT) { assertEquals(parser.longValue(), mapParser.longValue()); if (parser.longValue() <= Integer.MAX_VALUE && parser.longValue() >= Integer.MIN_VALUE) { assertEquals(parser.intValue(), mapParser.intValue()); diff --git a/libs/x-content/src/test/java/org/opensearch/common/xcontent/ObjectParserTests.java b/libs/x-content/src/test/java/org/opensearch/common/xcontent/ObjectParserTests.java index 3e6381fd548..7ac11e05f74 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/xcontent/ObjectParserTests.java +++ b/libs/x-content/src/test/java/org/opensearch/common/xcontent/ObjectParserTests.java @@ -70,6 +70,7 @@ public class ObjectParserTests extends OpenSearchTestCase { public String test; int testNumber; List ints = new ArrayList<>(); + public void setTestNumber(int testNumber) { this.testNumber = testNumber; } @@ -88,30 +89,51 @@ public class ObjectParserTests extends OpenSearchTestCase { assertEquals(s.test, "foo"); assertEquals(s.testNumber, 2); assertEquals(s.ints, Arrays.asList(1, 2, 3, 4)); - assertEquals(objectParser.toString(), "ObjectParser{name='foo', fields=[" + assertEquals( + objectParser.toString(), + "ObjectParser{name='foo', fields=[" + "FieldParser{preferred_name=test, supportedTokens=[VALUE_STRING], type=STRING}, " + "FieldParser{preferred_name=test_array, supportedTokens=[START_ARRAY, VALUE_STRING, VALUE_NUMBER], type=INT_ARRAY}, " - + "FieldParser{preferred_name=test_number, supportedTokens=[VALUE_STRING, VALUE_NUMBER], type=INT}]}"); + + "FieldParser{preferred_name=test_number, supportedTokens=[VALUE_STRING, VALUE_NUMBER], type=INT}]}" + ); } public void testNullDeclares() { ObjectParser objectParser = new ObjectParser<>("foo"); - Exception e = expectThrows(IllegalArgumentException.class, - () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING)); + Exception e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField(null, (r, c) -> null, new ParseField("test"), ObjectParser.ValueType.STRING) + ); assertEquals("[consumer] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (ContextParser) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (ContextParser) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (CheckedFunction) null, - new ParseField("test"), ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField( + (o, v) -> {}, + (CheckedFunction) null, + new ParseField("test"), + ObjectParser.ValueType.STRING + ) + ); assertEquals("[parser] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, null, ObjectParser.ValueType.STRING) + ); assertEquals("[parseField] is required", e.getMessage()); - e = expectThrows(IllegalArgumentException.class, () -> objectParser.declareField( - (o, v) -> {}, (r, c) -> null, new ParseField("test"), null)); + e = expectThrows( + IllegalArgumentException.class, + () -> objectParser.declareField((o, v) -> {}, (r, c) -> null, new ParseField("test"), null) + ); assertEquals("[type] is required", e.getMessage()); } @@ -143,16 +165,16 @@ public class ObjectParserTests extends OpenSearchTestCase { String host = ""; int port = 0; XContentParser.Token token; - while (( token = parser.currentToken()) != XContentParser.Token.END_OBJECT) { + while ((token = parser.currentToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { fieldName = parser.currentName(); - } else if (token == XContentParser.Token.VALUE_STRING){ + } else if (token == XContentParser.Token.VALUE_STRING) { if (fieldName.equals("host")) { host = parser.text(); } else { throw new IllegalStateException("boom"); } - } else if (token == XContentParser.Token.VALUE_NUMBER){ + } else if (token == XContentParser.Token.VALUE_NUMBER) { if (fieldName.equals("port")) { port = parser.intValue(); } else { @@ -167,6 +189,7 @@ public class ObjectParserTests extends OpenSearchTestCase { class Foo { public String name; public URI uri; + public void setName(String name) { this.name = name; } @@ -200,15 +223,14 @@ public class ObjectParserTests extends OpenSearchTestCase { objectParser.declareString(Foo::setName, new ParseField("name")); objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url")); Foo s = objectParser.parse(parser, new Foo(), new CustomParseContext(new ClassicParser())); - assertEquals(s.uri.getHost(), "foobar"); - assertEquals(s.uri.getPort(), 80); + assertEquals(s.uri.getHost(), "foobar"); + assertEquals(s.uri.getPort(), 80); assertEquals(s.name, "foobarbaz"); } public void testExceptions() throws IOException { class TestStruct { - public void setTest(int test) { - } + public void setTest(int test) {} } ObjectParser objectParser = new ObjectParser<>("the_parser"); TestStruct s = new TestStruct(); @@ -264,8 +286,7 @@ public class ObjectParserTests extends OpenSearchTestCase { TestStruct s = new TestStruct(); s.object = new TestStruct(); objectParser.declareField((i, c, x) -> c.test = i.intValue(), new ParseField("test"), ValueType.INT); - objectParser.declareField((i, c, x) -> objectParser.parse(parser, c.object, null), new ParseField("object"), - ValueType.OBJECT); + objectParser.declareField((i, c, x) -> objectParser.parse(parser, c.object, null), new ParseField("object"), ValueType.OBJECT); objectParser.parse(parser, s, null); assertEquals(s.test, 1); assertEquals(s.object.test, 2); @@ -334,7 +355,8 @@ public class ObjectParserTests extends OpenSearchTestCase { } enum TestEnum { - FOO, BAR + FOO, + BAR } public void testParseEnumFromString() throws IOException { @@ -435,39 +457,51 @@ public class ObjectParserTests extends OpenSearchTestCase { List string_array_field; boolean null_value; String string_or_null = "adsfsa"; + public void setInt_field(int int_field) { this.int_field = int_field; } + public void setNullableIntField(int nullableIntField) { this.nullableIntField = nullableIntField; } + public void setLong_field(long long_field) { this.long_field = long_field; } + public void setFloat_field(float float_field) { this.float_field = float_field; } + public void setDouble_field(double double_field) { this.double_field = double_field; } + public void setNullableDoubleField(double nullableDoubleField) { this.nullableDoubleField = nullableDoubleField; } + public void setString_field(String string_field) { this.string_field = string_field; } + public void setInt_array_field(List int_array_field) { this.int_array_field = int_array_field; } + public void setLong_array_field(List long_array_field) { this.long_array_field = long_array_field; } + public void setFloat_array_field(List float_array_field) { this.float_array_field = float_array_field; } + public void setDouble_array_field(List double_array_field) { this.double_array_field = double_array_field; } + public void setString_array_field(List string_array_field) { this.string_array_field = string_array_field; } @@ -523,8 +557,7 @@ public class ObjectParserTests extends OpenSearchTestCase { } public void testParseNamedObject() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, - "{\"named\": { \"a\": {\"foo\" : 11} }, \"bar\": \"baz\"}"); + XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": { \"a\": {\"foo\" : 11} }, \"bar\": \"baz\"}"); NamedObjectHolder h = NamedObjectHolder.PARSER.apply(parser, null); assertEquals("a", h.named.name); assertEquals(11, h.named.foo); @@ -557,35 +590,46 @@ public class ObjectParserTests extends OpenSearchTestCase { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}, \"b\": {}}]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectsNoFieldsInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {} ]}"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectsJunkInArray() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ \"junk\" ] }"); XContentParseException e = expectThrows(XContentParseException.class, () -> NamedObjectsHolder.PARSER.apply(parser, null)); assertThat(e.getMessage(), containsString("[named_objects_holder] failed to parse field [named]")); - assertThat(e.getCause().getMessage(), - containsString("[named] can be a single object with any number of fields " + - "or an array where each entry is an object with a single field")); + assertThat( + e.getCause().getMessage(), + containsString( + "[named] can be a single object with any number of fields " + + "or an array where each entry is an object with a single field" + ) + ); } public void testParseNamedObjectsInOrderNotSupported() throws IOException { XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"named\": [ {\"a\": {}} ] }"); // Create our own parser for this test so we can disable support for the "ordered" mode specified by the array above - ObjectParser objectParser = new ObjectParser<>("named_object_holder", - NamedObjectsHolder::new); + ObjectParser objectParser = new ObjectParser<>("named_object_holder", NamedObjectsHolder::new); objectParser.declareNamedObjects(NamedObjectsHolder::setNamed, NamedObject.PARSER, new ParseField("named")); // Now firing the xml through it fails @@ -692,8 +736,12 @@ public class ObjectParserTests extends OpenSearchTestCase { ObjectParser objectParser = new ObjectParser<>("foo"); TestStruct s = new TestStruct(); - objectParser.declareFieldArray(TestStruct::setArray, (p, c) -> XContentParserUtils.parseFieldsValue(p), - new ParseField("test_array"), ValueType.VALUE_ARRAY); + objectParser.declareFieldArray( + TestStruct::setArray, + (p, c) -> XContentParserUtils.parseFieldsValue(p), + new ParseField("test_array"), + ValueType.VALUE_ARRAY + ); objectParser.declareIntArray(TestStruct::setInts, new ParseField("int_array")); objectParser.parse(parser, s, null); assertEquals(s.testArray, Arrays.asList(1, null, "3", 4.2)); @@ -726,50 +774,57 @@ public class ObjectParserTests extends OpenSearchTestCase { public void testNoopDeclareObject() throws IOException { ObjectParser, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); parser.declareString(AtomicReference::set, new ParseField("body")); - parser.declareObject((a,b) -> {}, (p, c) -> null, new ParseField("noop")); + parser.declareObject((a, b) -> {}, (p, c) -> null, new ParseField("noop")); assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); - Exception garbageException = expectThrows(IllegalStateException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"), - null)); + Exception garbageException = expectThrows( + IllegalStateException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": {\"garbage\": \"shouldn't\"}}"), null) + ); assertEquals("parser for [noop] did not end on END_OBJECT", garbageException.getMessage()); - Exception sneakyException = expectThrows(IllegalStateException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"), - null)); + Exception sneakyException = expectThrows( + IllegalStateException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": {\"body\": \"shouldn't\"}}"), null) + ); assertEquals("parser for [noop] did not end on END_OBJECT", sneakyException.getMessage()); } public void testNoopDeclareField() throws IOException { ObjectParser, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); parser.declareString(AtomicReference::set, new ParseField("body")); - parser.declareField((a,b) -> {}, (p, c) -> null, new ParseField("noop"), ValueType.STRING_ARRAY); + parser.declareField((a, b) -> {}, (p, c) -> null, new ParseField("noop"), ValueType.STRING_ARRAY); assertEquals("i", parser.parse(createParser(JsonXContent.jsonXContent, "{\"body\": \"i\"}"), null).get()); - Exception e = expectThrows(IllegalStateException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": [\"ignored\"]}"), - null)); + Exception e = expectThrows( + IllegalStateException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [\"ignored\"]}"), null) + ); assertEquals("parser for [noop] did not end on END_ARRAY", e.getMessage()); } public void testNoopDeclareObjectArray() { ObjectParser, Void> parser = new ObjectParser<>("noopy", AtomicReference::new); parser.declareString(AtomicReference::set, new ParseField("body")); - parser.declareObjectArray((a,b) -> {}, (p, c) -> null, new ParseField("noop")); + parser.declareObjectArray((a, b) -> {}, (p, c) -> null, new ParseField("noop")); - XContentParseException garbageError = expectThrows(XContentParseException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"), - null)); + XContentParseException garbageError = expectThrows( + XContentParseException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"garbage\": \"shouldn't\"}}]"), null) + ); assertEquals("expected value but got [FIELD_NAME]", garbageError.getCause().getMessage()); - XContentParseException sneakyError = expectThrows(XContentParseException.class, () -> parser.parse( - createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"), - null)); + XContentParseException sneakyError = expectThrows( + XContentParseException.class, + () -> parser.parse(createParser(JsonXContent.jsonXContent, "{\"noop\": [{\"body\": \"shouldn't\"}}]"), null) + ); assertEquals("expected value but got [FIELD_NAME]", sneakyError.getCause().getMessage()); } // singular static class NamedObjectHolder { - public static final ObjectParser PARSER = new ObjectParser<>("named_object_holder", - NamedObjectHolder::new); + public static final ObjectParser PARSER = new ObjectParser<>( + "named_object_holder", + NamedObjectHolder::new + ); static { PARSER.declareNamedObject(NamedObjectHolder::setNamed, NamedObject.PARSER, new ParseField("named")); PARSER.declareString(NamedObjectHolder::setBar, new ParseField("bar")); @@ -789,11 +844,17 @@ public class ObjectParserTests extends OpenSearchTestCase { // plural static class NamedObjectsHolder { - public static final ObjectParser PARSER = new ObjectParser<>("named_objects_holder", - NamedObjectsHolder::new); + public static final ObjectParser PARSER = new ObjectParser<>( + "named_objects_holder", + NamedObjectsHolder::new + ); static { - PARSER.declareNamedObjects(NamedObjectsHolder::setNamed, NamedObject.PARSER, NamedObjectsHolder::keepNamedInOrder, - new ParseField("named")); + PARSER.declareNamedObjects( + NamedObjectsHolder::setNamed, + NamedObject.PARSER, + NamedObjectsHolder::keepNamedInOrder, + new ParseField("named") + ); } private List named; @@ -831,16 +892,19 @@ public class ObjectParserTests extends OpenSearchTestCase { private static class ObjectWithArbitraryFields { String name; Map fields = new HashMap<>(); + void setField(String key, Object value) { fields.put(key, value); } + void setName(String name) { this.name = name; } } public void testConsumeUnknownFields() throws IOException { - XContentParser parser = createParser(JsonXContent.jsonXContent, + XContentParser parser = createParser( + JsonXContent.jsonXContent, "{\n" + " \"test\" : \"foo\",\n" + " \"test_number\" : 2,\n" @@ -849,9 +913,13 @@ public class ObjectParserTests extends OpenSearchTestCase { + " \"test_null\" : null,\n" + " \"test_array\": [1,2,3,4],\n" + " \"test_nested\": { \"field\" : \"value\", \"field2\" : [ \"list1\", \"list2\" ] }\n" - + "}"); - ObjectParser op - = new ObjectParser<>("unknown", ObjectWithArbitraryFields::setField, ObjectWithArbitraryFields::new); + + "}" + ); + ObjectParser op = new ObjectParser<>( + "unknown", + ObjectWithArbitraryFields::setField, + ObjectWithArbitraryFields::new + ); op.declareString(ObjectWithArbitraryFields::setName, new ParseField("name")); ObjectWithArbitraryFields o = op.parse(parser, null); @@ -883,7 +951,7 @@ public class ObjectParserTests extends OpenSearchTestCase { ObjectParser objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); TestStruct obj = objectParser.apply(parser, null); assertThat(obj.a, equalTo(123L)); @@ -893,7 +961,7 @@ public class ObjectParserTests extends OpenSearchTestCase { objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); obj = objectParser.apply(parser, null); assertThat(obj.a, nullValue()); @@ -903,7 +971,7 @@ public class ObjectParserTests extends OpenSearchTestCase { objectParser = new ObjectParser<>("foo", true, TestStruct::new); objectParser.declareLong(TestStruct::setA, new ParseField("a")); objectParser.declareLong(TestStruct::setB, new ParseField("b")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); obj = objectParser.apply(parser, null); assertThat(obj.a, equalTo(123L)); @@ -941,12 +1009,14 @@ public class ObjectParserTests extends OpenSearchTestCase { objectParser.declareLong(TestStruct::setB, new ParseField("b")); objectParser.declareLong(TestStruct::setC, new ParseField("c")); objectParser.declareLong(TestStruct::setD, new ParseField("d")); - objectParser.declareRequiredFieldSet(new String[]{"a", "b"}); - objectParser.declareRequiredFieldSet(new String[]{"c", "d"}); + objectParser.declareRequiredFieldSet(new String[] { "a", "b" }); + objectParser.declareRequiredFieldSet(new String[] { "c", "d" }); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectParser.apply(parser, null)); - assertThat(e.getMessage(), equalTo("Required one of fields [a, b], but none were specified. " + - "Required one of fields [c, d], but none were specified. ")); + assertThat( + e.getMessage(), + equalTo("Required one of fields [a, b], but none were specified. " + "Required one of fields [c, d], but none were specified. ") + ); } public void testExclusiveFieldSet() throws IOException { @@ -971,26 +1041,31 @@ public class ObjectParserTests extends OpenSearchTestCase { assertThat(e.getMessage(), containsString("The following fields are not allowed together: [a, b]")); e = expectThrows(IllegalArgumentException.class, () -> parser.parse(badmulti, null)); - assertThat(e.getMessage(), - containsString("allowed together: [a, b] The following fields are not allowed together: [c, d]")); + assertThat(e.getMessage(), containsString("allowed together: [a, b] The following fields are not allowed together: [c, d]")); } @Override protected NamedXContentRegistry xContentRegistry() { - return new NamedXContentRegistry(Arrays.asList( - new NamedXContentRegistry.Entry(Object.class, new ParseField("str"), p -> p.text()), - new NamedXContentRegistry.Entry(Object.class, new ParseField("int"), p -> p.intValue()), - new NamedXContentRegistry.Entry(Object.class, new ParseField("float"), p -> p.floatValue()), - new NamedXContentRegistry.Entry(Object.class, new ParseField("bool"), p -> p.booleanValue()) - )); + return new NamedXContentRegistry( + Arrays.asList( + new NamedXContentRegistry.Entry(Object.class, new ParseField("str"), p -> p.text()), + new NamedXContentRegistry.Entry(Object.class, new ParseField("int"), p -> p.intValue()), + new NamedXContentRegistry.Entry(Object.class, new ParseField("float"), p -> p.floatValue()), + new NamedXContentRegistry.Entry(Object.class, new ParseField("bool"), p -> p.booleanValue()) + ) + ); } private static class TopLevelNamedXConent { public static final ObjectParser PARSER = new ObjectParser<>( - "test", Object.class, TopLevelNamedXConent::setNamed, TopLevelNamedXConent::new + "test", + Object.class, + TopLevelNamedXConent::setNamed, + TopLevelNamedXConent::new ); Object named; + void setNamed(Object named) { if (this.named != null) { throw new IllegalArgumentException("Only one [named] allowed!"); diff --git a/libs/x-content/src/test/java/org/opensearch/common/xcontent/SimpleStruct.java b/libs/x-content/src/test/java/org/opensearch/common/xcontent/SimpleStruct.java index 912d333863e..acc6dc4c8aa 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/xcontent/SimpleStruct.java +++ b/libs/x-content/src/test/java/org/opensearch/common/xcontent/SimpleStruct.java @@ -55,9 +55,11 @@ class SimpleStruct implements ToXContentObject { private static final ParseField S = new ParseField("s"); @SuppressWarnings("unchecked") - private static final ConstructingObjectParser PARSER = - new ConstructingObjectParser<>( - "simple_struct", true, args -> new SimpleStruct((int) args[0], (double) args[1], (String) args[2])); + private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( + "simple_struct", + true, + args -> new SimpleStruct((int) args[0], (double) args[1], (String) args[2]) + ); static { PARSER.declareInt(constructorArg(), I); @@ -77,8 +79,7 @@ class SimpleStruct implements ToXContentObject { @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - return builder - .startObject() + return builder.startObject() .field(I.getPreferredName(), i) .field(D.getPreferredName(), d) .field(S.getPreferredName(), s) @@ -103,4 +104,3 @@ class SimpleStruct implements ToXContentObject { return Strings.toString(this); } } - diff --git a/libs/x-content/src/test/java/org/opensearch/common/xcontent/XContentParserTests.java b/libs/x-content/src/test/java/org/opensearch/common/xcontent/XContentParserTests.java index 9775a1301d9..33dc7f4ba25 100644 --- a/libs/x-content/src/test/java/org/opensearch/common/xcontent/XContentParserTests.java +++ b/libs/x-content/src/test/java/org/opensearch/common/xcontent/XContentParserTests.java @@ -161,8 +161,8 @@ public class XContentParserTests extends OpenSearchTestCase { } public void testMap() throws IOException { - String source = "{\"i\": {\"_doc\": {\"f1\": {\"type\": \"text\", \"analyzer\": \"english\"}, " + - "\"f2\": {\"type\": \"object\", \"properties\": {\"sub1\": {\"type\": \"keyword\", \"foo\": 17}}}}}}"; + String source = "{\"i\": {\"_doc\": {\"f1\": {\"type\": \"text\", \"analyzer\": \"english\"}, " + + "\"f2\": {\"type\": \"object\", \"properties\": {\"sub1\": {\"type\": \"keyword\", \"foo\": 17}}}}}}"; Map f1 = new HashMap<>(); f1.put("type", "text"); f1.put("analyzer", "english"); @@ -269,9 +269,7 @@ public class XContentParserTests extends OpenSearchTestCase { } public void testEmptyList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startArray("some_array").endArray().endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -286,12 +284,14 @@ public class XContentParserTests extends OpenSearchTestCase { } public void testSimpleList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .value(1) - .value(3) - .value(0) - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startArray("some_array") + .value(1) + .value(3) + .value(0) + .endArray() + .endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -306,12 +306,20 @@ public class XContentParserTests extends OpenSearchTestCase { } public void testNestedList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .startArray().endArray() - .startArray().value(1).value(3).endArray() - .startArray().value(2).endArray() - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startArray("some_array") + .startArray() + .endArray() + .startArray() + .value(1) + .value(3) + .endArray() + .startArray() + .value(2) + .endArray() + .endArray() + .endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -321,18 +329,21 @@ public class XContentParserTests extends OpenSearchTestCase { // sometimes read the start array token, sometimes not assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); } - assertEquals( - Arrays.asList(Collections.emptyList(), Arrays.asList(1, 3), Arrays.asList(2)), - parser.list()); + assertEquals(Arrays.asList(Collections.emptyList(), Arrays.asList(1, 3), Arrays.asList(2)), parser.list()); } } public void testNestedMapInList() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject() - .startArray("some_array") - .startObject().field("foo", "bar").endObject() - .startObject().endObject() - .endArray().endObject(); + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .startArray("some_array") + .startObject() + .field("foo", "bar") + .endObject() + .startObject() + .endObject() + .endArray() + .endObject(); try (XContentParser parser = createParser(JsonXContent.jsonXContent, Strings.toString(builder))) { assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); @@ -342,18 +353,16 @@ public class XContentParserTests extends OpenSearchTestCase { // sometimes read the start array token, sometimes not assertEquals(XContentParser.Token.START_ARRAY, parser.nextToken()); } - assertEquals( - Arrays.asList(singletonMap("foo", "bar"), emptyMap()), - parser.list()); + assertEquals(Arrays.asList(singletonMap("foo", "bar"), emptyMap()), parser.list()); } } public void testGenericMap() throws IOException { - String content = "{" + - "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + - "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + - "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + - "}"; + String content = "{" + + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + + "}"; SimpleStruct structA = new SimpleStruct(1, 0.1, "aaa"); SimpleStruct structB = new SimpleStruct(2, 0.2, "bbb"); SimpleStruct structC = new SimpleStruct(3, 0.3, "ccc"); @@ -371,11 +380,11 @@ public class XContentParserTests extends OpenSearchTestCase { } public void testGenericMapOrdered() throws IOException { - String content = "{" + - "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + - "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + - "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + - "}"; + String content = "{" + + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }, " + + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": \"bbb\" }" + + "}"; SimpleStruct structA = new SimpleStruct(1, 0.1, "aaa"); SimpleStruct structB = new SimpleStruct(2, 0.2, "bbb"); SimpleStruct structC = new SimpleStruct(3, 0.3, "ccc"); @@ -394,15 +403,16 @@ public class XContentParserTests extends OpenSearchTestCase { } public void testGenericMap_Failure_MapContainingUnparsableValue() throws IOException { - String content = "{" + - "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + - "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": 666 }, " + - "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }" + - "}"; + String content = "{" + + "\"a\": { \"i\": 1, \"d\": 0.1, \"s\": \"aaa\" }, " + + "\"b\": { \"i\": 2, \"d\": 0.2, \"s\": 666 }, " + + "\"c\": { \"i\": 3, \"d\": 0.3, \"s\": \"ccc\" }" + + "}"; try (XContentParser parser = createParser(JsonXContent.jsonXContent, content)) { XContentParseException exception = expectThrows( XContentParseException.class, - () -> parser.map(HashMap::new, SimpleStruct::fromXContent)); + () -> parser.map(HashMap::new, SimpleStruct::fromXContent) + ); assertThat(exception, hasMessage(containsString("s doesn't support values of type: VALUE_NUMBER"))); } } @@ -433,7 +443,7 @@ public class XContentParserTests extends OpenSearchTestCase { subParser.skipChildren(); } - } finally { + } finally { assertFalse(subParser.isClosed()); subParser.close(); assertTrue(subParser.isClosed()); @@ -478,7 +488,7 @@ public class XContentParserTests extends OpenSearchTestCase { subParser.skipChildren(); } - } finally { + } finally { assertFalse(subParser.isClosed()); subParser.close(); assertTrue(subParser.isClosed()); @@ -503,7 +513,6 @@ public class XContentParserTests extends OpenSearchTestCase { } } - public void testCreateRootSubParser() throws IOException { XContentBuilder builder = XContentFactory.jsonBuilder(); int numberOfTokens = generateRandomObjectForMarking(builder); @@ -529,9 +538,7 @@ public class XContentParserTests extends OpenSearchTestCase { * Returns the number of tokens in the marked field */ private static int generateRandomObjectForMarking(XContentBuilder builder) throws IOException { - builder.startObject() - .field("first_field", "foo") - .field("marked_field"); + builder.startObject().field("first_field", "foo").field("marked_field"); int numberOfTokens = generateRandomObject(builder, 0); builder.field("last_field", "bar").endObject(); return numberOfTokens; @@ -550,37 +557,32 @@ public class XContentParserTests extends OpenSearchTestCase { } private static int generateRandomValue(XContentBuilder builder, int level) throws IOException { - @SuppressWarnings("unchecked") CheckedSupplier fieldGenerator = randomFrom( - () -> { - builder.value(randomInt()); + @SuppressWarnings("unchecked") + CheckedSupplier fieldGenerator = randomFrom(() -> { + builder.value(randomInt()); + return 1; + }, () -> { + builder.value(randomAlphaOfLength(10)); + return 1; + }, () -> { + builder.value(randomDouble()); + return 1; + }, () -> { + if (level < 3) { + // don't need to go too deep + return generateRandomObject(builder, level + 1); + } else { + builder.value(0); return 1; - }, - () -> { - builder.value(randomAlphaOfLength(10)); - return 1; - }, - () -> { - builder.value(randomDouble()); - return 1; - }, - () -> { - if (level < 3) { - // don't need to go too deep - return generateRandomObject(builder, level + 1); - } else { - builder.value(0); - return 1; - } - }, - () -> { - if (level < 5) { // don't need to go too deep - return generateRandomArray(builder, level); - } else { - builder.value(0); - return 1; - } } - ); + }, () -> { + if (level < 5) { // don't need to go too deep + return generateRandomArray(builder, level); + } else { + builder.value(0); + return 1; + } + }); return fieldGenerator.get(); }