diff --git a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilege.java b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilege.java index 507d6a5a1956f..3eed3bd281574 100644 --- a/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilege.java +++ b/client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilege.java @@ -55,7 +55,7 @@ public GlobalOperationPrivilege(String category, String operation, Map privilege = randomFrom(Arrays.asList(null, Collections.emptyMap(), Collections.singletonMap("k1", "v1"))); + + if (Strings.hasText(category) && Strings.hasText(operation) && privilege != null && privilege.isEmpty() == false) { + GlobalOperationPrivilege globalOperationPrivilege = new GlobalOperationPrivilege(category, operation, privilege); + assertThat(globalOperationPrivilege.getCategory(), equalTo(category)); + assertThat(globalOperationPrivilege.getOperation(), equalTo(operation)); + assertThat(globalOperationPrivilege.getRaw(), equalTo(privilege)); + } else { + if (category == null || operation == null) { + expectThrows(NullPointerException.class, + () -> new GlobalOperationPrivilege(category, operation, privilege)); + } else { + final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class, + () -> new GlobalOperationPrivilege(category, operation, privilege)); + assertThat(ile.getMessage(), equalTo("privileges cannot be empty or null")); + } + } + } + + public void testEqualsHashCode() { + final String category = randomAlphaOfLength(5); + final String operation = randomAlphaOfLength(5); + final Map privilege = Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(5)); + GlobalOperationPrivilege globalOperationPrivilege = new GlobalOperationPrivilege(category, operation, privilege); + + EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalOperationPrivilege, (original) -> { + return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw()); + }); + EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalOperationPrivilege, (original) -> { + return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw()); + }, GlobalOperationPrivilegeTests::mutateTestItem); + } + + private static GlobalOperationPrivilege mutateTestItem(GlobalOperationPrivilege original) { + switch (randomIntBetween(0, 2)) { + case 0: + return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw()); + case 1: + return new GlobalOperationPrivilege(original.getCategory(), randomAlphaOfLength(5), original.getRaw()); + case 2: + return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), + Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4))); + default: + return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw()); + } + } +} diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java index bb1e933089189..a9d2702970962 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java @@ -21,6 +21,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.test.AbstractXContentTestCase; +import org.elasticsearch.test.EqualsHashCodeTestUtils; import java.io.IOException; import java.util.Arrays; @@ -51,12 +52,12 @@ protected GlobalPrivileges doParseInstance(XContentParser parser) throws IOExcep protected boolean supportsUnknownFields() { return false; // true really means inserting bogus privileges } - + public void testEmptyOrNullGlobalOperationPrivilege() { final Map privilege = randomBoolean() ? null : Collections.emptyMap(); final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GlobalOperationPrivilege(randomAlphaOfLength(2), randomAlphaOfLength(2), privilege)); - assertThat(e.getMessage(), is("Privileges cannot be empty or null")); + assertThat(e.getMessage(), is("privileges cannot be empty or null")); } public void testEmptyOrNullGlobalPrivileges() { @@ -91,4 +92,21 @@ private static GlobalOperationPrivilege buildRandomGlobalScopedPrivilege() { } return new GlobalOperationPrivilege("application", randomAlphaOfLength(2) + idCounter++, privilege); } + + public void testEqualsHashCode() { + final List privilegeList = Arrays + .asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege())); + GlobalPrivileges globalPrivileges = new GlobalPrivileges(privilegeList); + + EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalPrivileges, (original) -> { + return new GlobalPrivileges(original.getPrivileges()); + }); + EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalPrivileges, (original) -> { + return new GlobalPrivileges(original.getPrivileges()); + }, (original) -> { + final List newList = Arrays + .asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege())); + return new GlobalPrivileges(newList); + }); + } }