Add RefPermission#fromName and test it
Change-Id: I707cddcb96f970cbc534ace4d3e92bb6de324333
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user