mirror of
https://github.com/google/nomulus.git
synced 2025-07-12 05:58:13 +02:00
Add MapUserType to support converstion between Map and hstore (#443)
This commit is contained in:
parent
cf0e8e1b26
commit
6aa7c19344
8 changed files with 293 additions and 40 deletions
|
@ -0,0 +1,140 @@
|
|||
// 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.model.transaction.TransactionManagerFactory.jpaTm;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import google.registry.model.ImmutableObject;
|
||||
import google.registry.model.transaction.JpaTestRules;
|
||||
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
|
||||
import java.util.Map;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.NoResultException;
|
||||
import org.hibernate.annotations.Type;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.JUnit4;
|
||||
|
||||
/** Unit tests for {@link MapUserType}. */
|
||||
@RunWith(JUnit4.class)
|
||||
public class MapUserTypeTest {
|
||||
|
||||
// Note that JpaIntegrationTestRule is used here as the hstore extension is installed
|
||||
// when nomulus.golden.sql is executed as the init script.
|
||||
@Rule
|
||||
public final JpaIntegrationTestRule jpaRule =
|
||||
new JpaTestRules.Builder().withEntityClass(TestEntity.class).buildIntegrationTestRule();
|
||||
|
||||
@Test
|
||||
public void roundTripConversion_returnsSameMap() {
|
||||
Map<String, String> map = ImmutableMap.of("key1", "value1", "key2", "value2");
|
||||
TestEntity testEntity = new TestEntity(map);
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity));
|
||||
TestEntity persisted =
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
|
||||
assertThat(persisted.map).containsExactly("key1", "value1", "key2", "value2");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMerge_succeeds() {
|
||||
Map<String, String> map = ImmutableMap.of("key1", "value1", "key2", "value2");
|
||||
TestEntity testEntity = new TestEntity(map);
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity));
|
||||
TestEntity persisted =
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
|
||||
persisted.map = ImmutableMap.of("key3", "value3");
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().merge(persisted));
|
||||
TestEntity updated =
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
|
||||
assertThat(updated.map).containsExactly("key3", "value3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValue_writesAndReadsNullSuccessfully() {
|
||||
TestEntity testEntity = new TestEntity(null);
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity));
|
||||
TestEntity persisted =
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
|
||||
assertThat(persisted.map).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyCollection_writesAndReadsEmptyCollectionSuccessfully() {
|
||||
TestEntity testEntity = new TestEntity(ImmutableMap.of());
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().persist(testEntity));
|
||||
TestEntity persisted =
|
||||
jpaTm().transact(() -> jpaTm().getEntityManager().find(TestEntity.class, "id"));
|
||||
assertThat(persisted.map).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNativeQuery_succeeds() throws Exception {
|
||||
executeNativeQuery(
|
||||
"INSERT INTO \"TestEntity\" (name, map) VALUES ('id', 'key1=>value1, key2=>value2')");
|
||||
|
||||
assertThat(
|
||||
getSingleResultFromNativeQuery(
|
||||
"SELECT map -> 'key1' FROM \"TestEntity\" WHERE name = 'id'"))
|
||||
.isEqualTo("value1");
|
||||
assertThat(
|
||||
getSingleResultFromNativeQuery(
|
||||
"SELECT map -> 'key2' FROM \"TestEntity\" WHERE name = 'id'"))
|
||||
.isEqualTo("value2");
|
||||
|
||||
executeNativeQuery("UPDATE \"TestEntity\" SET map = 'key3=>value3' WHERE name = 'id'");
|
||||
|
||||
assertThat(
|
||||
getSingleResultFromNativeQuery(
|
||||
"SELECT map -> 'key3' FROM \"TestEntity\" WHERE name = 'id'"))
|
||||
.isEqualTo("value3");
|
||||
|
||||
executeNativeQuery("DELETE FROM \"TestEntity\" WHERE name = 'id'");
|
||||
assertThrows(
|
||||
NoResultException.class,
|
||||
() ->
|
||||
getSingleResultFromNativeQuery(
|
||||
"SELECT map -> 'key3' FROM \"TestEntity\" WHERE name = 'id'"));
|
||||
}
|
||||
|
||||
private static Object getSingleResultFromNativeQuery(String sql) {
|
||||
return jpaTm()
|
||||
.transact(() -> jpaTm().getEntityManager().createNativeQuery(sql).getSingleResult());
|
||||
}
|
||||
|
||||
private static Object executeNativeQuery(String sql) {
|
||||
return jpaTm()
|
||||
.transact(() -> jpaTm().getEntityManager().createNativeQuery(sql).executeUpdate());
|
||||
}
|
||||
|
||||
@Entity(name = "TestEntity") // Override entity name to avoid the nested class reference.
|
||||
private static class TestEntity extends ImmutableObject {
|
||||
|
||||
@Id String name = "id";
|
||||
|
||||
@Type(type = "google.registry.persistence.MapUserType")
|
||||
Map<String, String> map;
|
||||
|
||||
private TestEntity() {}
|
||||
|
||||
private TestEntity(Map<String, String> map) {
|
||||
this.map = map;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,6 +16,7 @@ package google.registry.schema.integration;
|
|||
|
||||
import google.registry.model.registry.RegistryLockDaoTest;
|
||||
import google.registry.model.transaction.JpaTestRules.JpaIntegrationTestRule;
|
||||
import google.registry.persistence.MapUserTypeTest;
|
||||
import google.registry.schema.cursor.CursorDaoTest;
|
||||
import google.registry.schema.tld.PremiumListDaoTest;
|
||||
import google.registry.schema.tld.PremiumListUtilsTest;
|
||||
|
@ -45,6 +46,7 @@ import org.junit.runners.Suite.SuiteClasses;
|
|||
CreateReservedListCommandTest.class,
|
||||
CursorDaoTest.class,
|
||||
CreatePremiumListActionTest.class,
|
||||
MapUserTypeTest.class,
|
||||
PremiumListDaoTest.class,
|
||||
PremiumListUtilsTest.class,
|
||||
RegistryLockDaoTest.class,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue