Add RefPermission#fromName and test it

Change-Id: I707cddcb96f970cbc534ace4d3e92bb6de324333
This commit is contained in:
Han-Wen Nienhuys
2018-01-24 15:16:48 +01:00
parent dd63455b32
commit e138aece2a
2 changed files with 36 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.permissions;
import com.google.gerrit.common.data.Permission;
import java.util.Arrays;
import java.util.Locale;
import java.util.Optional;
@@ -80,4 +81,9 @@ public enum RefPermission {
public String describeForException() {
return toString().toLowerCase(Locale.US).replace('_', ' ');
}
/** @return the enum constant for a given permission name if present. */
public static Optional<RefPermission> fromName(String name) {
return Arrays.stream(RefPermission.values()).filter(p -> name.equals(p.name)).findFirst();
}
}

View File

@@ -0,0 +1,30 @@
// Copyright (C) 2018 The Android Open Source Project
//
// Licensed 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 com.google.gerrit.server.permissions;
import static com.google.common.truth.Truth8.assertThat;
import com.google.gerrit.common.data.Permission;
import org.junit.Test;
public class RefPermissionTest {
@Test
public void fromName() {
assertThat(RefPermission.fromName("doesnotexist")).isEmpty();
assertThat(RefPermission.fromName("")).isEmpty();
assertThat(RefPermission.fromName(Permission.VIEW_PRIVATE_CHANGES))
.hasValue(RefPermission.READ_PRIVATE_CHANGES);
}
}