Skip to content

Commit

Permalink
[HLRC] Fix issue in equals impl for GlobalOperationPrivileges (#35721)
Browse files Browse the repository at this point in the history
This commit fixes an issue in the equals implementation for
GlobalOperationPrivileges and adds few tests.
  • Loading branch information
bizybot authored and Yogesh Gaikwad committed Nov 27, 2018
1 parent b6c2cb1 commit 29f7d38
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public GlobalOperationPrivilege(String category, String operation, Map<String, O
this.category = Objects.requireNonNull(category);
this.operation = Objects.requireNonNull(operation);
if (privilege == null || privilege.isEmpty()) {
throw new IllegalArgumentException("Privileges cannot be empty or null");
throw new IllegalArgumentException("privileges cannot be empty or null");
}
this.privilege = Collections.unmodifiableMap(privilege);
}
Expand Down Expand Up @@ -84,7 +84,7 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || (false == this instanceof GlobalOperationPrivilege)) {
if (false == (o instanceof GlobalOperationPrivilege)) {
return false;
}
final GlobalOperationPrivilege that = (GlobalOperationPrivilege) o;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import org.elasticsearch.common.Strings;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.EqualsHashCodeTestUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import static org.hamcrest.Matchers.equalTo;

public class GlobalOperationPrivilegeTests extends ESTestCase {

public void testConstructor() {
final String category = randomFrom(Arrays.asList(null, randomAlphaOfLength(5)));
final String operation = randomFrom(Arrays.asList(null, randomAlphaOfLength(5)));
final Map<String, Object> 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<String, Object> 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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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() {
Expand Down Expand Up @@ -91,4 +92,21 @@ private static GlobalOperationPrivilege buildRandomGlobalScopedPrivilege() {
}
return new GlobalOperationPrivilege("application", randomAlphaOfLength(2) + idCounter++, privilege);
}

public void testEqualsHashCode() {
final List<GlobalOperationPrivilege> 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<GlobalOperationPrivilege> newList = Arrays
.asList(randomArray(1, 4, size -> new GlobalOperationPrivilege[size], () -> buildRandomGlobalScopedPrivilege()));
return new GlobalPrivileges(newList);
});
}
}

0 comments on commit 29f7d38

Please sign in to comment.