Add serializer for PermissionRule
This commit adds a serializer for the PermissionRule entitiy. The eventual goal is that we serialize CachedProjectConfig. The entity is too large to be serialized directly, though, so we divide an conquer. Change-Id: Ifc691c58486c57c3a1f3ed80b731ef698011526f
This commit is contained in:
47
java/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializer.java
vendored
Normal file
47
java/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializer.java
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
// Copyright (C) 2020 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.cache.serialize.entities;
|
||||
|
||||
import com.google.common.base.Converter;
|
||||
import com.google.common.base.Enums;
|
||||
import com.google.gerrit.common.data.PermissionRule;
|
||||
import com.google.gerrit.server.cache.proto.Cache;
|
||||
|
||||
/** Helper to (de)serialize values for caches. */
|
||||
public class PermissionRuleSerializer {
|
||||
private static final Converter<String, PermissionRule.Action> ACTION_CONVERTER =
|
||||
Enums.stringConverter(PermissionRule.Action.class);
|
||||
|
||||
public static PermissionRule deserialize(Cache.PermissionRuleProto proto) {
|
||||
return PermissionRule.builder(GroupReferenceSerializer.deserialize(proto.getGroup()))
|
||||
.setAction(ACTION_CONVERTER.convert(proto.getAction()))
|
||||
.setForce(proto.getForce())
|
||||
.setMin(proto.getMin())
|
||||
.setMax(proto.getMax())
|
||||
.build();
|
||||
}
|
||||
|
||||
public static Cache.PermissionRuleProto serialize(PermissionRule autoValue) {
|
||||
return Cache.PermissionRuleProto.newBuilder()
|
||||
.setAction(ACTION_CONVERTER.reverse().convert(autoValue.getAction()))
|
||||
.setForce(autoValue.getForce())
|
||||
.setMin(autoValue.getMin())
|
||||
.setMax(autoValue.getMax())
|
||||
.setGroup(GroupReferenceSerializer.serialize(autoValue.getGroup()))
|
||||
.build();
|
||||
}
|
||||
|
||||
private PermissionRuleSerializer() {}
|
||||
}
|
||||
43
javatests/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializerTest.java
vendored
Normal file
43
javatests/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializerTest.java
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright (C) 2020 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.cache.serialize.entities;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static com.google.gerrit.server.cache.serialize.entities.PermissionRuleSerializer.deserialize;
|
||||
import static com.google.gerrit.server.cache.serialize.entities.PermissionRuleSerializer.serialize;
|
||||
|
||||
import com.google.gerrit.common.data.GroupReference;
|
||||
import com.google.gerrit.common.data.PermissionRule;
|
||||
import org.junit.Test;
|
||||
|
||||
public class PermissionRuleSerializerTest {
|
||||
@Test
|
||||
public void roundTrip() {
|
||||
PermissionRule permissionRuleAutoValue =
|
||||
PermissionRule.builder(GroupReference.create("name"))
|
||||
.setAction(PermissionRule.Action.BATCH)
|
||||
.setForce(true)
|
||||
.setMax(321)
|
||||
.setMin(123)
|
||||
.build();
|
||||
assertThat(deserialize(serialize(permissionRuleAutoValue))).isEqualTo(permissionRuleAutoValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundTripWithMinimalValues() {
|
||||
PermissionRule permissionRuleAutoValue = PermissionRule.create(GroupReference.create("name"));
|
||||
assertThat(deserialize(serialize(permissionRuleAutoValue))).isEqualTo(permissionRuleAutoValue);
|
||||
}
|
||||
}
|
||||
@@ -345,3 +345,13 @@ message GroupReferenceProto {
|
||||
string uuid = 1;
|
||||
string name = 2;
|
||||
}
|
||||
|
||||
// Serialized form of com.google.gerrit.common.data.PermissionRule.
|
||||
// Next ID: 6
|
||||
message PermissionRuleProto {
|
||||
string action = 1; // ENUM as String
|
||||
bool force = 2;
|
||||
int32 min = 3;
|
||||
int32 max = 4;
|
||||
GroupReferenceProto group = 5;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user