Add VKey to String and String to VKey methods (#1396)

* Add stringify and parse methods to SerializeUTils

* Improve comments and test cases

* Fix comments and test strings

* Fix dependency warning
This commit is contained in:
Rachel Guan
2021-11-02 13:25:35 -04:00
committed by GitHub
parent e0dc2e43bb
commit 2218663d55
6 changed files with 325 additions and 1 deletions
@@ -24,8 +24,10 @@ import com.googlecode.objectify.annotation.Entity;
import google.registry.model.billing.BillingEvent.OneTime;
import google.registry.model.domain.DomainBase;
import google.registry.model.registrar.RegistrarContact;
import google.registry.model.translators.VKeyTranslatorFactory;
import google.registry.testing.AppEngineExtension;
import google.registry.testing.TestObject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -39,6 +41,11 @@ class VKeyTest {
.withOfyTestEntities(TestObject.class)
.build();
@BeforeAll
static void beforeAll() {
VKeyTranslatorFactory.addTestEntityClass(TestObject.class);
}
@Test
void testOptionalAccessors() {
VKey<TestObject> key =
@@ -130,6 +137,176 @@ class VKeyTest {
assertThat(vkey.getSqlKey()).isEqualTo("ROID-1");
}
/** Test stringify() with vkey created via different ways. */
@Test
void testStringify_sqlOnlyVKey() throws Exception {
assertThat(VKey.createSql(TestObject.class, "foo").stringify())
.isEqualTo("kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw");
}
@Test
void testStringify_ofyOnlyVKey() throws Exception {
assertThat(VKey.createOfy(TestObject.class, Key.create(TestObject.class, "foo")).stringify())
.isEqualTo(
"kind:google.registry.testing.TestObject@ofy:agR0ZXN0chMLEgpUZXN0T2JqZWN0IgNmb28M");
}
@Test
void testStringify_vkeyFromWebsafeKey() throws Exception {
DomainBase domain = newDomainBase("example.com", "ROID-1", persistActiveContact("contact-1"));
Key<DomainBase> key = Key.create(domain);
VKey<DomainBase> vkey = VKey.fromWebsafeKey(key.getString());
assertThat(vkey.stringify())
.isEqualTo(
"kind:google.registry.model.domain.DomainBas"
+ "e@sql:rO0ABXQABlJPSUQtMQ"
+ "@ofy:agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM");
}
@Test
void testStringify_sqlAndOfyVKey() throws Exception {
assertThat(
VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo"))).stringify())
.isEqualTo(
"kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw@ofy:agR0ZXN0cjELEg9FbnRpdH"
+ "lHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M");
}
@Test
void testStringify_asymmetricVKey() throws Exception {
assertThat(
VKey.create(TestObject.class, "test", Key.create(TestObject.create("foo"))).stringify())
.isEqualTo(
"kind:google.registry.testing.TestObject@sql:rO0ABXQABHRlc3Q@ofy:agR0ZXN0cjELEg9FbnRpd"
+ "HlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M");
}
/** Test create() via different vkey string representations. */
@Test
void testCreate_stringifedVKey_sqlOnlyVKeyString() throws Exception {
assertThat(VKey.create("kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw"))
.isEqualTo(VKey.createSql(TestObject.class, "foo"));
}
@Test
void testCreate_stringifedVKey_ofyOnlyVKeyString() throws Exception {
assertThat(
VKey.create(
"kind:google.registry.testing.TestObject@ofy:agR0ZXN0chMLEgpUZXN0T2JqZWN0IgNmb28M"))
.isEqualTo(VKey.createOfy(TestObject.class, Key.create(TestObject.class, "foo")));
}
@Test
void testCreate_stringifedVKey_asymmetricVKeyString() throws Exception {
assertThat(
VKey.create(
"kind:google.registry.testing.TestObject@sql:rO0ABXQABHRlc3Q@ofy:agR0ZXN0cjELEg9Fb"
+ "nRpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M"))
.isEqualTo(VKey.create(TestObject.class, "test", Key.create(TestObject.create("foo"))));
}
@Test
void testCreate_stringifedVKey_sqlAndOfyVKeyString() throws Exception {
assertThat(
VKey.create(
"kind:google.registry.testing.TestObject@sql:rO0ABXQAA2Zvbw@ofy:agR0ZXN0cjELEg9Fbn"
+ "RpdHlHcm91cFJvb3QiCWNyb3NzLXRsZAwLEgpUZXN0T2JqZWN0IgNmb28M"))
.isEqualTo(VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo"))));
}
@Test
void testCreate_stringifyVkey_fromWebsafeKey() throws Exception {
assertThat(
VKey.create(
"kind:google.registry.model.domain.DomainBase@sql:rO0ABXQABlJPSUQtMQ"
+ "@ofy:agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"))
.isEqualTo(
VKey.fromWebsafeKey(
Key.create(
newDomainBase("example.com", "ROID-1", persistActiveContact("contact-1")))
.getString()));
}
@Test
void testCreate_stringifedVKey_websafeKey() throws Exception {
assertThat(VKey.create("agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"))
.isEqualTo(VKey.fromWebsafeKey("agR0ZXN0chYLEgpEb21haW5CYXNlIgZST0lELTEM"));
}
@Test
void testCreate_invalidStringifiedVKey_failure() throws Exception {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> VKey.create("kind:google.registry.testing.TestObject@sq:l@ofya:bc"));
assertThat(thrown)
.hasMessageThat()
.contains("Cannot parse key string: kind:google.registry.testing.TestObject@sq:l@ofya:bc");
}
@Test
void testCreate_invalidOfyKeyString_failure() throws Exception {
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> VKey.create("invalid"));
assertThat(thrown).hasMessageThat().contains("Could not parse Reference");
}
/** Test stringify() then create() flow. */
@Test
void testStringifyThenCreate_sqlOnlyVKey_testObject_stringKey_success() throws Exception {
VKey<TestObject> vkey = VKey.createSql(TestObject.class, "foo");
VKey<TestObject> newVkey = VKey.create(vkey.stringify());
assertThat(newVkey).isEqualTo(vkey);
}
@Test
void testStringifyThenCreate_sqlOnlyVKey_testObject_longKey_success() throws Exception {
VKey<TestObject> vkey = VKey.createSql(TestObject.class, (long) 12345);
VKey<TestObject> newVkey = VKey.create(vkey.stringify());
assertThat(newVkey).isEqualTo(vkey);
}
@Test
void testCreate_createFromExistingOfyKey_success() throws Exception {
String keyString =
Key.create(newDomainBase("example.com", "ROID-1", persistActiveContact("contact-1")))
.getString();
assertThat(VKey.fromWebsafeKey(keyString)).isEqualTo(VKey.create(keyString));
}
@Test
void testStringifyThenCreate_ofyOnlyVKey_testObject_success() throws Exception {
VKey<TestObject> vkey =
VKey.createOfy(TestObject.class, Key.create(TestObject.class, "tmpKey"));
assertThat(VKey.create(vkey.stringify())).isEqualTo(vkey);
}
@Test
void testStringifyThenCreate_ofyOnlyVKey_testObject_websafeString_success() throws Exception {
VKey<TestObject> vkey = VKey.fromWebsafeKey(Key.create(TestObject.create("foo")).getString());
assertThat(VKey.create(vkey.stringify())).isEqualTo(vkey);
}
@Test
void testStringifyThenCreate_sqlAndOfyVKey_success() throws Exception {
VKey<TestObject> vkey =
VKey.create(TestObject.class, "foo", Key.create(TestObject.create("foo")));
assertThat(VKey.create(vkey.stringify())).isEqualTo(vkey);
}
@Test
void testStringifyThenCreate_asymmetricVKey_success() throws Exception {
VKey<TestObject> vkey =
VKey.create(TestObject.class, "sqlKey", Key.create(TestObject.create("foo")));
assertThat(VKey.create(vkey.stringify())).isEqualTo(vkey);
}
@Test
void testStringifyThenCreate_symmetricVKey_success() throws Exception {
VKey<TestObject> vkey = TestObject.create("foo").key();
assertThat(VKey.create(vkey.stringify())).isEqualTo(vkey);
}
@Entity
static class OtherObject {}
}