diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/DeleteSampleConfigurationActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/DeleteSampleConfigurationActionTests.java index 9727c82eb7178..ba9d4d656bcc4 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/DeleteSampleConfigurationActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/DeleteSampleConfigurationActionTests.java @@ -47,7 +47,7 @@ protected DeleteSampleConfigurationAction.Request mutateInstance(DeleteSampleCon instance.masterNodeTimeout(), instance.ackTimeout() ); - String[] newIndices = randomValueOtherThan(instance.indices(), () -> new String[] { randomAlphaOfLength(10) }); + String[] newIndices = randomArrayOtherThan(instance.indices(), () -> new String[] { randomAlphaOfLength(10) }); mutated.indices(newIndices); yield mutated; } diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/PutSampleConfigurationActionTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/PutSampleConfigurationActionTests.java index ffe446a9cc2b0..a4fc1b91e491c 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/PutSampleConfigurationActionTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/sampling/PutSampleConfigurationActionTests.java @@ -55,7 +55,7 @@ protected PutSampleConfigurationAction.Request mutateInstance(PutSampleConfigura instance.masterNodeTimeout(), instance.ackTimeout() ); - mutated.indices(randomValueOtherThan(instance.indices(), () -> new String[] { randomAlphaOfLengthBetween(1, 10) })); + mutated.indices(randomArrayOtherThan(instance.indices(), () -> new String[] { randomAlphaOfLengthBetween(1, 10) })); yield mutated; } default -> throw new IllegalStateException("Invalid mutation case"); diff --git a/server/src/test/java/org/elasticsearch/action/synonyms/PutSynonymsActionRequestSerializingTests.java b/server/src/test/java/org/elasticsearch/action/synonyms/PutSynonymsActionRequestSerializingTests.java index ff9953fef595a..8af9bc9c6202b 100644 --- a/server/src/test/java/org/elasticsearch/action/synonyms/PutSynonymsActionRequestSerializingTests.java +++ b/server/src/test/java/org/elasticsearch/action/synonyms/PutSynonymsActionRequestSerializingTests.java @@ -14,8 +14,6 @@ import org.elasticsearch.test.AbstractWireSerializingTestCase; import java.io.IOException; -import java.util.Arrays; -import java.util.List; import static org.elasticsearch.action.synonyms.SynonymsTestUtils.randomSynonymsSet; @@ -34,14 +32,14 @@ protected PutSynonymsAction.Request createTestInstance() { @Override protected PutSynonymsAction.Request mutateInstance(PutSynonymsAction.Request instance) throws IOException { String synonymsSetId = instance.synonymsSetId(); - List synonymRules = Arrays.asList(instance.synonymRules()); + SynonymRule[] synonymRules = instance.synonymRules(); boolean refresh = instance.refresh(); switch (between(0, 2)) { case 0 -> synonymsSetId = randomValueOtherThan(synonymsSetId, () -> randomIdentifier()); - case 1 -> synonymRules = randomValueOtherThan(synonymRules, () -> Arrays.asList(randomSynonymsSet())); + case 1 -> synonymRules = randomArrayOtherThan(synonymRules, () -> randomSynonymsSet()); case 2 -> refresh = refresh == false; default -> throw new AssertionError("Illegal randomisation branch"); } - return new PutSynonymsAction.Request(synonymsSetId, synonymRules.toArray(new SynonymRule[0]), refresh); + return new PutSynonymsAction.Request(synonymsSetId, synonymRules, refresh); } } diff --git a/server/src/test/java/org/elasticsearch/search/vectors/KnnSearchBuilderTests.java b/server/src/test/java/org/elasticsearch/search/vectors/KnnSearchBuilderTests.java index 7c0c4ba25300b..9f97fe7c861c7 100644 --- a/server/src/test/java/org/elasticsearch/search/vectors/KnnSearchBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/search/vectors/KnnSearchBuilderTests.java @@ -126,7 +126,7 @@ yield new KnnSearchBuilder( ).boost(instance.boost); } case 1 -> { - float[] newVector = randomValueOtherThan(instance.queryVector.asFloatVector(), () -> randomVector(5)); + float[] newVector = randomArrayOtherThan(instance.queryVector.asFloatVector(), () -> randomVector(5)); yield new KnnSearchBuilder( instance.field, newVector, diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java index 8150456194b76..b8639c3c02064 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESTestCase.java @@ -1501,9 +1501,74 @@ public static void maybeSet(Consumer consumer, T value) { * helper to get a random value in a certain range that's different from the input */ public static T randomValueOtherThan(T input, Supplier randomSupplier) { + assert input == null || input.getClass().isArray() == false + : "randomValueOtherThan() does not work as expected with arrays, use randomArrayOtherThan() instead"; return randomValueOtherThanMany(v -> Objects.equals(input, v), randomSupplier); } + /** + * helper to get a random value in a certain range that's different from the input, for object arrays + */ + public static T[] randomArrayOtherThan(T[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for boolean arrays + */ + public static boolean[] randomArrayOtherThan(boolean[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for byte arrays + */ + public static byte[] randomArrayOtherThan(byte[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for char arrays + */ + public static char[] randomArrayOtherThan(char[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for short arrays + */ + public static short[] randomArrayOtherThan(short[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for int arrays + */ + public static int[] randomArrayOtherThan(int[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for long arrays + */ + public static long[] randomArrayOtherThan(long[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for float arrays + */ + public static float[] randomArrayOtherThan(float[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + + /** + * helper to get a random value in a certain range that's different from the input, for double arrays + */ + public static double[] randomArrayOtherThan(double[] input, Supplier randomSupplier) { + return randomValueOtherThanMany(v -> Arrays.equals(input, v), randomSupplier); + } + /** * helper to get a random value in a certain range that's different from the input */ diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/ClusterRequestTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/ClusterRequestTests.java index b25fba6f1dc48..da54b327050bf 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/ClusterRequestTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/ClusterRequestTests.java @@ -121,7 +121,7 @@ protected ClusterComputeRequest mutateInstance(ClusterComputeRequest in) throws in.configuration(), new RemoteClusterPlan( plan.plan(), - randomValueOtherThan(plan.targetIndices(), () -> generateRandomStringArray(10, 10, false, false)), + randomArrayOtherThan(plan.targetIndices(), () -> generateRandomStringArray(10, 10, false, false)), plan.originalIndices() ) ); diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/DataNodeRequestSerializationTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/DataNodeRequestSerializationTests.java index a9faac859f0da..267515b52c94f 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/DataNodeRequestSerializationTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/plugin/DataNodeRequestSerializationTests.java @@ -243,7 +243,7 @@ protected DataNodeRequest mutateInstance(DataNodeRequest in) throws IOException yield request; } case 7 -> { - var indices = randomValueOtherThan(in.indices(), () -> generateRandomStringArray(10, 10, false, false)); + var indices = randomArrayOtherThan(in.indices(), () -> generateRandomStringArray(10, 10, false, false)); var request = new DataNodeRequest( in.sessionId(), in.configuration(),