From f557544fa9a9e278f4370137a26852f00fbb8606 Mon Sep 17 00:00:00 2001 From: Patrick Hiesel Date: Fri, 10 Jul 2020 14:25:51 +0200 Subject: [PATCH] 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 --- .../entities/PermissionRuleSerializer.java | 47 +++++++++++++++++++ .../PermissionRuleSerializerTest.java | 43 +++++++++++++++++ proto/cache.proto | 10 ++++ 3 files changed, 100 insertions(+) create mode 100644 java/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializer.java create mode 100644 javatests/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializerTest.java diff --git a/java/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializer.java b/java/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializer.java new file mode 100644 index 0000000000..d310f18209 --- /dev/null +++ b/java/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializer.java @@ -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 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() {} +} diff --git a/javatests/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializerTest.java b/javatests/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializerTest.java new file mode 100644 index 0000000000..b447d5e10e --- /dev/null +++ b/javatests/com/google/gerrit/server/cache/serialize/entities/PermissionRuleSerializerTest.java @@ -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); + } +} diff --git a/proto/cache.proto b/proto/cache.proto index 42e21e9fc8..4e75be25c6 100644 --- a/proto/cache.proto +++ b/proto/cache.proto @@ -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; +}