AccountSshKey.Id: Add isValid() method

The isValid() method returns false when the Id's sequence number is
invalid (i.e. < 1).

Extend AccountSshKey#isValid() to also check its Id's validity.

Change-Id: I7b428b06cc5bbb04cceef20d9f284e58071b9abe
This commit is contained in:
David Pursehouse
2016-05-05 06:50:06 +09:00
parent fa00224b74
commit 165fc01acd
2 changed files with 45 additions and 2 deletions

View File

@@ -53,6 +53,10 @@ public final class AccountSshKey {
protected void set(int newValue) {
seq = newValue;
}
public boolean isValid() {
return seq > 0;
}
}
@Column(id = 1, name = Column.NONE)
@@ -70,7 +74,7 @@ public final class AccountSshKey {
public AccountSshKey(final AccountSshKey.Id i, final String pub) {
id = i;
sshPublicKey = pub;
valid = true; // We can assume it is fine.
valid = id.isValid();
}
public Account.Id getAccount() {
@@ -125,7 +129,7 @@ public final class AccountSshKey {
}
public boolean isValid() {
return valid;
return valid && id.isValid();
}
public void setInvalid() {

View File

@@ -0,0 +1,39 @@
// Copyright (C) 2016 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.reviewdb.client;
import static com.google.common.truth.Truth.assertThat;
import org.junit.Test;
public class AccountSshKeyTest {
private static final String KEY =
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQCgug5VyMXQGnem2H1KVC4/HcRcD4zzBqS"
+ "uJBRWVonSSoz3RoAZ7bWXCVVGwchtXwUURD689wFYdiPecOrWOUgeeyRq754YWRhU+W28"
+ "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T"
+ "w== john.doe@example.com";
@Test
public void testValidity() throws Exception {
Account.Id accountId = new Account.Id(1);
AccountSshKey key = new AccountSshKey(
new AccountSshKey.Id(accountId, -1), KEY);
assertThat(key.isValid()).isFalse();
key = new AccountSshKey(new AccountSshKey.Id(accountId, 0), KEY);
assertThat(key.isValid()).isFalse();
key = new AccountSshKey(new AccountSshKey.Id(accountId, 1), KEY);
assertThat(key.isValid()).isTrue();
}
}