AccountSshKey: Split common getter code to utility method

Change-Id: I39b566890da209381bcfc7ac01596a4d4276e495
This commit is contained in:
David Pursehouse
2016-05-06 10:55:06 +09:00
parent 165fc01acd
commit 7ef114dc62
2 changed files with 25 additions and 30 deletions

View File

@@ -89,43 +89,27 @@ public final class AccountSshKey {
return sshPublicKey;
}
public String getAlgorithm() {
final String s = getSshPublicKey();
if (s == null || s.length() == 0) {
return "none";
private String getPublicKeyPart(int index, String defaultValue) {
String s = getSshPublicKey();
if (s != null && s.length() > 0) {
String[] parts = s.split(" ");
if (parts.length > index) {
return parts[index];
}
}
return defaultValue;
}
final String[] parts = s.split(" ");
if (parts.length < 1) {
return "none";
}
return parts[0];
public String getAlgorithm() {
return getPublicKeyPart(0, "none");
}
public String getEncodedKey() {
final String s = getSshPublicKey();
if (s == null || s.length() == 0) {
return null;
}
final String[] parts = s.split(" ");
if (parts.length < 2) {
return null;
}
return parts[1];
return getPublicKeyPart(1, null);
}
public String getComment() {
final String s = getSshPublicKey();
if (s == null || s.length() == 0) {
return "";
}
final String[] parts = s.split(" ", 3);
if (parts.length < 3) {
return "";
}
return parts[2];
return getPublicKeyPart(2, "");
}
public boolean isValid() {

View File

@@ -25,9 +25,10 @@ public class AccountSshKeyTest {
+ "vf8IZixgjCmiBhaL2gt3wff6pP+NXJpTSA4aeWE5DfNK5tZlxlSxqkKOS8JRSUeNQov5T"
+ "w== john.doe@example.com";
private final Account.Id accountId = new Account.Id(1);
@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();
@@ -36,4 +37,14 @@ public class AccountSshKeyTest {
key = new AccountSshKey(new AccountSshKey.Id(accountId, 1), KEY);
assertThat(key.isValid()).isTrue();
}
@Test
public void testGetters() throws Exception {
AccountSshKey key = new AccountSshKey(
new AccountSshKey.Id(accountId, 1), KEY);
assertThat(key.getSshPublicKey()).isEqualTo(KEY);
assertThat(key.getAlgorithm()).isEqualTo(KEY.split(" ")[0]);
assertThat(key.getEncodedKey()).isEqualTo(KEY.split(" ")[1]);
assertThat(key.getComment()).isEqualTo(KEY.split(" ")[2]);
}
}