Allow multiple threat types in the Spec11ThreatMatch table (#650)

* Update to generic Spec11ThreatMatch table

* Fix SQL syntax

* Make changes to the schema and add a test for null and empty threatTypes

* Fix a small typo

* Change the exception thrown with illegal arguments

Change the import for isNullOrEmpty

* Fix import for checkArgument

* Added a threat to test multiple threat types
This commit is contained in:
Legina Chen 2020-06-26 10:35:00 -07:00 committed by GitHub
parent 74b2de5c35
commit 5c5b6b20ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 129 additions and 56 deletions

View file

@ -15,7 +15,8 @@
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.model.reporting.Spec11ThreatMatch.ThreatType.MALWARE;
import static google.registry.model.reporting.Spec11ThreatMatch.ThreatType.UNWANTED_SOFTWARE;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.SqlHelper.assertThrowForeignKeyViolation;
@ -34,18 +35,18 @@ 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 {
/** Unit tests for {@link Spec11ThreatMatch}. */
public class Spec11ThreatMatchTest 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 Spec11ThreatMatch threat;
private DomainBase domain;
private HostResource host;
private ContactResource registrantContact;
public SafeBrowsingThreatTest() {
public Spec11ThreatMatchTest() {
super(true);
}
@ -89,8 +90,8 @@ public class SafeBrowsingThreatTest extends EntityTestCase {
.build();
threat =
new SafeBrowsingThreat.Builder()
.setThreatType(MALWARE)
new Spec11ThreatMatch.Builder()
.setThreatTypes(ImmutableSet.of(MALWARE, UNWANTED_SOFTWARE))
.setCheckDate(DATE)
.setDomainName("foo.tld")
.setDomainRepoId(domainRepoId)
@ -111,8 +112,8 @@ public class SafeBrowsingThreatTest extends EntityTestCase {
jpaTm().saveNew(threat);
});
VKey<SafeBrowsingThreat> threatVKey = VKey.createSql(SafeBrowsingThreat.class, threat.getId());
SafeBrowsingThreat persistedThreat = jpaTm().transact(() -> jpaTm().load(threatVKey));
VKey<Spec11ThreatMatch> threatVKey = VKey.createSql(Spec11ThreatMatch.class, threat.getId());
Spec11ThreatMatch persistedThreat = jpaTm().transact(() -> jpaTm().load(threatVKey));
threat.id = persistedThreat.id;
assertThat(threat).isEqualTo(persistedThreat);
}
@ -148,7 +149,7 @@ public class SafeBrowsingThreatTest extends EntityTestCase {
}
@Test
public void testFailure_threatsWithNullFields() {
public void testFailure_threatsWithInvalidFields() {
assertThrows(
IllegalArgumentException.class, () -> threat.asBuilder().setRegistrarId(null).build());
@ -159,9 +160,12 @@ public class SafeBrowsingThreatTest extends EntityTestCase {
IllegalArgumentException.class, () -> threat.asBuilder().setCheckDate(null).build());
assertThrows(
IllegalArgumentException.class, () -> threat.asBuilder().setThreatType(null).build());
IllegalArgumentException.class, () -> threat.asBuilder().setDomainRepoId(null).build());
assertThrows(
IllegalArgumentException.class, () -> threat.asBuilder().setDomainRepoId(null).build());
IllegalArgumentException.class, () -> threat.asBuilder().setThreatTypes(ImmutableSet.of()));
assertThrows(
IllegalArgumentException.class, () -> threat.asBuilder().setThreatTypes(null).build());
}
}

View file

@ -22,7 +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.model.reporting.Spec11ThreatMatchTest;
import google.registry.persistence.transaction.JpaEntityCoverage;
import google.registry.schema.cursor.CursorDaoTest;
import google.registry.schema.integration.SqlIntegrationTestSuite.AfterSuiteTest;
@ -84,7 +84,7 @@ import org.junit.runner.RunWith;
RegistrarDaoTest.class,
RegistryLockDaoTest.class,
ReservedListDaoTest.class,
SafeBrowsingThreatTest.class,
Spec11ThreatMatchTest.class,
// AfterSuiteTest must be the last entry. See class javadoc for details.
AfterSuiteTest.class
})