diff --git a/core/src/main/java/google/registry/persistence/converter/PremiumListKeyConverter.java b/core/src/main/java/google/registry/persistence/converter/PremiumListKeyConverter.java new file mode 100644 index 000000000..580694816 --- /dev/null +++ b/core/src/main/java/google/registry/persistence/converter/PremiumListKeyConverter.java @@ -0,0 +1,37 @@ +// 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 google.registry.model.common.EntityGroupRoot.getCrossTldKey; + +import com.googlecode.objectify.Key; +import google.registry.model.registry.label.PremiumList; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +/** JPA converter for a {@link Key} containing a {@link PremiumList} */ +@Converter(autoApply = true) +public class PremiumListKeyConverter implements AttributeConverter, String> { + + @Override + public String convertToDatabaseColumn(Key attribute) { + return (attribute == null) ? null : attribute.getName(); + } + + @Override + public Key convertToEntityAttribute(String dbData) { + return (dbData == null) ? null : Key.create(getCrossTldKey(), PremiumList.class, dbData); + } +} diff --git a/core/src/main/java/google/registry/persistence/converter/ReservedListKeyConverter.java b/core/src/main/java/google/registry/persistence/converter/ReservedListKeyConverter.java new file mode 100644 index 000000000..f0f370279 --- /dev/null +++ b/core/src/main/java/google/registry/persistence/converter/ReservedListKeyConverter.java @@ -0,0 +1,37 @@ +// 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 google.registry.model.common.EntityGroupRoot.getCrossTldKey; + +import com.googlecode.objectify.Key; +import google.registry.model.registry.label.ReservedList; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; + +/** JPA converter for a {@link Key} containing a {@link ReservedList} */ +@Converter(autoApply = true) +public class ReservedListKeyConverter implements AttributeConverter, String> { + + @Override + public String convertToDatabaseColumn(Key attribute) { + return (attribute == null) ? null : attribute.getName(); + } + + @Override + public Key convertToEntityAttribute(String dbData) { + return (dbData == null) ? null : Key.create(getCrossTldKey(), ReservedList.class, dbData); + } +} diff --git a/core/src/main/resources/META-INF/persistence.xml b/core/src/main/resources/META-INF/persistence.xml index d43c8e56c..28988f43e 100644 --- a/core/src/main/resources/META-INF/persistence.xml +++ b/core/src/main/resources/META-INF/persistence.xml @@ -79,7 +79,9 @@ google.registry.persistence.converter.InetAddressSetConverter google.registry.persistence.converter.LocalDateConverter google.registry.persistence.converter.PostalInfoChoiceListConverter + google.registry.persistence.converter.PremiumListKeyConverter google.registry.persistence.converter.RegistrarPocSetConverter + google.registry.persistence.converter.ReservedListKeyConverter google.registry.persistence.converter.Spec11ThreatMatchThreatTypeSetConverter google.registry.persistence.converter.StatusValueSetConverter google.registry.persistence.converter.StringListConverter diff --git a/core/src/test/java/google/registry/persistence/converter/PremiumListKeyConverterTest.java b/core/src/test/java/google/registry/persistence/converter/PremiumListKeyConverterTest.java new file mode 100644 index 000000000..308837675 --- /dev/null +++ b/core/src/test/java/google/registry/persistence/converter/PremiumListKeyConverterTest.java @@ -0,0 +1,90 @@ +// 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.model.common.EntityGroupRoot.getCrossTldKey; +import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; + +import com.googlecode.objectify.Key; +import google.registry.model.ImmutableObject; +import google.registry.model.registry.label.PremiumList; +import google.registry.testing.AppEngineExtension; +import javax.persistence.Entity; +import javax.persistence.Id; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +/** Unit tests for {@link PremiumListKeyConverter}. */ +class PremiumListKeyConverterTest { + + @RegisterExtension + final AppEngineExtension appEngine = + AppEngineExtension.builder() + .withDatastoreAndCloudSql() + .withJpaUnitTestEntities(PremiumListEntity.class) + .build(); + + private final PremiumListKeyConverter converter = new PremiumListKeyConverter(); + + @Test + void convertToDatabaseColumn_returnsNullIfInputIsNull() { + assertThat(converter.convertToDatabaseColumn(null)).isNull(); + } + + @Test + void convertToDatabaseColumn_convertsCorrectly() { + assertThat( + converter.convertToDatabaseColumn( + Key.create(getCrossTldKey(), PremiumList.class, "testList"))) + .isEqualTo("testList"); + } + + @Test + void convertToEntityAttribute_returnsNullIfInputIsNull() { + assertThat(converter.convertToEntityAttribute(null)).isNull(); + } + + @Test + void convertToEntityAttribute_convertsCorrectly() { + assertThat(converter.convertToEntityAttribute("testList")) + .isEqualTo(Key.create(getCrossTldKey(), PremiumList.class, "testList")); + } + + @Test + void testRoundTrip() { + Key key = Key.create(getCrossTldKey(), PremiumList.class, "test"); + PremiumListEntity testEntity = new PremiumListEntity(key); + jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity)); + PremiumListEntity persisted = + jpaTm().transact(() -> jpaTm().getEntityManager().find(PremiumListEntity.class, "test")); + assertThat(persisted.premiumList).isEqualTo(key); + } + + @Entity(name = "PremiumListEntity") + private static class PremiumListEntity extends ImmutableObject { + + @Id String name; + + Key premiumList; + + public PremiumListEntity() {} + + PremiumListEntity(Key premiumList) { + this.name = premiumList.getName(); + this.premiumList = premiumList; + } + } +} diff --git a/core/src/test/java/google/registry/persistence/converter/ReservedListKeyConverterTest.java b/core/src/test/java/google/registry/persistence/converter/ReservedListKeyConverterTest.java new file mode 100644 index 000000000..dd2122511 --- /dev/null +++ b/core/src/test/java/google/registry/persistence/converter/ReservedListKeyConverterTest.java @@ -0,0 +1,90 @@ +// 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.model.common.EntityGroupRoot.getCrossTldKey; +import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; + +import com.googlecode.objectify.Key; +import google.registry.model.ImmutableObject; +import google.registry.model.registry.label.ReservedList; +import google.registry.testing.AppEngineExtension; +import javax.persistence.Entity; +import javax.persistence.Id; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +/** Unit tests for {@link ReservedListKeyConverter}. */ +class ReservedListKeyConverterTest { + + @RegisterExtension + final AppEngineExtension appEngine = + AppEngineExtension.builder() + .withDatastoreAndCloudSql() + .withJpaUnitTestEntities(ReservedListEntity.class) + .build(); + + private final ReservedListKeyConverter converter = new ReservedListKeyConverter(); + + @Test + void convertToDatabaseColumn_returnsNullIfInputIsNull() { + assertThat(converter.convertToDatabaseColumn(null)).isNull(); + } + + @Test + void convertToDatabaseColumn_convertsCorrectly() { + assertThat( + converter.convertToDatabaseColumn( + Key.create(getCrossTldKey(), ReservedList.class, "testList"))) + .isEqualTo("testList"); + } + + @Test + void convertToEntityAttribute_returnsNullIfInputIsNull() { + assertThat(converter.convertToEntityAttribute(null)).isNull(); + } + + @Test + void convertToEntityAttribute_convertsCorrectly() { + assertThat(converter.convertToEntityAttribute("testList")) + .isEqualTo(Key.create(getCrossTldKey(), ReservedList.class, "testList")); + } + + @Test + void testRoundTrip() { + Key key = Key.create(getCrossTldKey(), ReservedList.class, "test"); + ReservedListEntity testEntity = new ReservedListEntity(key); + jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity)); + ReservedListEntity persisted = + jpaTm().transact(() -> jpaTm().getEntityManager().find(ReservedListEntity.class, "test")); + assertThat(persisted.reservedList).isEqualTo(key); + } + + @Entity(name = "ReservedListEntity") + private static class ReservedListEntity extends ImmutableObject { + + @Id String name; + + Key reservedList; + + public ReservedListEntity() {} + + ReservedListEntity(Key reservedList) { + this.name = reservedList.getName(); + this.reservedList = reservedList; + } + } +}