|
| 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 | +} |
0 commit comments