[TEST] Prevent duplicate key in XContent test (#36581)

It was possible to generate a response that would output duplicate
keys

Closes: #36473
This commit is contained in:
Tim Vernum 2018-12-13 19:39:59 +11:00 committed by GitHub
parent f6b5d7e013
commit c650be72cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 3 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.collect.MapBuilder; import org.elasticsearch.common.collect.MapBuilder;
import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.BytesStreamOutput;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
@ -155,7 +156,8 @@ public class HasPrivilegesResponseTests
private Collection<HasPrivilegesResponse.ResourcePrivileges> randomResourcePrivileges() { private Collection<HasPrivilegesResponse.ResourcePrivileges> randomResourcePrivileges() {
final Collection<HasPrivilegesResponse.ResourcePrivileges> list = new ArrayList<>(); final Collection<HasPrivilegesResponse.ResourcePrivileges> list = new ArrayList<>();
for (String resource : randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(2, 6))) { // Use hash set to force a unique set of resources
for (String resource : Sets.newHashSet(randomArray(1, 3, String[]::new, () -> randomAlphaOfLengthBetween(2, 6)))) {
final Map<String, Boolean> privileges = new HashMap<>(); final Map<String, Boolean> privileges = new HashMap<>();
for (String priv : randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) { for (String priv : randomArray(1, 5, String[]::new, () -> randomAlphaOfLengthBetween(3, 8))) {
privileges.put(priv, randomBoolean()); privileges.put(priv, randomBoolean());