mirror of
https://github.com/google/nomulus.git
synced 2025-07-08 20:23:24 +02:00
Create a Java entity to store ThreatMatch objects in SQL (#617)
* Squash everything together Create SafeBrowsing_Threats table Create LocalDateConverter and add indexes to SafeBrowsingThreats Add indexes to SafeBrowsingThreats and make small style changes Pass in DateTimeFormatter Delete LocalDateConverterTest.java Rebase Make changes to ThreatType comments Create LocalDateConverterTest Add review changes Add SafeBrowsingThreatTest Rename repoId, refactor LocalDateConverterTest/SafeBrowsingThreatTest, add foreign keys Change imports Add foreign keys and rename version number Add new generated db-schema file Clean up null test cases Add changes Add foreign keys into SafeBrowsingThreatTeat and apply style checks Add SafeBrowsingThreatTest into SqlIntegrationTestSuite and change golden file Make small changes to SafeBrowsingThreatTest Add tests for ForeignKeyViolations and remove setId in SafeBrowsingThreat * Change V35 -> V36 * Add a foreign key test for a reference to Registrar * Move some variables around
This commit is contained in:
parent
2c243a7d5f
commit
ec09226baa
10 changed files with 588 additions and 2 deletions
|
@ -0,0 +1,167 @@
|
|||
// 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.model.reporting;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.model.reporting.SafeBrowsingThreat.ThreatType.MALWARE;
|
||||
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
|
||||
import static google.registry.testing.DatastoreHelper.createTld;
|
||||
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
|
||||
import static google.registry.testing.SqlHelper.saveRegistrar;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import google.registry.model.EntityTestCase;
|
||||
import google.registry.model.contact.ContactResource;
|
||||
import google.registry.model.domain.DomainBase;
|
||||
import google.registry.model.host.HostResource;
|
||||
import google.registry.model.transfer.ContactTransferData;
|
||||
import google.registry.persistence.VKey;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/** Unit tests for {@link SafeBrowsingThreat}. */
|
||||
public class SafeBrowsingThreatTest extends EntityTestCase {
|
||||
|
||||
private static final String REGISTRAR_ID = "registrar";
|
||||
private static final LocalDate DATE = LocalDate.parse("2020-06-10", ISODateTimeFormat.date());
|
||||
|
||||
private SafeBrowsingThreat threat;
|
||||
private DomainBase domain;
|
||||
private HostResource host;
|
||||
private ContactResource registrantContact;
|
||||
|
||||
public SafeBrowsingThreatTest() {
|
||||
super(true);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
VKey<HostResource> hostVKey = VKey.createSql(HostResource.class, "host");
|
||||
VKey<ContactResource> registrantContactVKey =
|
||||
VKey.createSql(ContactResource.class, "contact_id");
|
||||
String domainRepoId = "4-TLD";
|
||||
createTld("tld");
|
||||
|
||||
/** Create a domain for the purpose of testing a foreign key reference in the Threat table. */
|
||||
domain =
|
||||
new DomainBase()
|
||||
.asBuilder()
|
||||
.setCreationClientId(REGISTRAR_ID)
|
||||
.setPersistedCurrentSponsorClientId(REGISTRAR_ID)
|
||||
.setDomainName("foo.tld")
|
||||
.setRepoId(domainRepoId)
|
||||
.setNameservers(hostVKey)
|
||||
.setRegistrant(registrantContactVKey)
|
||||
.setContacts(ImmutableSet.of())
|
||||
.build();
|
||||
|
||||
/** Create a contact for the purpose of testing a foreign key reference in the Domain table. */
|
||||
registrantContact =
|
||||
new ContactResource.Builder()
|
||||
.setRepoId("contact_id")
|
||||
.setCreationClientId(REGISTRAR_ID)
|
||||
.setTransferData(new ContactTransferData.Builder().build())
|
||||
.setPersistedCurrentSponsorClientId(REGISTRAR_ID)
|
||||
.build();
|
||||
|
||||
/** Create a host for the purpose of testing a foreign key reference in the Domain table. */
|
||||
host =
|
||||
new HostResource.Builder()
|
||||
.setRepoId("host")
|
||||
.setHostName("ns1.example.com")
|
||||
.setCreationClientId(REGISTRAR_ID)
|
||||
.setPersistedCurrentSponsorClientId(REGISTRAR_ID)
|
||||
.build();
|
||||
|
||||
threat =
|
||||
new SafeBrowsingThreat.Builder()
|
||||
.setThreatType(MALWARE)
|
||||
.setCheckDate(DATE)
|
||||
.setDomainName("foo.tld")
|
||||
.setDomainRepoId(domainRepoId)
|
||||
.setRegistrarId(REGISTRAR_ID)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPersistence() {
|
||||
saveRegistrar(REGISTRAR_ID);
|
||||
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> {
|
||||
jpaTm().saveNew(registrantContact);
|
||||
jpaTm().saveNew(domain);
|
||||
jpaTm().saveNew(host);
|
||||
jpaTm().saveNew(threat);
|
||||
});
|
||||
|
||||
VKey<SafeBrowsingThreat> threatVKey = VKey.createSql(SafeBrowsingThreat.class, threat.getId());
|
||||
SafeBrowsingThreat persistedThreat = jpaTm().transact(() -> jpaTm().load(threatVKey));
|
||||
threat.id = persistedThreat.id;
|
||||
assertThat(threat).isEqualTo(persistedThreat);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testThreatForeignKeyConstraints() {
|
||||
assertThrowForeignKeyViolation(
|
||||
() -> {
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> {
|
||||
// Persist the threat without the associated registrar.
|
||||
jpaTm().saveNew(host);
|
||||
jpaTm().saveNew(registrantContact);
|
||||
jpaTm().saveNew(domain);
|
||||
jpaTm().saveNew(threat);
|
||||
});
|
||||
});
|
||||
|
||||
saveRegistrar(REGISTRAR_ID);
|
||||
|
||||
assertThrowForeignKeyViolation(
|
||||
() -> {
|
||||
jpaTm()
|
||||
.transact(
|
||||
() -> {
|
||||
// Persist the threat without the associated domain.
|
||||
jpaTm().saveNew(registrantContact);
|
||||
jpaTm().saveNew(host);
|
||||
jpaTm().saveNew(threat);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFailure_threatsWithNullFields() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setRegistrarId(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setDomainName(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setCheckDate(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setThreatType(null).build());
|
||||
|
||||
assertThrows(
|
||||
IllegalArgumentException.class, () -> threat.asBuilder().setDomainRepoId(null).build());
|
||||
}
|
||||
}
|
|
@ -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.model.ImmutableObject;
|
||||
import google.registry.persistence.VKey;
|
||||
import google.registry.persistence.transaction.JpaTestRules;
|
||||
import google.registry.persistence.transaction.JpaTestRules.JpaUnitTestRule;
|
||||
import google.registry.schema.replay.EntityTest;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.format.ISODateTimeFormat;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.RegisterExtension;
|
||||
|
||||
/** Unit tests for {@link LocalDateConverter}. */
|
||||
public class LocalDateConverterTest {
|
||||
|
||||
@RegisterExtension
|
||||
public final JpaUnitTestRule jpaRule =
|
||||
new JpaTestRules.Builder()
|
||||
.withEntityClass(LocalDateConverterTestEntity.class)
|
||||
.buildUnitTestRule();
|
||||
|
||||
private final LocalDate exampleDate = LocalDate.parse("2020-06-10", ISODateTimeFormat.date());
|
||||
|
||||
@Test
|
||||
public void testNullInput() {
|
||||
LocalDateConverterTestEntity retrievedEntity = persistAndLoadTestEntity(null);
|
||||
assertThat(retrievedEntity.date).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveAndLoad_success() {
|
||||
LocalDateConverterTestEntity retrievedEntity = persistAndLoadTestEntity(exampleDate);
|
||||
assertThat(retrievedEntity.date).isEqualTo(exampleDate);
|
||||
}
|
||||
|
||||
private LocalDateConverterTestEntity persistAndLoadTestEntity(LocalDate date) {
|
||||
LocalDateConverterTestEntity entity = new LocalDateConverterTestEntity(date);
|
||||
jpaTm().transact(() -> jpaTm().saveNew(entity));
|
||||
LocalDateConverterTestEntity retrievedEntity =
|
||||
jpaTm()
|
||||
.transact(() -> jpaTm().load(VKey.createSql(LocalDateConverterTestEntity.class, "id")));
|
||||
return retrievedEntity;
|
||||
}
|
||||
|
||||
/** Override entity name to avoid the nested class reference. */
|
||||
@Entity(name = "LocalDateConverterTestEntity")
|
||||
@EntityTest.EntityForTesting
|
||||
private static class LocalDateConverterTestEntity extends ImmutableObject {
|
||||
|
||||
@Id String name = "id";
|
||||
|
||||
LocalDate date;
|
||||
|
||||
public LocalDateConverterTestEntity() {}
|
||||
|
||||
LocalDateConverterTestEntity(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,6 +22,7 @@ import google.registry.model.domain.DomainBaseSqlTest;
|
|||
import google.registry.model.history.HostHistoryTest;
|
||||
import google.registry.model.poll.PollMessageTest;
|
||||
import google.registry.model.registry.RegistryLockDaoTest;
|
||||
import google.registry.model.reporting.SafeBrowsingThreatTest;
|
||||
import google.registry.persistence.transaction.JpaEntityCoverage;
|
||||
import google.registry.schema.cursor.CursorDaoTest;
|
||||
import google.registry.schema.integration.SqlIntegrationTestSuite.AfterSuiteTest;
|
||||
|
@ -83,6 +84,7 @@ import org.junit.runner.RunWith;
|
|||
RegistrarDaoTest.class,
|
||||
RegistryLockDaoTest.class,
|
||||
ReservedListDaoTest.class,
|
||||
SafeBrowsingThreatTest.class,
|
||||
// AfterSuiteTest must be the last entry. See class javadoc for details.
|
||||
AfterSuiteTest.class
|
||||
})
|
||||
|
|
|
@ -25,7 +25,7 @@ import google.registry.model.registry.RegistryLockDao;
|
|||
import google.registry.schema.domain.RegistryLock;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Optional;
|
||||
import javax.persistence.RollbackException;
|
||||
import javax.persistence.PersistenceException;
|
||||
import org.junit.function.ThrowingRunnable;
|
||||
|
||||
/** Static utils for setting up and retrieving test resources from the SQL database. */
|
||||
|
@ -66,7 +66,7 @@ public class SqlHelper {
|
|||
}
|
||||
|
||||
public static void assertThrowForeignKeyViolation(ThrowingRunnable runnable) {
|
||||
RollbackException thrown = assertThrows(RollbackException.class, runnable);
|
||||
PersistenceException thrown = assertThrows(PersistenceException.class, runnable);
|
||||
assertThat(Throwables.getRootCause(thrown)).isInstanceOf(SQLException.class);
|
||||
assertThat(Throwables.getRootCause(thrown))
|
||||
.hasMessageThat()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue