Use JPA @Enumerated to convert enum to string (#466)

This commit is contained in:
Shicong Huang 2020-02-04 10:50:03 -05:00 committed by GitHub
parent c0afb9aeee
commit 9ea06ebf35
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 9 additions and 159 deletions

View file

@ -1,76 +0,0 @@
// 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;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import google.registry.model.ImmutableObject;
import google.registry.model.registrar.Registrar.Type;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
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;
/** Unit tests for {@link RegistrarTypeConverter}. */
@RunWith(JUnit4.class)
public class RegistrarTypeConverterTest {
@Rule
public final JpaUnitTestRule jpaRule =
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildUnitTestRule();
@Test
public void roundTripConversion_returnsSameEnum() {
TestEntity testEntity = new TestEntity(Type.MONITORING);
jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity));
TestEntity persisted =
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
assertThat(persisted.type).isEqualTo(Type.MONITORING);
}
@Test
public void testNativeQuery_succeeds() {
TestEntity testEntity = new TestEntity(Type.MONITORING);
jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity));
assertThat(
jpaTm()
.transact(
() ->
jpaTm()
.getEntityManager()
.createNativeQuery("SELECT type FROM \"TestEntity\" WHERE name = 'id'")
.getSingleResult()))
.isEqualTo("MONITORING");
}
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.
private static class TestEntity extends ImmutableObject {
@Id String name = "id";
Type type;
private TestEntity() {}
private TestEntity(Type type) {
this.type = type;
}
}
}

View file

@ -22,15 +22,17 @@ import google.registry.model.registrar.Registrar.State;
import google.registry.persistence.transaction.JpaTestRules;
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link RegistrarStateConverter}. */
/** Unit tests for {@link Enumerated} annotation. */
@RunWith(JUnit4.class)
public class RegistrarStateConverterTest {
public class StringValueEnumeratedTest {
@Rule
public final JpaUnitTestRule jpaRule =
@ -66,6 +68,7 @@ public class RegistrarStateConverterTest {
@Id String name = "id";
@Enumerated(EnumType.STRING)
State state;
private TestEntity() {}