Implement VKeyConverter (#538)

* Implement VKeyConverter

Implement a SQL converter that can be used for VKey objects.

Caveats:

- This only works with string columns (there's an excellent chance that all of
  our VKeys will use SQL string columns).
- Using this dpesn't establish a foreign key constraint between the referenced
  type (the "T" in VKey<T>) and the entity itself: this needs to be
  defined manually in the schema.
This commit is contained in:
Michael Muller 2020-04-16 09:45:23 -04:00 committed by GitHub
parent 0fcf26def0
commit ec22d4d1a0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 120 additions and 0 deletions

View file

@ -0,0 +1,41 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.persistence.converter;
import google.registry.persistence.VKey;
import javax.annotation.Nullable;
import javax.persistence.AttributeConverter;
/**
* Converts VKey to a string column.
*/
public abstract class VKeyConverter<T> implements AttributeConverter<VKey<T>, String> {
@Override
@Nullable
public String convertToDatabaseColumn(@Nullable VKey<T> attribute) {
return attribute == null ? null : (String) attribute.getSqlKey();
}
@Override
@Nullable
public VKey<T> convertToEntityAttribute(@Nullable String dbData) {
return dbData == null ? null : VKey.createSql(getAttributeClass(), dbData);
}
/**
* Returns the class of the attribute.
*/
protected abstract Class<T> getAttributeClass();
}

View file

@ -0,0 +1,79 @@
// Copyright 2020 The Nomulus Authors. All Rights Reserved.
//
// 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 google.registry.persistence.converter;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import google.registry.persistence.VKey;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Test SQL persistence of VKey. */
@RunWith(JUnit4.class)
public class VKeyConverterTest {
@Rule
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
public VKeyConverterTest() {}
@Test
public void testRoundTrip() {
TestEntity original =
new TestEntity("TheRealSpartacus", VKey.createSql(TestEntity.class, "ImSpartacus!"));
jpaTm().transact(() -> jpaTm().getEntityManager().persist(original));
TestEntity retrieved =
jpaTm().transact(
() -> jpaTm().getEntityManager().find(TestEntity.class, "TheRealSpartacus"));
assertThat(retrieved.other.getSqlKey()).isEqualTo("ImSpartacus!");
}
static class TestEntityVKeyConverter extends VKeyConverter<TestEntity> {
@Override
protected Class<TestEntity> getAttributeClass() {
return TestEntity.class;
}
}
@Entity(name = "TestEntity")
static class TestEntity {
@Id
String id;
// Specifying "@Converter(autoApply = true) on TestEntityVKeyConverter this doesn't seem to
// work.
@Convert(converter = TestEntityVKeyConverter.class)
VKey<TestEntity> other;
TestEntity(String id, VKey<TestEntity> other) {
this.id = id;
this.other = other;
}
/** Default constructor, needed for hibernate. */
public TestEntity() {}
}
}