Merge changes I5f74b124,I3f474829
* changes: Add serializer to Account Add serializer to ProjectWatchKey
This commit is contained in:
commit
0407831a2f
@ -19,10 +19,15 @@ import static com.google.gerrit.entities.RefNames.REFS_STARRED_CHANGES;
|
||||
import static com.google.gerrit.entities.RefNames.REFS_USERS;
|
||||
|
||||
import com.google.auto.value.AutoValue;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.extensions.client.DiffPreferencesInfo;
|
||||
import com.google.gerrit.proto.Protos;
|
||||
import com.google.gerrit.server.cache.proto.Cache.AccountProto;
|
||||
import com.google.gerrit.server.cache.serialize.CacheSerializer;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@ -120,6 +125,42 @@ public abstract class Account {
|
||||
}
|
||||
}
|
||||
|
||||
enum Serializer implements CacheSerializer<Account> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Account account) {
|
||||
// We don't care about the difference of empty strings and null in the Account entity.
|
||||
AccountProto.Builder proto =
|
||||
AccountProto.newBuilder()
|
||||
.setId(account.id().get())
|
||||
.setRegisteredOn(account.registeredOn().toInstant().toEpochMilli())
|
||||
.setInactive(account.inactive())
|
||||
.setFullName(Strings.nullToEmpty(account.fullName()))
|
||||
.setDisplayName(Strings.nullToEmpty(account.displayName()))
|
||||
.setPreferredEmail(Strings.nullToEmpty(account.preferredEmail()))
|
||||
.setStatus(Strings.nullToEmpty(account.status()))
|
||||
.setMetaId(Strings.nullToEmpty(account.metaId()));
|
||||
return Protos.toByteArray(proto.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Account deserialize(byte[] in) {
|
||||
// We don't care about the difference of empty strings and null in the Account entity.
|
||||
AccountProto proto = Protos.parseUnchecked(AccountProto.parser(), in);
|
||||
return Account.builder(
|
||||
Account.id(proto.getId()),
|
||||
Timestamp.from(Instant.ofEpochMilli(proto.getRegisteredOn())))
|
||||
.setFullName(Strings.emptyToNull(proto.getFullName()))
|
||||
.setDisplayName(Strings.emptyToNull(proto.getDisplayName()))
|
||||
.setPreferredEmail(Strings.emptyToNull(proto.getPreferredEmail()))
|
||||
.setInactive(proto.getInactive())
|
||||
.setStatus(Strings.emptyToNull(proto.getStatus()))
|
||||
.setMetaId(Strings.emptyToNull(proto.getMetaId()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
public abstract Id id();
|
||||
|
||||
/** Date and time the user registered with the review server. */
|
||||
|
@ -10,12 +10,15 @@ java_library(
|
||||
deps = [
|
||||
"//java/com/google/gerrit/common:annotations",
|
||||
"//java/com/google/gerrit/extensions:api",
|
||||
"//java/com/google/gerrit/proto",
|
||||
"//java/com/google/gerrit/server/cache/serialize",
|
||||
"//lib:guava",
|
||||
"//lib:jgit",
|
||||
"//lib:protobuf",
|
||||
"//lib/auto:auto-value",
|
||||
"//lib/auto:auto-value-annotations",
|
||||
"//lib/errorprone:annotations",
|
||||
"//proto:cache_java_proto",
|
||||
"//proto:entities_java_proto",
|
||||
],
|
||||
)
|
||||
|
@ -32,6 +32,9 @@ import com.google.common.collect.Sets;
|
||||
import com.google.gerrit.common.Nullable;
|
||||
import com.google.gerrit.entities.Account;
|
||||
import com.google.gerrit.entities.Project;
|
||||
import com.google.gerrit.proto.Protos;
|
||||
import com.google.gerrit.server.cache.proto.Cache.ProjectWatchKeyProto;
|
||||
import com.google.gerrit.server.cache.serialize.CacheSerializer;
|
||||
import com.google.gerrit.server.git.ValidationError;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@ -79,6 +82,7 @@ import org.eclipse.jgit.lib.Config;
|
||||
public class ProjectWatches {
|
||||
@AutoValue
|
||||
public abstract static class ProjectWatchKey {
|
||||
|
||||
public static ProjectWatchKey create(Project.NameKey project, @Nullable String filter) {
|
||||
return new AutoValue_ProjectWatches_ProjectWatchKey(project, Strings.emptyToNull(filter));
|
||||
}
|
||||
@ -86,6 +90,26 @@ public class ProjectWatches {
|
||||
public abstract Project.NameKey project();
|
||||
|
||||
public abstract @Nullable String filter();
|
||||
|
||||
enum Serializer implements CacheSerializer<ProjectWatchKey> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public byte[] serialize(ProjectWatchKey key) {
|
||||
ProjectWatchKeyProto.Builder proto =
|
||||
ProjectWatchKeyProto.newBuilder().setProject(key.project().get());
|
||||
if (key.filter() != null) {
|
||||
proto.setFilter(key.filter());
|
||||
}
|
||||
return Protos.toByteArray(proto.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ProjectWatchKey deserialize(byte[] in) {
|
||||
ProjectWatchKeyProto proto = Protos.parseUnchecked(ProjectWatchKeyProto.parser(), in);
|
||||
return ProjectWatchKey.create(Project.nameKey(proto.getProject()), proto.getFilter());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum NotifyType {
|
||||
|
64
javatests/com/google/gerrit/entities/AccountCacheTest.java
Normal file
64
javatests/com/google/gerrit/entities/AccountCacheTest.java
Normal file
@ -0,0 +1,64 @@
|
||||
// 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.entities;
|
||||
|
||||
import com.google.common.truth.Truth;
|
||||
import com.google.common.truth.extensions.proto.ProtoTruth;
|
||||
import com.google.gerrit.server.cache.proto.Cache.AccountProto;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test to ensure that we are serializing and deserializing {@link Account} correctly. This is part
|
||||
* of the {@code AccountCache}.
|
||||
*/
|
||||
public class AccountCacheTest {
|
||||
@Test
|
||||
public void roundTrip() throws Exception {
|
||||
Account account =
|
||||
Account.builder(Account.id(1), Timestamp.from(Instant.EPOCH))
|
||||
.setFullName("foo bar")
|
||||
.setDisplayName("foo")
|
||||
.setActive(false)
|
||||
.setMetaId("dead..beef")
|
||||
.setStatus("OOO")
|
||||
.setPreferredEmail("foo@bar.tld")
|
||||
.build();
|
||||
byte[] serialized = Account.Serializer.INSTANCE.serialize(account);
|
||||
ProtoTruth.assertThat(AccountProto.parseFrom(serialized))
|
||||
.isEqualTo(
|
||||
AccountProto.newBuilder()
|
||||
.setId(1)
|
||||
.setRegisteredOn(0)
|
||||
.setFullName("foo bar")
|
||||
.setDisplayName("foo")
|
||||
.setInactive(true)
|
||||
.setMetaId("dead..beef")
|
||||
.setStatus("OOO")
|
||||
.setPreferredEmail("foo@bar.tld")
|
||||
.build());
|
||||
Truth.assertThat(Account.Serializer.INSTANCE.deserialize(serialized)).isEqualTo(account);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void roundTripNullFields() throws Exception {
|
||||
Account account = Account.builder(Account.id(1), Timestamp.from(Instant.EPOCH)).build();
|
||||
byte[] serialized = Account.Serializer.INSTANCE.serialize(account);
|
||||
ProtoTruth.assertThat(AccountProto.parseFrom(serialized))
|
||||
.isEqualTo(AccountProto.newBuilder().setId(1).setRegisteredOn(0).build());
|
||||
Truth.assertThat(Account.Serializer.INSTANCE.deserialize(serialized)).isEqualTo(account);
|
||||
}
|
||||
}
|
@ -9,5 +9,7 @@ junit_tests(
|
||||
"//lib:guava",
|
||||
"//lib:jgit",
|
||||
"//lib/truth",
|
||||
"//lib/truth:truth-proto-extension",
|
||||
"//proto:cache_java_proto",
|
||||
],
|
||||
)
|
||||
|
@ -0,0 +1,50 @@
|
||||
// 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.account;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import com.google.common.truth.extensions.proto.ProtoTruth;
|
||||
import com.google.gerrit.entities.Project;
|
||||
import com.google.gerrit.server.cache.proto.Cache.ProjectWatchKeyProto;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Test to ensure that we are serializing and deserializing {@link ProjectWatches.ProjectWatchKey}
|
||||
* correctly. This is part of the {@code AccountCache}.
|
||||
*/
|
||||
public class ProjectWatchCacheTest {
|
||||
@Test
|
||||
public void keyRoundTrip() throws Exception {
|
||||
ProjectWatches.ProjectWatchKey key =
|
||||
ProjectWatches.ProjectWatchKey.create(Project.nameKey("pro/ject"), "*");
|
||||
byte[] serialized = ProjectWatches.ProjectWatchKey.Serializer.INSTANCE.serialize(key);
|
||||
ProtoTruth.assertThat(ProjectWatchKeyProto.parseFrom(serialized))
|
||||
.isEqualTo(ProjectWatchKeyProto.newBuilder().setProject("pro/ject").setFilter("*").build());
|
||||
assertThat(ProjectWatches.ProjectWatchKey.Serializer.INSTANCE.deserialize(serialized))
|
||||
.isEqualTo(key);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void keyRoundTripNullFilter() throws Exception {
|
||||
ProjectWatches.ProjectWatchKey key =
|
||||
ProjectWatches.ProjectWatchKey.create(Project.nameKey("pro/ject"), null);
|
||||
byte[] serialized = ProjectWatches.ProjectWatchKey.Serializer.INSTANCE.serialize(key);
|
||||
ProtoTruth.assertThat(ProjectWatchKeyProto.parseFrom(serialized))
|
||||
.isEqualTo(ProjectWatchKeyProto.newBuilder().setProject("pro/ject").build());
|
||||
assertThat(ProjectWatches.ProjectWatchKey.Serializer.INSTANCE.deserialize(serialized))
|
||||
.isEqualTo(key);
|
||||
}
|
||||
}
|
@ -277,3 +277,24 @@ message PureRevertKeyProto {
|
||||
bytes claimed_original = 2;
|
||||
bytes claimed_revert = 3;
|
||||
}
|
||||
|
||||
// Key for com.google.gerrit.server.account.ProjectWatches.ProjectWatcheKey.
|
||||
// Next ID: 3
|
||||
message ProjectWatchKeyProto {
|
||||
string project = 1;
|
||||
string filter = 2;
|
||||
}
|
||||
|
||||
// Serialized form of
|
||||
// com.google.gerrit.entities.Account.
|
||||
// Next ID: 9
|
||||
message AccountProto {
|
||||
int32 id = 1;
|
||||
int64 registered_on = 2;
|
||||
string full_name = 3;
|
||||
string display_name = 4;
|
||||
string preferred_email = 5;
|
||||
bool inactive = 6;
|
||||
string status = 7;
|
||||
string meta_id = 8;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user