Skip to content

Commit 37ff64a

Browse files
author
Yogesh Gaikwad
committed
[HLRC] Fix issue in equals for GlobalOperationPrivileges
This commit fixes an issue in the equals implementation for GlobalOperationPrivileges and adds few tests.
1 parent 3bba9a5 commit 37ff64a

File tree

4 files changed

+118
-9
lines changed

4 files changed

+118
-9
lines changed

client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalOperationPrivilege.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.client.security.user.privileges;
2121

22+
import org.elasticsearch.common.Strings;
2223
import org.elasticsearch.common.xcontent.XContentParser;
2324

2425
import java.io.IOException;
@@ -52,10 +53,16 @@ public class GlobalOperationPrivilege {
5253
* The privilege definition.
5354
*/
5455
public GlobalOperationPrivilege(String category, String operation, Map<String, Object> privilege) {
55-
this.category = Objects.requireNonNull(category);
56-
this.operation = Objects.requireNonNull(operation);
56+
if (Strings.hasText(category) == false) {
57+
throw new IllegalArgumentException("category is required");
58+
}
59+
this.category = category;
60+
if (Strings.hasText(operation) == false) {
61+
throw new IllegalArgumentException("operation is required");
62+
}
63+
this.operation = operation;
5764
if (privilege == null || privilege.isEmpty()) {
58-
throw new IllegalArgumentException("Privileges cannot be empty or null");
65+
throw new IllegalArgumentException("privileges cannot be empty or null");
5966
}
6067
this.privilege = Collections.unmodifiableMap(privilege);
6168
}
@@ -84,7 +91,7 @@ public boolean equals(Object o) {
8491
if (this == o) {
8592
return true;
8693
}
87-
if (o == null || (false == this instanceof GlobalOperationPrivilege)) {
94+
if (o == null || (this.getClass() != o.getClass())) {
8895
return false;
8996
}
9097
final GlobalOperationPrivilege that = (GlobalOperationPrivilege) o;

client/rest-high-level/src/main/java/org/elasticsearch/client/security/user/privileges/GlobalPrivileges.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public final class GlobalPrivileges implements ToXContentObject {
8080
*/
8181
public GlobalPrivileges(Collection<? extends GlobalOperationPrivilege> privileges) {
8282
if (privileges == null || privileges.isEmpty()) {
83-
throw new IllegalArgumentException("Privileges cannot be empty or null");
83+
throw new IllegalArgumentException("privileges cannot be empty or null");
8484
}
8585
// duplicates are just ignored
8686
this.privileges = Collections.unmodifiableSet(new HashSet<>(Objects.requireNonNull(privileges)));
@@ -91,7 +91,7 @@ public GlobalPrivileges(Collection<? extends GlobalOperationPrivilege> privilege
9191
final Set<String> allOperations = privilegesByCategory.getValue().stream().map(p -> p.getOperation())
9292
.collect(Collectors.toSet());
9393
if (allOperations.size() != privilegesByCategory.getValue().size()) {
94-
throw new IllegalArgumentException("Different privileges for the same category and operation are not permitted");
94+
throw new IllegalArgumentException("different privileges for the same category and operation are not permitted");
9595
}
9696
}
9797
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.security.user.privileges;
21+
22+
import org.elasticsearch.common.Strings;
23+
import org.elasticsearch.test.ESTestCase;
24+
import org.elasticsearch.test.EqualsHashCodeTestUtils;
25+
26+
import java.util.Arrays;
27+
import java.util.Collections;
28+
import java.util.Map;
29+
30+
import static org.hamcrest.Matchers.equalTo;
31+
32+
public class GlobalOperationPrivilegeTests extends ESTestCase {
33+
34+
public void testConstructor() {
35+
final String category = randomFrom(Arrays.asList("", " ", null, randomAlphaOfLength(5)));
36+
final String operation = randomFrom(Arrays.asList("", " ", null, randomAlphaOfLength(5)));
37+
final Map<String, Object> privilege = randomFrom(Arrays.asList(null, Collections.emptyMap(), Collections.singletonMap("k1", "v1")));
38+
39+
if (Strings.hasText(category) && Strings.hasText(operation) && privilege != null && privilege.isEmpty() == false) {
40+
GlobalOperationPrivilege globalOperationPrivilege = new GlobalOperationPrivilege(category, operation, privilege);
41+
assertThat(globalOperationPrivilege.getCategory(), equalTo(category));
42+
assertThat(globalOperationPrivilege.getOperation(), equalTo(operation));
43+
assertThat(globalOperationPrivilege.getRaw(), equalTo(privilege));
44+
} else {
45+
final IllegalArgumentException ile = expectThrows(IllegalArgumentException.class,
46+
() -> new GlobalOperationPrivilege(category, operation, privilege));
47+
if (Strings.hasText(category) == false) {
48+
assertThat(ile.getMessage(), equalTo("category is required"));
49+
} else if (Strings.hasText(operation) == false) {
50+
assertThat(ile.getMessage(), equalTo("operation is required"));
51+
} else {
52+
assertThat(ile.getMessage(), equalTo("privileges cannot be empty or null"));
53+
}
54+
}
55+
}
56+
57+
public void testEqualsHashCode() {
58+
final String category = randomAlphaOfLength(5);
59+
final String operation = randomAlphaOfLength(5);
60+
final Map<String, Object> privilege = Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(5));
61+
GlobalOperationPrivilege globalOperationPrivilege = new GlobalOperationPrivilege(category, operation, privilege);
62+
63+
EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalOperationPrivilege, (original) -> {
64+
return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw());
65+
});
66+
EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalOperationPrivilege, (original) -> {
67+
return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(), original.getRaw());
68+
}, GlobalOperationPrivilegeTests::mutateTestItem);
69+
}
70+
71+
private static GlobalOperationPrivilege mutateTestItem(GlobalOperationPrivilege original) {
72+
switch (randomIntBetween(0, 2)) {
73+
case 0:
74+
return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw());
75+
case 1:
76+
return new GlobalOperationPrivilege(original.getCategory(), randomAlphaOfLength(5), original.getRaw());
77+
case 2:
78+
return new GlobalOperationPrivilege(original.getCategory(), original.getOperation(),
79+
Collections.singletonMap(randomAlphaOfLength(4), randomAlphaOfLength(4)));
80+
default:
81+
return new GlobalOperationPrivilege(randomAlphaOfLength(5), original.getOperation(), original.getRaw());
82+
}
83+
}
84+
}

