Add method to show readable key info (#1431)

* Resolve conflict

* Apply new printing method to existing command
This commit is contained in:
Rachel Guan 2021-12-01 10:46:27 -05:00 committed by GitHub
parent a46907df0e
commit 4a869761ec
5 changed files with 60 additions and 7 deletions

View file

@ -311,4 +311,29 @@ public class VKey<T> extends ImmutableObject implements Serializable {
}
return key;
}
/**
* Constructs the readable string representation of a {@link VKey}.
*
* <p>This readable string representation of a vkey contains its type and its sql key or ofy key,
* or both.
*/
@Override
public String toString() {
if (maybeGetOfyKey().isPresent() && maybeGetSqlKey().isPresent()) {
return String.format(
"VKey<%s>(%s:%s,%s:%s)",
getKind().getSimpleName(), SQL_LOOKUP_KEY, sqlKey, OFY_LOOKUP_KEY, ofyKeyToString());
} else if (maybeGetSqlKey().isPresent()) {
return String.format("VKey<%s>(%s:%s)", getKind().getSimpleName(), SQL_LOOKUP_KEY, sqlKey);
} else if (maybeGetOfyKey().isPresent()) {
return String.format("VKey<%s>(%s:%s)", ofyKey.getKind(), OFY_LOOKUP_KEY, ofyKeyToString());
} else {
throw new IllegalStateException("VKey should contain at least one form of key");
}
}
private String ofyKeyToString() {
return ofyKey.getName() == null ? String.valueOf(ofyKey.getId()) : ofyKey.getName();
}
}

View file

@ -48,7 +48,7 @@ final class GetResourceByKeyCommand implements CommandWithRemoteApi {
EppResource resource =
checkNotNull(
auditedOfy().load().key(resourceKey.getOfyKey()).now(),
"Could not load resource for key: " + resourceKey.getOfyKey());
"Could not load resource for key: " + resourceKey);
System.out.println(expand ? resource.toHydratedString() : resource.toString());
}
}

View file

@ -418,6 +418,32 @@ class VKeyTest {
assertThat(VKey.create(vkeyStringFromQueue)).isEqualTo(vkey);
}
@Test
void testToString_sqlOnlyVKey() {
assertThat(VKey.createSql(TestObject.class, "testId").toString())
.isEqualTo("VKey<TestObject>(sql:testId)");
}
@Test
void testToString_ofyOnlyVKey_withName() {
assertThat(
VKey.createOfy(TestObject.class, Key.create(TestObject.class, "testName")).toString())
.isEqualTo("VKey<TestObject>(ofy:testName)");
}
@Test
void testToString_ofyOnlyVKey_withId() {
assertThat(VKey.createOfy(TestObject.class, Key.create(TestObject.class, 12345)).toString())
.isEqualTo("VKey<TestObject>(ofy:12345)");
}
@Test
void testToString_sqlAndOfyVKey() {
assertThat(
VKey.create(TestObject.class, "foo", Key.create(TestObject.create("ofy"))).toString())
.isEqualTo("VKey<TestObject>(sql:foo,ofy:ofy)");
}
@Entity
static class OtherObject {}
}

View file

@ -79,7 +79,7 @@ class GetResourceByKeyCommandTest extends CommandTestCase<GetResourceByKeyComman
"agR0ZXN0chULEgpEb21haW5CYXNlIgU0LVRMRAw"));
assertThat(thrown)
.hasMessageThat()
.contains("Could not load resource for key: Key<?>(DomainBase(\"4-TLD\"))");
.contains("Could not load resource for key: VKey<DomainBase>(sql:4-TLD,ofy:4-TLD)");
}
@Test
@ -128,7 +128,7 @@ class GetResourceByKeyCommandTest extends CommandTestCase<GetResourceByKeyComman
"agR0ZXN0chsLEg9Db250YWN0UmVzb3VyY2UiBjMtUk9JRAw"));
assertThat(thrown)
.hasMessageThat()
.contains("Could not load resource for key: Key<?>(ContactResource(\"3-ROID\"))");
.contains("Could not load resource for key: VKey<ContactResource>(sql:3-ROID,ofy:3-ROID)");
}
@Test
@ -177,7 +177,7 @@ class GetResourceByKeyCommandTest extends CommandTestCase<GetResourceByKeyComman
"agR0ZXN0chgLEgxIb3N0UmVzb3VyY2UiBjMtUk9JRAw"));
assertThat(thrown)
.hasMessageThat()
.contains("Could not load resource for key: Key<?>(HostResource(\"3-ROID\"))");
.contains("Could not load resource for key: VKey<HostResource>(sql:3-ROID,ofy:3-ROID)");
}
@Test
@ -210,7 +210,7 @@ class GetResourceByKeyCommandTest extends CommandTestCase<GetResourceByKeyComman
() -> runCommand("agR0ZXN0chULEgpEb21haW5CYXNlIgUyLVRMRAw"));
assertThat(thrown)
.hasMessageThat()
.contains("Could not load resource for key: Key<?>(DomainBase(\"2-TLD\"))");
.contains("Could not load resource for key: VKey<DomainBase>(sql:2-TLD,ofy:2-TLD)");
}
@Test

View file

@ -331,7 +331,9 @@ class SetupOteCommandTest extends CommandTestCase<SetupOteCommand> {
"--registrar=blobio",
"--email=contact@email.com",
"--certfile=" + getCertFilename()));
assertThat(thrown).hasMessageThat().contains("Registry(\"blobio-sunrise\")");
assertThat(thrown)
.hasMessageThat()
.contains("VKey<Registry>(sql:blobio-sunrise,ofy:blobio-sunrise)");
}
@Test
@ -367,7 +369,7 @@ class SetupOteCommandTest extends CommandTestCase<SetupOteCommand> {
"--registrar=blobio",
"--email=contact@email.com",
"--certfile=" + getCertFilename()));
assertThat(thrown).hasMessageThat().contains("Registrar(\"blobio-1\")");
assertThat(thrown).hasMessageThat().contains("VKey<Registrar>(sql:blobio-1,ofy:blobio-1)");
}
@Test