Add DB annotations to console User and related classes (#1757)

We added the DB code last week, this is the corresponding bit now that
that has been released.
This commit is contained in:
gbrodman
2022-08-25 16:54:39 -04:00
committed by GitHub
parent b9f8faa165
commit 7aec579d96
8 changed files with 219 additions and 2 deletions
@@ -15,12 +15,71 @@
package google.registry.model.console;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatabaseHelper.persistResource;
import static org.junit.jupiter.api.Assertions.assertThrows;
import google.registry.model.EntityTestCase;
import org.junit.jupiter.api.Test;
/** Tests for {@link User}. */
public class UserTest {
public class UserTest extends EntityTestCase {
UserTest() {
super(JpaEntityCoverageCheck.ENABLED);
}
@Test
void testPersistence_lookupByEmail() {
User user =
new User.Builder()
.setGaiaId("gaiaId")
.setEmailAddress("email@email.com")
.setUserRoles(
new UserRoles.Builder().setGlobalRole(GlobalRole.FTE).setIsAdmin(true).build())
.build();
persistResource(user);
jpaTm()
.transact(
() -> {
assertThat(
jpaTm()
.query("FROM User WHERE emailAddress = 'email@email.com'", User.class)
.getSingleResult())
.isEqualTo(user);
assertThat(
jpaTm()
.query("FROM User WHERE emailAddress = 'bad@fake.com'", User.class)
.getResultList())
.isEmpty();
});
}
@Test
void testPersistence_lookupByGaiaId() {
User user =
new User.Builder()
.setGaiaId("gaiaId")
.setEmailAddress("email@email.com")
.setUserRoles(
new UserRoles.Builder().setGlobalRole(GlobalRole.FTE).setIsAdmin(true).build())
.build();
persistResource(user);
jpaTm()
.transact(
() -> {
assertThat(
jpaTm()
.query("FROM User WHERE gaiaId = 'gaiaId'", User.class)
.getSingleResult())
.isEqualTo(user);
assertThat(
jpaTm()
.query("FROM User WHERE gaiaId = 'badGaiaId'", User.class)
.getResultList())
.isEmpty();
});
}
@Test
void testFailure_badInputs() {
@@ -0,0 +1,68 @@
// Copyright 2022 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 com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import google.registry.model.ImmutableObject;
import google.registry.model.console.RegistrarRole;
import google.registry.persistence.transaction.JpaTestExtensions;
import google.registry.persistence.transaction.JpaTestExtensions.JpaUnitTestExtension;
import google.registry.testing.DatabaseHelper;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
/** Tests for {@link RegistrarToRoleConverter}. */
public class RegistrarToRoleConverterTest {
@RegisterExtension
public final JpaUnitTestExtension jpa =
new JpaTestExtensions.Builder().withEntityClass(TestEntity.class).buildUnitTestExtension();
@Test
void testRoundTripConversion() {
Map<String, RegistrarRole> map =
ImmutableMap.of(
"TheRegistrar",
RegistrarRole.ACCOUNT_MANAGER,
"NewRegistrar",
RegistrarRole.PRIMARY_CONTACT,
"FooRegistrar",
RegistrarRole.TECH_CONTACT);
TestEntity entity = new TestEntity(map);
DatabaseHelper.insertInDb(entity);
TestEntity persisted = Iterables.getOnlyElement(DatabaseHelper.loadAllOf(TestEntity.class));
assertThat(persisted.map).isEqualTo(map);
}
@Entity(name = "TestEntity")
private static class TestEntity extends ImmutableObject {
@Id String name = "id";
Map<String, RegistrarRole> map;
private TestEntity() {}
private TestEntity(Map<String, RegistrarRole> map) {
this.map = map;
}
}
}
@@ -18,6 +18,7 @@ import static com.google.common.truth.Truth.assert_;
import google.registry.model.billing.BillingEventTest;
import google.registry.model.common.CursorTest;
import google.registry.model.console.UserTest;
import google.registry.model.contact.ContactResourceTest;
import google.registry.model.domain.DomainSqlTest;
import google.registry.model.domain.token.AllocationTokenTest;
@@ -101,6 +102,7 @@ import org.junit.runner.RunWith;
SignedMarkRevocationListDaoTest.class,
Spec11ThreatMatchTest.class,
TmchCrlTest.class,
UserTest.class,
// AfterSuiteTest must be the last entry. See class javadoc for details.
AfterSuiteTest.class
})