client/rest-high-level/src/test/java/org/elasticsearch/client/security/user/privileges/GlobalPrivilegesTests.java

+21-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.elasticsearch.common.xcontent.XContentParser;
2323
import org.elasticsearch.test.AbstractXContentTestCase;
24+
import org.elasticsearch.test.EqualsHashCodeTestUtils;
2425

2526
import java.io.IOException;
2627
import java.util.Arrays;
@@ -56,13 +57,13 @@ public void testEmptyOrNullGlobalOperationPrivilege() {
5657
final Map<String, Object> privilege = randomBoolean() ? null : Collections.emptyMap();
5758
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
5859
() -> new GlobalOperationPrivilege(randomAlphaOfLength(2), randomAlphaOfLength(2), privilege));
59-
assertThat(e.getMessage(), is("Privileges cannot be empty or null"));
60+
assertThat(e.getMessage(), is("privileges cannot be empty or null"));
6061
}
6162

6263
public void testEmptyOrNullGlobalPrivileges() {
6364
final List<GlobalOperationPrivilege> privileges = randomBoolean() ? null : Collections.emptyList();
6465
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GlobalPrivileges(privileges));
65-
assertThat(e.getMessage(), is("Privileges cannot be empty or null"));
66+
assertThat(e.getMessage(), is("privileges cannot be empty or null"));
6667
}
6768

6869
public void testDuplicateGlobalOperationPrivilege() {
@@ -81,7 +82,7 @@ public void testSameScopeGlobalOperationPrivilege() {
8182
privilege.getOperation(), buildRandomGlobalScopedPrivilege().getRaw());
8283
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
8384
() -> new GlobalPrivileges(Arrays.asList(privilege, sameOperationPrivilege)));
84-
assertThat(e.getMessage(), is("Different privileges for the same category and operation are not permitted"));
85+
assertThat(e.getMessage(), is("different privileges for the same category and operation are not permitted"));
8586
}
8687

8788
private static GlobalOperationPrivilege buildRandomGlobalScopedPrivilege() {
@@ -91,4 +92,21 @@ private static GlobalOperationPrivilege buildRandomGlobalScopedPrivilege() {
9192
}
9293
return new GlobalOperationPrivilege("application", randomAlphaOfLength(2) + idCounter++, privilege);
9394
}
95+
96+
public void testEqualsHashCode() {
97+
final List<GlobalOperationPrivilege> privilegeList = Arrays
98+
.asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege()));
99+
GlobalPrivileges globalPrivileges = new GlobalPrivileges(privilegeList);
100+
101+
EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalPrivileges, (original) -> {
102+
return new GlobalPrivileges(original.getPrivileges());
103+
});
104+
EqualsHashCodeTestUtils.checkEqualsAndHashCode(globalPrivileges, (original) -> {
105+
return new GlobalPrivileges(original.getPrivileges());
106+
}, (original) -> {
107+
final List<GlobalOperationPrivilege> newList = Arrays
108+
.asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege()));
109+
return new GlobalPrivileges(newList);
110+
});
111+
}
94112
}

0 commit comments

Comments
 (0)