From 6856df6192bea54630ac1205182fd6355ff91881 Mon Sep 17 00:00:00 2001 From: Ed Shryane Date: Wed, 20 Jul 2022 17:18:45 +0200 Subject: [PATCH 1/6] Move references DAO methods to RPSL object DAO (they don't get updated) --- .../db/whois/api/rest/ReferencesService.java | 11 +- .../db/whois/common/dao/RpslObjectDao.java | 14 ++- .../whois/common/dao/RpslObjectUpdateDao.java | 16 --- .../common/dao/jdbc/JdbcRpslObjectDao.java | 117 ++++++++++++++++++ .../dao/jdbc/JdbcRpslObjectUpdateDao.java | 113 ----------------- .../JdbcRpslObjectDaoIntegrationTest.java | 99 ++++++++++++++- ...RpslObjectUpdateDaoCtdIntegrationTest.java | 109 ---------------- .../common/ObjectReferencedValidator.java | 10 +- .../ReferencedObjectsExistValidator.java | 10 +- .../validator/inetnum/ReferenceCheck.java | 2 +- .../organisation/AbuseValidator.java | 7 +- .../OrgNameAndCountryAttrValidator.java | 13 +- ...KeepAbuseMailboxIfReferencedValidator.java | 12 +- .../common/ObjectReferencedValidatorTest.java | 8 +- .../ReferencedObjectsExistValidatorTest.java | 8 +- .../validator/inetnum/ReferenceCheckTest.java | 16 ++- .../organisation/AbuseValidatorTest.java | 8 +- .../OrgNameAndCountryAttrValidatorTest.java | 8 +- ...AbuseMailboxIfReferencedValidatorTest.java | 4 +- 19 files changed, 279 insertions(+), 306 deletions(-) diff --git a/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java b/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java index d7ac84b18b..f8003aa347 100644 --- a/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java +++ b/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java @@ -19,7 +19,6 @@ import net.ripe.db.whois.common.Messages; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.rpsl.AttributeTemplate; import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectTemplate; @@ -41,6 +40,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.DataAccessException; import org.springframework.dao.EmptyResultDataAccessException; @@ -93,7 +93,6 @@ public class ReferencesService { private static final Logger LOGGER = LoggerFactory.getLogger(ReferencesService.class); private final RpslObjectDao rpslObjectDao; - private final RpslObjectUpdateDao rpslObjectUpdateDao; private final SourceContext sourceContext; private final InternalUpdatePerformer updatePerformer; private final SsoTranslator ssoTranslator; @@ -104,8 +103,7 @@ public class ReferencesService { @Autowired public ReferencesService( - final RpslObjectDao rpslObjectDao, - final RpslObjectUpdateDao rpslObjectUpdateDao, + @Qualifier("jdbcRpslObjectSlaveDao") final RpslObjectDao rpslObjectDao, final SourceContext sourceContext, final InternalUpdatePerformer updatePerformer, final SsoTranslator ssoTranslator, @@ -114,7 +112,6 @@ public ReferencesService( final @Value("#{${whois.dummy}}") Map dummyMap) { this.rpslObjectDao = rpslObjectDao; - this.rpslObjectUpdateDao = rpslObjectUpdateDao; this.sourceContext = sourceContext; this.updatePerformer = updatePerformer; this.ssoTranslator = ssoTranslator; @@ -572,7 +569,7 @@ private void validateReferences(final RpslObject primaryObject, final Map entry : references.entrySet()) { final RpslObject reference = entry.getValue(); - for (final RpslObjectInfo referenceToReference : rpslObjectUpdateDao.getReferences(reference)) { + for (final RpslObjectInfo referenceToReference : rpslObjectDao.getReferences(reference)) { if (!referenceMatches(referenceToReference, primaryObject) && !references.keySet().contains(referenceToReference)) { throw new IllegalArgumentException("Referencing object " + reference.getKey() + " itself is referenced by " + referenceToReference.getKey()); } @@ -718,7 +715,7 @@ private RpslObject lookupObjectByKey(final String primaryKey, final ObjectType o private Map findReferences(final RpslObject rpslObject) { final Map references = Maps.newHashMap(); try { - for (final RpslObjectInfo rpslObjectInfo : rpslObjectUpdateDao.getReferences(rpslObject)) { + for (final RpslObjectInfo rpslObjectInfo : rpslObjectDao.getReferences(rpslObject)) { references.put(rpslObjectInfo, rpslObjectDao.getById(rpslObjectInfo.getObjectId())); } } catch (EmptyResultDataAccessException e) { diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java index b81c0c349f..aaed6f7a85 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java @@ -5,12 +5,15 @@ import net.ripe.db.whois.common.domain.Identifiable; import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; +import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; -import java.time.LocalDateTime; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import java.time.LocalDateTime; import java.util.Collection; import java.util.List; +import java.util.Map; import java.util.Set; // these should return Collection<> instead of List<> to allow for greater flexibility in implementation @@ -50,4 +53,13 @@ public interface RpslObjectDao extends ProxyLoader { List findMemberOfByObjectTypeWithoutMbrsByRef(ObjectType objectType, String attributeValue); Collection relatedTo(RpslObject identifiable, Set excludeObjectTypes); + + boolean isReferenced(RpslObject object); + + Set getReferences(RpslObject object); + + Map> getInvalidReferences(RpslObject object); + + @CheckForNull + RpslObjectInfo getAttributeReference(AttributeType attributeType, CIString keyValue); } diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectUpdateDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectUpdateDao.java index cc2ebb2ee4..d3c471c658 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectUpdateDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectUpdateDao.java @@ -1,15 +1,8 @@ package net.ripe.db.whois.common.dao; -import net.ripe.db.whois.common.domain.CIString; -import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; -import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; -import javax.annotation.CheckForNull; -import java.util.Map; -import java.util.Set; - public interface RpslObjectUpdateDao { RpslObjectUpdateInfo lookupObject(ObjectType type, String pkey); @@ -21,13 +14,4 @@ public interface RpslObjectUpdateDao { RpslObjectUpdateInfo updateObject(int objectId, RpslObject object); RpslObjectUpdateInfo createObject(RpslObject object); - - boolean isReferenced(RpslObject object); - - Set getReferences(RpslObject object); - - Map> getInvalidReferences(RpslObject object); - - @CheckForNull - RpslObjectInfo getAttributeReference(AttributeType attributeType, CIString keyValue); } diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java index 183d955981..4e3ec5c931 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java @@ -5,6 +5,7 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.ripe.db.whois.common.aspects.RetryFor; +import net.ripe.db.whois.common.collect.CollectionHelper; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.dao.jdbc.domain.RpslObjectInfoResultSetExtractor; @@ -52,6 +53,8 @@ import java.util.Map; import java.util.Set; +import static net.ripe.db.whois.common.domain.CIString.ciString; + @Repository @Primary @RetryFor(RecoverableDataAccessException.class) @@ -346,4 +349,118 @@ public Collection relatedTo(final RpslObject identifiable, final return result; } + + + @Override + public boolean isReferenced(final RpslObject object) { + for (final RpslAttribute attribute : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { + for (final IndexStrategy indexStrategy : IndexStrategies.getReferencing(object.getType())) { + for (final CIString value : attribute.getReferenceValues()) { + for (final RpslObjectInfo result : indexStrategy.findInIndex(jdbcTemplate, value)) { + if (object.getKey().equals(ciString(result.getKey())) && result.getObjectType().equals(object.getType())) { + continue; + } + + return true; + } + } + } + } + return false; + } + + @Override + public Set getReferences(final RpslObject object) { + Set references = Sets.newHashSet(); + final List indexStrategies = IndexStrategies.getReferencing(object.getType()); + + // for route(6), individually check each key + for (final RpslAttribute keyAttr : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { + for (final IndexStrategy indexStrategy : indexStrategies) { + for (final CIString value : keyAttr.getReferenceValues()) { + final List results = indexStrategy.findInIndex(jdbcTemplate, value); + for (final RpslObjectInfo result : results) { + if (object.getKey().equals(result.getKey()) && result.getObjectType().equals(object.getType())) { + continue; + } + + references.add(result); + } + } + } + } + + return references; + } + @Override + public Map> getInvalidReferences(final RpslObject object) { + final Map> invalidReferenceMap = Maps.newHashMap(); + + for (final RpslAttribute attribute : object.getAttributes()) { + final Set invalidReferenceValues = getInvalidReferences(object, attribute); + if (!invalidReferenceValues.isEmpty()) { + invalidReferenceMap.put(attribute, invalidReferenceValues); + } + } + + return invalidReferenceMap; + } + + private Set getInvalidReferences(final RpslObject object, final RpslAttribute attribute) { + AttributeType attributeType = attribute.getType(); + if (attributeType != null && attributeType.getReferences().isEmpty()) { + return Collections.emptySet(); + } + + final Set invalidReferences = Sets.newLinkedHashSet(); + for (final CIString value : attribute.getReferenceValues()) { + if (isInvalidReference(object, attributeType, value)) { + invalidReferences.add(value); + } + } + + return invalidReferences; + } + + private boolean isInvalidReference(final RpslObject object, final AttributeType attributeType, final CIString referenceValue) { + final Set references = attributeType.getReferences(referenceValue); + if (references.isEmpty()) { + return false; + } + + for (final ObjectType reference : references) { + if (reference.equals(object.getType()) && object.getKey().equals(referenceValue)) { + return false; + } + + if (getAttributeReference(reference, referenceValue) != null) { + return false; + } + } + + return true; + } + + @CheckForNull + public RpslObjectInfo getAttributeReference(final AttributeType attributeType, final CIString value) { + final CIString referenceValue = new RpslAttribute(attributeType, value.toString()).getReferenceValue(); + for (final ObjectType objectType : attributeType.getReferences()) { + final RpslObjectInfo result = getAttributeReference(objectType, referenceValue); + if (result != null) { + return result; + } + } + + return null; + } + + private RpslObjectInfo getAttributeReference(final ObjectType objectType, final CIString keyValue) { + final ObjectTemplate referenceTemplate = ObjectTemplate.getTemplate(objectType); + final Set referenceKeyAttributes = referenceTemplate.getKeyAttributes(); + Validate.isTrue(referenceKeyAttributes.size() == 1, "We can never have a reference to a composed key"); + final IndexStrategy indexStrategy = IndexStrategies.get(referenceKeyAttributes.iterator().next()); + final List result = indexStrategy.findInIndex(jdbcTemplate, keyValue); + return CollectionHelper.uniqueResult(result); + } + } diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDao.java index 300f349be8..f281d1c9dd 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDao.java @@ -57,119 +57,6 @@ public JdbcRpslObjectUpdateDao(@Qualifier("sourceAwareDataSource") final DataSou this.dateTimeProvider = dateTimeProvider; } - @Override - public boolean isReferenced(final RpslObject object) { - for (final RpslAttribute attribute : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { - for (final IndexStrategy indexStrategy : IndexStrategies.getReferencing(object.getType())) { - for (final CIString value : attribute.getReferenceValues()) { - for (final RpslObjectInfo result : indexStrategy.findInIndex(jdbcTemplate, value)) { - if (object.getKey().equals(ciString(result.getKey())) && result.getObjectType().equals(object.getType())) { - continue; - } - - return true; - } - } - } - } - return false; - } - - @Override - public Set getReferences(final RpslObject object) { - Set references = Sets.newHashSet(); - final List indexStrategies = IndexStrategies.getReferencing(object.getType()); - - // for route(6), individually check each key - for (final RpslAttribute keyAttr : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { - for (final IndexStrategy indexStrategy : indexStrategies) { - for (final CIString value : keyAttr.getReferenceValues()) { - final List results = indexStrategy.findInIndex(jdbcTemplate, value); - for (final RpslObjectInfo result : results) { - if (object.getKey().equals(result.getKey()) && result.getObjectType().equals(object.getType())) { - continue; - } - - references.add(result); - } - } - } - } - - return references; - } - - @Override - public Map> getInvalidReferences(final RpslObject object) { - final Map> invalidReferenceMap = Maps.newHashMap(); - - for (final RpslAttribute attribute : object.getAttributes()) { - final Set invalidReferenceValues = getInvalidReferences(object, attribute); - if (!invalidReferenceValues.isEmpty()) { - invalidReferenceMap.put(attribute, invalidReferenceValues); - } - } - - return invalidReferenceMap; - } - - private Set getInvalidReferences(final RpslObject object, final RpslAttribute attribute) { - AttributeType attributeType = attribute.getType(); - if (attributeType != null && attributeType.getReferences().isEmpty()) { - return Collections.emptySet(); - } - - final Set invalidReferences = Sets.newLinkedHashSet(); - for (final CIString value : attribute.getReferenceValues()) { - if (isInvalidReference(object, attributeType, value)) { - invalidReferences.add(value); - } - } - - return invalidReferences; - } - - private boolean isInvalidReference(final RpslObject object, final AttributeType attributeType, final CIString referenceValue) { - final Set references = attributeType.getReferences(referenceValue); - if (references.isEmpty()) { - return false; - } - - for (final ObjectType reference : references) { - if (reference.equals(object.getType()) && object.getKey().equals(referenceValue)) { - return false; - } - - if (getAttributeReference(reference, referenceValue) != null) { - return false; - } - } - - return true; - } - - @CheckForNull - public RpslObjectInfo getAttributeReference(final AttributeType attributeType, final CIString value) { - final CIString referenceValue = new RpslAttribute(attributeType, value.toString()).getReferenceValue(); - for (final ObjectType objectType : attributeType.getReferences()) { - final RpslObjectInfo result = getAttributeReference(objectType, referenceValue); - if (result != null) { - return result; - } - } - - return null; - } - - private RpslObjectInfo getAttributeReference(final ObjectType objectType, final CIString keyValue) { - final ObjectTemplate referenceTemplate = ObjectTemplate.getTemplate(objectType); - final Set referenceKeyAttributes = referenceTemplate.getKeyAttributes(); - Validate.isTrue(referenceKeyAttributes.size() == 1, "We can never have a reference to a composed key"); - final IndexStrategy indexStrategy = IndexStrategies.get(referenceKeyAttributes.iterator().next()); - final List result = indexStrategy.findInIndex(jdbcTemplate, keyValue); - return CollectionHelper.uniqueResult(result); - } - @Override public RpslObjectUpdateInfo deleteObject(final int objectId, final String pkey) { final RpslObjectUpdateInfo rpslObjectInfo = lookupRpslObjectUpdateInfo(jdbcTemplate, objectId, pkey); diff --git a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java index e2563ace5c..e93a7a1193 100644 --- a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java +++ b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java @@ -4,7 +4,9 @@ import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.domain.CIString; +import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; +import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.common.source.Source; import net.ripe.db.whois.common.support.AbstractDaoIntegrationTest; @@ -12,7 +14,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.dao.EmptyResultDataAccessException; @@ -20,14 +21,17 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Set; import static net.ripe.db.whois.common.domain.CIString.ciSet; +import static net.ripe.db.whois.common.domain.CIString.ciString; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.hamcrest.MatcherAssert.assertThat; @org.junit.jupiter.api.Tag("IntegrationTest") public class JdbcRpslObjectDaoIntegrationTest extends AbstractDaoIntegrationTest { @@ -420,4 +424,95 @@ public void getById() { assertThat(object, is(rpslObject)); } + @Test + public void invalid_reference() { + final RpslObject maintainer = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC-TEST"); + + final Map> invalidReferences = subject.getInvalidReferences(maintainer); + assertThat(invalidReferences.keySet(), contains(maintainer.findAttribute(AttributeType.ADMIN_C))); + assertThat(invalidReferences.get(maintainer.findAttribute(AttributeType.ADMIN_C)), contains(ciString("NIC-TEST"))); + } + + @Test + public void valid_reference() { + final RpslObject noInvalidRefPerson = RpslObject.parse("person: other person\nnic-hdl:NIC2-TEST"); + + assertThat(subject.getInvalidReferences(noInvalidRefPerson).keySet(), hasSize(0)); + } + + @Test + public void valid_persisted_reference() { + databaseHelper.addObject(RpslObject.parse("person: person\nnic-hdl:NIC1-TEST")); + final RpslObject maintainer2 = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC1-TEST"); + + assertThat(subject.getInvalidReferences(maintainer2).keySet(), hasSize(0)); + } + + @Test + public void is_referenced() { + final RpslObject referenced = RpslObject.parse("person:person\nnic-hdl:NIC3-TEST"); + databaseHelper.addObject(referenced); + + final RpslObject mntRpsl = RpslObject.parse("mntner:MNT-TEST\nmnt-by:MNT-TEST\nadmin-c:NIC3-TEST"); + databaseHelper.addObject(mntRpsl); + + assertThat(subject.isReferenced(referenced), is(true)); + } + + @Test + public void is_not_referenced() { + final RpslObject person2 = RpslObject.parse("person: person\nnic-hdl:NIC5-TEST"); + databaseHelper.addObject(person2); + + assertThat(subject.isReferenced(person2), is(false)); + } + + @Test + public void getOrgAttributeReference() { + final RpslObject object = databaseHelper.addObject(RpslObject.parse("organisation: ORG-TEST")); + + final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\norg:ORG-TEST\nnetname: TEST-RIPE"); + databaseHelper.addObject(inet); + + final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.ORG, ciString("ORG-TEST")); + assertThat(attributeReference.getObjectId(), is(object.getObjectId())); + } + + @Test + public void getSponsoringOrgReference() { + final RpslObject object = databaseHelper.addObject(RpslObject.parse("organisation: ORG-TEST")); + + final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\nsponsoring-org:ORG-TEST\nnetname: TEST-RIPE"); + databaseHelper.addObject(inet); + + final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.SPONSORING_ORG, ciString("ORG-TEST")); + assertThat(attributeReference.getObjectId(), is(object.getObjectId())); + } + + @Test + public void getReferences() { + final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); + databaseHelper.addObject(role); + + final RpslObject org = RpslObject.parse("organisation: ORG-TEST\nabuse-c: NIC-TEST"); + databaseHelper.addObject(org); + + final Set roleReferences = subject.getReferences(role); + + assertThat(roleReferences.size(), is(1)); + assertThat(roleReferences.iterator().next().getKey(), is("ORG-TEST")); + } + + @Test + public void getReferences_none_found() { + final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); + databaseHelper.addObject(role); + + final RpslObject org = RpslObject.parse("organisation: ORG-TEST"); + databaseHelper.addObject(org); + + final Set roleReferences = subject.getReferences(role); + + assertThat(roleReferences.size(), is(0)); + } } diff --git a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDaoCtdIntegrationTest.java b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDaoCtdIntegrationTest.java index e13afc6a43..ecc5c124e7 100644 --- a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDaoCtdIntegrationTest.java +++ b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectUpdateDaoCtdIntegrationTest.java @@ -1,30 +1,14 @@ package net.ripe.db.whois.common.dao.jdbc; - -import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; -import net.ripe.db.whois.common.dao.RpslObjectUpdateInfo; -import net.ripe.db.whois.common.domain.CIString; -import net.ripe.db.whois.common.rpsl.AttributeType; -import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.common.support.AbstractDaoIntegrationTest; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.springframework.beans.factory.annotation.Autowired; -import java.util.Map; -import java.util.Set; - -import static net.ripe.db.whois.common.domain.CIString.ciString; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.MatcherAssert.assertThat; - @org.junit.jupiter.api.Tag("IntegrationTest") public class JdbcRpslObjectUpdateDaoCtdIntegrationTest extends AbstractDaoIntegrationTest { @Autowired RpslObjectUpdateDao subject; @@ -39,102 +23,9 @@ public void cleanup() { sourceContext.removeCurrentSource(); } - @Test - public void invalid_reference() { - final RpslObject maintainer = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC-TEST"); - - final Map> invalidReferences = subject.getInvalidReferences(maintainer); - assertThat(invalidReferences.keySet(), contains(maintainer.findAttribute(AttributeType.ADMIN_C))); - assertThat(invalidReferences.get(maintainer.findAttribute(AttributeType.ADMIN_C)), contains(ciString("NIC-TEST"))); - } - - @Test - public void valid_reference() { - final RpslObject noInvalidRefPerson = RpslObject.parse("person: other person\nnic-hdl:NIC2-TEST"); - - assertThat(subject.getInvalidReferences(noInvalidRefPerson).keySet(), hasSize(0)); - } - - @Test - public void valid_persisted_reference() { - subject.createObject(RpslObject.parse("person: person\nnic-hdl:NIC1-TEST")); - final RpslObject maintainer2 = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC1-TEST"); - - assertThat(subject.getInvalidReferences(maintainer2).keySet(), hasSize(0)); - } - - @Test - public void is_referenced() { - final RpslObject referenced = RpslObject.parse("person:person\nnic-hdl:NIC3-TEST"); - subject.createObject(referenced); - - final RpslObject mntRpsl = RpslObject.parse("mntner:MNT-TEST\nmnt-by:MNT-TEST\nadmin-c:NIC3-TEST"); - subject.createObject(mntRpsl); - - assertThat(subject.isReferenced(referenced), is(true)); - } - - @Test - public void is_not_referenced() { - final RpslObject person2 = RpslObject.parse("person: person\nnic-hdl:NIC5-TEST"); - subject.createObject(person2); - - assertThat(subject.isReferenced(person2), is(false)); - } - @Test public void self_referencing_role_test() { RpslObject selfrefRole = RpslObject.parse("role: some role\nadmin-c:ROLE-NIC\nnic-hdl:ROLE-NIC"); subject.createObject(selfrefRole); } - - @Test - public void getOrgAttributeReference() { - final RpslObjectUpdateInfo object = subject.createObject(RpslObject.parse("organisation: ORG-TEST")); - - final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\norg:ORG-TEST\nnetname: TEST-RIPE"); - subject.createObject(inet); - - final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.ORG, ciString("ORG-TEST")); - assertThat(attributeReference.getObjectId(), is(object.getObjectId())); - } - - @Test - public void getSponsoringOrgReference() { - final RpslObjectUpdateInfo object = subject.createObject(RpslObject.parse("organisation: ORG-TEST")); - - final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\nsponsoring-org:ORG-TEST\nnetname: TEST-RIPE"); - subject.createObject(inet); - - final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.SPONSORING_ORG, ciString("ORG-TEST")); - assertThat(attributeReference.getObjectId(), is(object.getObjectId())); - } - - @Test - public void getReferences() { - final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); - subject.createObject(role); - - final RpslObject org = RpslObject.parse("organisation: ORG-TEST\nabuse-c: NIC-TEST"); - subject.createObject(org); - - final Set roleReferences = subject.getReferences(role); - - assertThat(roleReferences.size(), is(1)); - assertThat(roleReferences.iterator().next().getKey(), is("ORG-TEST")); - } - - @Test - public void getReferences_none_found() { - final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); - subject.createObject(role); - - final RpslObject org = RpslObject.parse("organisation: ORG-TEST"); - subject.createObject(org); - - final Set roleReferences = subject.getReferences(role); - - assertThat(roleReferences.size(), is(0)); - } - } diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java index d4e11d9bad..0f612666ac 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java @@ -1,7 +1,7 @@ package net.ripe.db.whois.update.handler.validator.common; import com.google.common.collect.ImmutableList; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; +import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.update.domain.Action; import net.ripe.db.whois.update.domain.PreparedUpdate; @@ -16,11 +16,11 @@ public class ObjectReferencedValidator implements BusinessRuleValidator { private static final ImmutableList ACTIONS = ImmutableList.of(Action.DELETE); private static final ImmutableList TYPES = ImmutableList.copyOf(ObjectType.values()); - private final RpslObjectUpdateDao rpslObjectUpdateDao; + private final RpslObjectDao rpslObjectDao; @Autowired - public ObjectReferencedValidator(final RpslObjectUpdateDao rpslObjectUpdateDao) { - this.rpslObjectUpdateDao = rpslObjectUpdateDao; + public ObjectReferencedValidator(final RpslObjectDao rpslObjectDao) { + this.rpslObjectDao = rpslObjectDao; } @Override @@ -29,7 +29,7 @@ public void validate(final PreparedUpdate update, final UpdateContext updateCont return; } - if (rpslObjectUpdateDao.isReferenced(update.getReferenceObject())) { + if (rpslObjectDao.isReferenced(update.getReferenceObject())) { updateContext.addMessage(update, UpdateMessages.objectInUse(update.getReferenceObject())); } } diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java index d1fbc9e941..1af234dd70 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java @@ -1,7 +1,7 @@ package net.ripe.db.whois.update.handler.validator.common; import com.google.common.collect.ImmutableList; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; +import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.rpsl.ObjectMessages; import net.ripe.db.whois.common.rpsl.ObjectType; @@ -25,18 +25,18 @@ public class ReferencedObjectsExistValidator implements BusinessRuleValidator { private static final ImmutableList ACTIONS = ImmutableList.of(Action.CREATE, Action.MODIFY); private static final ImmutableList TYPES = ImmutableList.copyOf(ObjectType.values()); - private final RpslObjectUpdateDao rpslObjectUpdateDao; + private final RpslObjectDao rpslObjectDao; @Autowired - public ReferencedObjectsExistValidator(final RpslObjectUpdateDao rpslObjectUpdateDao) { - this.rpslObjectUpdateDao = rpslObjectUpdateDao; + public ReferencedObjectsExistValidator(final RpslObjectDao rpslObjectDao) { + this.rpslObjectDao = rpslObjectDao; } @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); - final Map> invalidReferences = rpslObjectUpdateDao.getInvalidReferences(updatedObject); + final Map> invalidReferences = rpslObjectDao.getInvalidReferences(updatedObject); final ObjectMessages objectMessages = updateContext.getMessages(update); for (final Map.Entry> invalidReferenceEntry : invalidReferences.entrySet()) { final RpslAttribute attribute = invalidReferenceEntry.getKey(); diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java index 820ede49d8..1621936585 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java @@ -72,7 +72,7 @@ public void validate(final PreparedUpdate update, final UpdateContext updateCont } private RpslObject findOrgReference(final RpslAttribute org) { - final RpslObjectInfo referencedOrganisationInfo = rpslObjectUpdateDao.getAttributeReference(org.getType(), org.getCleanValue()); + final RpslObjectInfo referencedOrganisationInfo = rpslObjectDao.getAttributeReference(org.getType(), org.getCleanValue()); return (referencedOrganisationInfo == null ? null : rpslObjectDao.getByKey(ObjectType.ORGANISATION, referencedOrganisationInfo.getKey())); } diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java index bfe29ef844..3819ea06fa 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java @@ -2,7 +2,6 @@ import com.google.common.collect.ImmutableList; import net.ripe.db.whois.common.dao.RpslObjectDao; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.domain.Maintainers; import net.ripe.db.whois.common.rpsl.AttributeType; @@ -36,14 +35,12 @@ public class AbuseValidator implements BusinessRuleValidator { private static final ImmutableList TYPES = ImmutableList.of(ORGANISATION, INETNUM, INET6NUM, AUT_NUM); private final RpslObjectDao objectDao; - private final RpslObjectUpdateDao updateDao; private Maintainers maintainers; @Autowired - public AbuseValidator(final RpslObjectDao objectDao, final RpslObjectUpdateDao updateDao, final Maintainers maintainers) { + public AbuseValidator(final RpslObjectDao objectDao, final Maintainers maintainers) { this.objectDao = objectDao; this.maintainers = maintainers; - this.updateDao = updateDao; } @Override @@ -106,7 +103,7 @@ private boolean isAbuseCRemoved(final RpslObject updatedObject, final PreparedUp } private boolean isOrgReferencedByRsMaintainedResources(final RpslObject updatedObject) { - return updateDao.getReferences(updatedObject) + return objectDao.getReferences(updatedObject) .stream() .filter(Objects::nonNull) .filter(rpslObjectInfo -> ObjectType.RESOURCE_TYPES.contains(rpslObjectInfo.getObjectType())) diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java index f2f897c194..73a41c1c77 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java @@ -4,12 +4,9 @@ import com.google.common.collect.Sets; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.domain.Maintainers; import net.ripe.db.whois.common.rpsl.AttributeType; -import static net.ripe.db.whois.common.rpsl.AttributeType.ORG_NAME; -import static net.ripe.db.whois.common.rpsl.AttributeType.COUNTRY; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.common.rpsl.attrs.OrgType; @@ -24,9 +21,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.Objects; import java.util.Set; +import static net.ripe.db.whois.common.rpsl.AttributeType.COUNTRY; +import static net.ripe.db.whois.common.rpsl.AttributeType.ORG_NAME; + @Component public class OrgNameAndCountryAttrValidator implements BusinessRuleValidator { @@ -35,13 +34,11 @@ public class OrgNameAndCountryAttrValidator implements BusinessRuleValidator { private static final Set RESOURCE_OBJECT_TYPES = Sets.newHashSet(ObjectType.AUT_NUM, ObjectType.INETNUM, ObjectType.INET6NUM); - private final RpslObjectUpdateDao objectUpdateDao; private final RpslObjectDao objectDao; private final Maintainers maintainers; @Autowired - public OrgNameAndCountryAttrValidator(final RpslObjectUpdateDao objectUpdateDao, final RpslObjectDao objectDao, final Maintainers maintainers) { - this.objectUpdateDao = objectUpdateDao; + public OrgNameAndCountryAttrValidator(final RpslObjectDao objectDao, final Maintainers maintainers) { this.objectDao = objectDao; this.maintainers = maintainers; } @@ -83,7 +80,7 @@ private boolean isLir(final RpslObject organisation) { } private boolean isReferencedByRsMaintainedResource(final RpslObject rpslObject) { - for (RpslObjectInfo referencedObjectInfo : objectUpdateDao.getReferences(rpslObject)) { + for (RpslObjectInfo referencedObjectInfo : objectDao.getReferences(rpslObject)) { if (RESOURCE_OBJECT_TYPES.contains(referencedObjectInfo.getObjectType())) { final RpslObject referencedObject = objectDao.getById(referencedObjectInfo.getObjectId()); if (isMaintainedByRs(referencedObject)) { diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java index 2e7f97aec4..38c3d1f95b 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java @@ -4,7 +4,6 @@ import com.google.common.collect.ImmutableSet; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; @@ -25,15 +24,16 @@ public class MustKeepAbuseMailboxIfReferencedValidator implements BusinessRuleVa private static final ImmutableList TYPES = ImmutableList.of(ObjectType.ROLE); private static final Set REFERENCED_OBJECT_TYPES = ImmutableSet.of( - ObjectType.ORGANISATION, ObjectType.INETNUM, ObjectType.INET6NUM, ObjectType.AUT_NUM + ObjectType.ORGANISATION, + ObjectType.INETNUM, + ObjectType.INET6NUM, + ObjectType.AUT_NUM ); - private final RpslObjectUpdateDao updateObjectDao; private final RpslObjectDao objectDao; @Autowired - public MustKeepAbuseMailboxIfReferencedValidator(final RpslObjectUpdateDao updateObjectDao, final RpslObjectDao objectDao) { - this.updateObjectDao = updateObjectDao; + public MustKeepAbuseMailboxIfReferencedValidator(final RpslObjectDao objectDao) { this.objectDao = objectDao; } @@ -46,7 +46,7 @@ public void validate(final PreparedUpdate update, final UpdateContext updateCont return; } - for (final RpslObjectInfo referenceInfo : updateObjectDao.getReferences(update.getUpdatedObject())) { + for (final RpslObjectInfo referenceInfo : objectDao.getReferences(update.getUpdatedObject())) { if (REFERENCED_OBJECT_TYPES.contains(referenceInfo.getObjectType())) { final Set abuseCAttributes = objectDao.getById(referenceInfo.getObjectId()).getValuesForAttribute(AttributeType.ABUSE_C); if (!abuseCAttributes.isEmpty() && abuseCAttributes.contains(update.getUpdatedObject().getValueForAttribute(AttributeType.NIC_HDL))) { diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java index c9a3f67757..187a7adac0 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java @@ -1,6 +1,6 @@ package net.ripe.db.whois.update.handler.validator.common; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; +import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.update.domain.Action; @@ -24,7 +24,7 @@ public class ObjectReferencedValidatorTest { @Mock PreparedUpdate update; @Mock UpdateContext updateContext; - @Mock RpslObjectUpdateDao rpslObjectUpdateDao; + @Mock RpslObjectDao rpslObjectDao; @InjectMocks ObjectReferencedValidator subject; @Test @@ -54,7 +54,7 @@ public void validate_referenced() { when(update.getType()).thenReturn(ObjectType.MNTNER); when(update.hasOriginalObject()).thenReturn(true); when(update.getReferenceObject()).thenReturn(object); - when(rpslObjectUpdateDao.isReferenced(object)).thenReturn(true); + when(rpslObjectDao.isReferenced(object)).thenReturn(true); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.objectInUse(object)); @@ -62,7 +62,7 @@ public void validate_referenced() { @Test public void validate_referenced_autnum() { - final RpslObject object = RpslObject.parse("aut-num: AS1"); + final RpslObject object = RpslObject.parse("aut-num: AS1"); // TODO: unused? when(update.getType()).thenReturn(ObjectType.AUT_NUM); when(update.hasOriginalObject()).thenReturn(true); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java index 0ffbc28386..3dad63cae0 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java @@ -1,7 +1,7 @@ package net.ripe.db.whois.update.handler.validator.common; import com.google.common.collect.Maps; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; +import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.rpsl.ObjectMessages; import net.ripe.db.whois.common.rpsl.RpslAttribute; @@ -32,7 +32,7 @@ public class ReferencedObjectsExistValidatorTest { @Mock PreparedUpdate update; @Mock UpdateContext updateContext; - @Mock RpslObjectUpdateDao rpslObjectUpdateDao; + @Mock RpslObjectDao rpslObjectDao; @InjectMocks ReferencedObjectsExistValidator subject; private RpslObject object; @@ -50,7 +50,7 @@ public void getActions() { @Test public void validate_no_invalid_references() { when(update.getUpdatedObject()).thenReturn(object); - when(rpslObjectUpdateDao.getInvalidReferences(object)).thenReturn(Collections.>emptyMap()); + when(rpslObjectDao.getInvalidReferences(object)).thenReturn(Collections.>emptyMap()); subject.validate(update, updateContext); verify(updateContext).getMessages(update); @@ -68,7 +68,7 @@ public void validate_invalid_references() { invalidReferences.put(invalidAttribute, invalidAttribute.getCleanValues()); when(updateContext.getMessages(update)).thenReturn(new ObjectMessages()); - when(rpslObjectUpdateDao.getInvalidReferences(object)).thenReturn(invalidReferences); + when(rpslObjectDao.getInvalidReferences(object)).thenReturn(invalidReferences); subject.validate(update, updateContext); verify(updateContext).addMessage(update, invalidAttribute, UpdateMessages.unknownObjectReferenced("ADMIN_NC")); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java index cc82cdea5f..882c5d7006 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java @@ -2,7 +2,6 @@ import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.common.rpsl.RpslObject; @@ -33,7 +32,6 @@ public class ReferenceCheckTest { @Mock private PreparedUpdate update; @Mock private UpdateContext updateContext; - @Mock private RpslObjectUpdateDao rpslObjectUpdateDao; @Mock private RpslObjectDao rpslObjectDao; @Mock private RpslObjectInfo rpslObjectInfo; @Mock Subject updateSubject; @@ -69,7 +67,7 @@ public void modify_org_reference_missing_override() { @Test public void modify_org_reference_not_found_in_db() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); subject.validate(update, updateContext); @@ -89,7 +87,7 @@ public void modify_org_reference_not_found_in_db_override() { public void modify_wrong_orgtype_on_found_org() { lenient().when(update.getAction()).thenReturn(Action.MODIFY); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: OTHER")); @@ -111,7 +109,7 @@ public void modify_wrong_orgtype_on_found_org_override() { @Test public void modify_right_orgtype_on_found_org() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: LIR")); @@ -143,7 +141,7 @@ public void create_org_reference_missing_override() { public void create_org_reference_not_found_in_db() { lenient().when(update.getAction()).thenReturn(Action.CREATE); lenient().when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); subject.validate(update, updateContext); @@ -163,7 +161,7 @@ public void create_org_reference_not_found_in_db_override() { @Test public void create_wrong_orgtype_on_found_org() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: OTHER")); @@ -175,7 +173,7 @@ public void create_wrong_orgtype_on_found_org() { @Test public void create_wrong_orgtype_on_found_org_direct_assignment() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); lenient().when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: DIRECT_ASSIGNMENT")); @@ -197,7 +195,7 @@ public void create_wrong_orgtype_on_found_org_override() { @Test public void create_right_orgtype_on_found_org() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectUpdateDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: LIR")); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java index 52c14f1f93..79ef629ba8 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java @@ -129,7 +129,7 @@ public void remove_abuse_contact_LIR_org() { public void allow_remove_abuse_contact_when_non_LIR_no_referencing_objects() { when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(updateDao.getReferences(update.getUpdatedObject())).thenReturn(Collections.EMPTY_SET); + when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Collections.EMPTY_SET); subject.validate(update, updateContext); @@ -143,7 +143,7 @@ public void allow_remove_abusec_when_referencing_object_is_not_resource() { final RpslObjectInfo info = new RpslObjectInfo(1, ObjectType.PERSON, "AS6"); when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(updateDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); + when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); subject.validate(update, updateContext); @@ -156,7 +156,7 @@ public void allow_remove_abuse_contact_when_referencing_resources_is_not_rs_main final RpslObjectInfo info = new RpslObjectInfo(1, ObjectType.AUT_NUM, "AS6"); when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(updateDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); + when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); when(objectDao.getById(info.getObjectId())).thenReturn(RESOURCE_NOT_RS_MAINTAINED); subject.validate(update, updateContext); @@ -173,7 +173,7 @@ public void do_not_allow_remove_abuse_contact_when_a_referencing_resource_is_rs_ final RpslObjectInfo info = new RpslObjectInfo(1, ObjectType.AUT_NUM, "AS6"); when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(updateDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); + when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); when(objectDao.getById(info.getObjectId())).thenReturn(RESOURCE_RS_MAINTAINED); subject.validate(update, updateContext); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java index 2d095fe865..5933efc5b9 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java @@ -3,7 +3,6 @@ import com.google.common.collect.Sets; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.domain.Maintainers; import net.ripe.db.whois.common.rpsl.AttributeType; @@ -42,7 +41,6 @@ public class OrgNameAndCountryAttrValidatorTest { @Mock private UpdateContext updateContext; @Mock private PreparedUpdate update; @Mock private Subject subjectObject; - @Mock private RpslObjectUpdateDao updateDao; @Mock private RpslObjectDao objectDao; @Mock private Maintainers maintainers; @InjectMocks private OrgNameAndCountryAttrValidator subject; @@ -129,7 +127,7 @@ public void orgname_not_changed() { public void orgname_changed_not_referenced_at_all() { when(update.getReferenceObject()).thenReturn(ORIGINAL_ORG); when(update.getUpdatedObject()).thenReturn(UPDATED_ORG_NEW_NAME); - when(updateDao.getReferences(ORIGINAL_ORG)).thenReturn(Collections.EMPTY_SET); + when(objectDao.getReferences(ORIGINAL_ORG)).thenReturn(Collections.EMPTY_SET); subject.validate(update, updateContext); @@ -157,7 +155,7 @@ public void orgname_changed_for_lir() { public void orgname_changed_not_referenced_by_resource() { when(update.getReferenceObject()).thenReturn(ORIGINAL_ORG); when(update.getUpdatedObject()).thenReturn(UPDATED_ORG_NEW_NAME); - when(updateDao.getReferences(ORIGINAL_ORG)).thenReturn(Sets.newHashSet(new RpslObjectInfo(5, ObjectType.PERSON, "TEST-NIC"))); + when(objectDao.getReferences(ORIGINAL_ORG)).thenReturn(Sets.newHashSet(new RpslObjectInfo(5, ObjectType.PERSON, "TEST-NIC"))); subject.validate(update, updateContext); @@ -331,6 +329,6 @@ private void presetReferrers(RpslObject... referrerObjects) { lenient().when(objectDao.getById(referrerObject.getObjectId())).thenReturn(referrerObject); } - lenient().when(updateDao.getReferences(ORIGINAL_ORG)).thenReturn(rpslObjectInfos); + lenient().when(objectDao.getReferences(ORIGINAL_ORG)).thenReturn(rpslObjectInfos); } } diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java index 8aefdcb2ad..dccd6581e4 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java @@ -60,7 +60,7 @@ public void remove_unreferenced_abuse_mailbox() { when(update.getReferenceObject()).thenReturn(originalObject); when(update.getUpdatedObject()).thenReturn(updatedObject); - when(updateDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet()); + when(objectDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet()); subject.validate(update, updateContext); verifyNoMoreInteractions(updateContext); @@ -86,7 +86,7 @@ public void remove_referenced_abuse_mailbox() { when(update.getReferenceObject()).thenReturn(originalObject); when(update.getUpdatedObject()).thenReturn(updatedObject); - when(updateDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet(new RpslObjectInfo(1, ObjectType.ORGANISATION, "ORG-TEST1"))); + when(objectDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet(new RpslObjectInfo(1, ObjectType.ORGANISATION, "ORG-TEST1"))); when(objectDao.getById(1)).thenReturn(RpslObject.parse("organisation: ORG-TEST1\nabuse-c: TEST-NIC")); subject.validate(update, updateContext); From 6770ebf7eea6cde5e4cc7ef1b31e6cedca5dd755 Mon Sep 17 00:00:00 2001 From: Ed Shryane Date: Thu, 21 Jul 2022 16:35:10 +0200 Subject: [PATCH 2/6] Fixed test (references are never checked for aut-num objects, if that ever changes the test should fail as there are references (which are not called so use lenient())). --- .../validator/common/ObjectReferencedValidatorTest.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java index 187a7adac0..bc8e749707 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java @@ -15,6 +15,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -62,10 +63,12 @@ public void validate_referenced() { @Test public void validate_referenced_autnum() { - final RpslObject object = RpslObject.parse("aut-num: AS1"); // TODO: unused? + final RpslObject object = RpslObject.parse("aut-num: AS1"); when(update.getType()).thenReturn(ObjectType.AUT_NUM); when(update.hasOriginalObject()).thenReturn(true); + lenient().when(update.getReferenceObject()).thenReturn(object); + lenient().when(rpslObjectDao.isReferenced(object)).thenReturn(true); subject.validate(update, updateContext); From 367e8cda9f8d0eaf77f38df4c14d16d2f39d964f Mon Sep 17 00:00:00 2001 From: Ed Shryane Date: Fri, 22 Jul 2022 09:50:40 +0200 Subject: [PATCH 3/6] Created separate class for reference DAO methods --- .../db/whois/api/rest/ReferencesService.java | 9 +- .../db/whois/common/dao/ReferencesDao.java | 23 +++ .../db/whois/common/dao/RpslObjectDao.java | 12 -- .../common/dao/jdbc/JdbcReferencesDao.java | 156 ++++++++++++++++++ .../common/dao/jdbc/JdbcRpslObjectDao.java | 116 ------------- .../JdbcReferencesDaoIntegrationTest.java | 133 +++++++++++++++ .../JdbcRpslObjectDaoIntegrationTest.java | 97 ----------- .../common/ObjectReferencedValidator.java | 10 +- .../ReferencedObjectsExistValidator.java | 10 +- .../validator/inetnum/ReferenceCheck.java | 10 +- .../organisation/AbuseValidator.java | 7 +- .../OrgNameAndCountryAttrValidator.java | 7 +- ...KeepAbuseMailboxIfReferencedValidator.java | 7 +- .../common/ObjectReferencedValidatorTest.java | 13 +- .../ReferencedObjectsExistValidatorTest.java | 8 +- .../validator/inetnum/ReferenceCheckTest.java | 16 +- .../organisation/AbuseValidatorTest.java | 12 +- .../OrgNameAndCountryAttrValidatorTest.java | 8 +- ...AbuseMailboxIfReferencedValidatorTest.java | 13 +- 19 files changed, 387 insertions(+), 280 deletions(-) create mode 100644 whois-commons/src/main/java/net/ripe/db/whois/common/dao/ReferencesDao.java create mode 100644 whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java create mode 100644 whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDaoIntegrationTest.java diff --git a/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java b/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java index fdd460051f..ffc516fa06 100644 --- a/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java +++ b/whois-api/src/main/java/net/ripe/db/whois/api/rest/ReferencesService.java @@ -18,6 +18,7 @@ import net.ripe.db.whois.api.rest.marshal.StreamingHelper; import net.ripe.db.whois.common.Message; import net.ripe.db.whois.common.Messages; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.rpsl.AttributeTemplate; @@ -94,6 +95,7 @@ public class ReferencesService { private static final Logger LOGGER = LoggerFactory.getLogger(ReferencesService.class); private final RpslObjectDao rpslObjectDao; + private final ReferencesDao referencesDao; private final SourceContext sourceContext; private final InternalUpdatePerformer updatePerformer; private final SsoTranslator ssoTranslator; @@ -104,14 +106,15 @@ public class ReferencesService { @Autowired public ReferencesService( @Qualifier("jdbcRpslObjectSlaveDao") final RpslObjectDao rpslObjectDao, + final ReferencesDao referencesDao, final SourceContext sourceContext, final InternalUpdatePerformer updatePerformer, final SsoTranslator ssoTranslator, final LoggerContext loggerContext, final WhoisObjectMapper whoisObjectMapper, final @Value("#{${whois.dummy}}") Map dummyMap) { - this.rpslObjectDao = rpslObjectDao; + this.referencesDao = referencesDao; this.sourceContext = sourceContext; this.updatePerformer = updatePerformer; this.ssoTranslator = ssoTranslator; @@ -569,7 +572,7 @@ private void validateReferences(final RpslObject primaryObject, final Map entry : references.entrySet()) { final RpslObject reference = entry.getValue(); - for (final RpslObjectInfo referenceToReference : rpslObjectDao.getReferences(reference)) { + for (final RpslObjectInfo referenceToReference : referencesDao.getReferences(reference)) { if (!referenceMatches(referenceToReference, primaryObject) && !references.keySet().contains(referenceToReference)) { throw new IllegalArgumentException("Referencing object " + reference.getKey() + " itself is referenced by " + referenceToReference.getKey()); } @@ -715,7 +718,7 @@ private RpslObject lookupObjectByKey(final String primaryKey, final ObjectType o private Map findReferences(final RpslObject rpslObject) { final Map references = Maps.newHashMap(); try { - for (final RpslObjectInfo rpslObjectInfo : rpslObjectDao.getReferences(rpslObject)) { + for (final RpslObjectInfo rpslObjectInfo : referencesDao.getReferences(rpslObject)) { references.put(rpslObjectInfo, rpslObjectDao.getById(rpslObjectInfo.getObjectId())); } } catch (EmptyResultDataAccessException e) { diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/ReferencesDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/ReferencesDao.java new file mode 100644 index 0000000000..8ffbc83966 --- /dev/null +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/ReferencesDao.java @@ -0,0 +1,23 @@ +package net.ripe.db.whois.common.dao; + +import net.ripe.db.whois.common.domain.CIString; +import net.ripe.db.whois.common.rpsl.AttributeType; +import net.ripe.db.whois.common.rpsl.RpslAttribute; +import net.ripe.db.whois.common.rpsl.RpslObject; + +import javax.annotation.CheckForNull; +import java.util.Map; +import java.util.Set; + +public interface ReferencesDao { + + boolean isReferenced(RpslObject object); + + Set getReferences(RpslObject object); + + Map> getInvalidReferences(RpslObject object); + + @CheckForNull + RpslObjectInfo getAttributeReference(AttributeType attributeType, CIString keyValue); + +} diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java index aaed6f7a85..0fc45ac89c 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/RpslObjectDao.java @@ -5,15 +5,12 @@ import net.ripe.db.whois.common.domain.Identifiable; import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; -import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.time.LocalDateTime; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Set; // these should return Collection<> instead of List<> to allow for greater flexibility in implementation @@ -53,13 +50,4 @@ public interface RpslObjectDao extends ProxyLoader { List findMemberOfByObjectTypeWithoutMbrsByRef(ObjectType objectType, String attributeValue); Collection relatedTo(RpslObject identifiable, Set excludeObjectTypes); - - boolean isReferenced(RpslObject object); - - Set getReferences(RpslObject object); - - Map> getInvalidReferences(RpslObject object); - - @CheckForNull - RpslObjectInfo getAttributeReference(AttributeType attributeType, CIString keyValue); } diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java new file mode 100644 index 0000000000..c318f3d033 --- /dev/null +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java @@ -0,0 +1,156 @@ +package net.ripe.db.whois.common.dao.jdbc; + +import com.google.common.collect.Maps; +import com.google.common.collect.Sets; +import net.ripe.db.whois.common.aspects.RetryFor; +import net.ripe.db.whois.common.collect.CollectionHelper; +import net.ripe.db.whois.common.dao.ReferencesDao; +import net.ripe.db.whois.common.dao.RpslObjectInfo; +import net.ripe.db.whois.common.dao.jdbc.index.IndexStrategies; +import net.ripe.db.whois.common.dao.jdbc.index.IndexStrategy; +import net.ripe.db.whois.common.domain.CIString; +import net.ripe.db.whois.common.rpsl.AttributeType; +import net.ripe.db.whois.common.rpsl.ObjectTemplate; +import net.ripe.db.whois.common.rpsl.ObjectType; +import net.ripe.db.whois.common.rpsl.RpslAttribute; +import net.ripe.db.whois.common.rpsl.RpslObject; +import org.apache.commons.lang.Validate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.dao.RecoverableDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +import javax.annotation.CheckForNull; +import javax.sql.DataSource; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static net.ripe.db.whois.common.domain.CIString.ciString; + +@Repository +@RetryFor(RecoverableDataAccessException.class) +public class JdbcReferencesDao implements ReferencesDao { + + private final JdbcTemplate jdbcTemplate; + + @Autowired + public JdbcReferencesDao(@Qualifier("sourceAwareDataSource") final DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + @Override + public boolean isReferenced(final RpslObject object) { + for (final RpslAttribute attribute : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { + for (final IndexStrategy indexStrategy : IndexStrategies.getReferencing(object.getType())) { + for (final CIString value : attribute.getReferenceValues()) { + for (final RpslObjectInfo result : indexStrategy.findInIndex(jdbcTemplate, value)) { + if (object.getKey().equals(ciString(result.getKey())) && result.getObjectType().equals(object.getType())) { + continue; + } + + return true; + } + } + } + } + return false; + } + + @Override + public Set getReferences(final RpslObject object) { + Set references = Sets.newHashSet(); + final List indexStrategies = IndexStrategies.getReferencing(object.getType()); + + // for route(6), individually check each key + for (final RpslAttribute keyAttr : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { + for (final IndexStrategy indexStrategy : indexStrategies) { + for (final CIString value : keyAttr.getReferenceValues()) { + final List results = indexStrategy.findInIndex(jdbcTemplate, value); + for (final RpslObjectInfo result : results) { + if (object.getKey().equals(result.getKey()) && result.getObjectType().equals(object.getType())) { + continue; + } + + references.add(result); + } + } + } + } + + return references; + } + + @Override + public Map> getInvalidReferences(final RpslObject object) { + final Map> invalidReferenceMap = Maps.newHashMap(); + + for (final RpslAttribute attribute : object.getAttributes()) { + final Set invalidReferenceValues = getInvalidReferences(object, attribute); + if (!invalidReferenceValues.isEmpty()) { + invalidReferenceMap.put(attribute, invalidReferenceValues); + } + } + + return invalidReferenceMap; + } + + private Set getInvalidReferences(final RpslObject object, final RpslAttribute attribute) { + AttributeType attributeType = attribute.getType(); + if (attributeType != null && attributeType.getReferences().isEmpty()) { + return Collections.emptySet(); + } + + final Set invalidReferences = Sets.newLinkedHashSet(); + for (final CIString value : attribute.getReferenceValues()) { + if (isInvalidReference(object, attributeType, value)) { + invalidReferences.add(value); + } + } + + return invalidReferences; + } + + private boolean isInvalidReference(final RpslObject object, final AttributeType attributeType, final CIString referenceValue) { + final Set references = attributeType.getReferences(referenceValue); + if (references.isEmpty()) { + return false; + } + + for (final ObjectType reference : references) { + if (reference.equals(object.getType()) && object.getKey().equals(referenceValue)) { + return false; + } + + if (getAttributeReference(reference, referenceValue) != null) { + return false; + } + } + + return true; + } + + @CheckForNull + public RpslObjectInfo getAttributeReference(final AttributeType attributeType, final CIString value) { + final CIString referenceValue = new RpslAttribute(attributeType, value.toString()).getReferenceValue(); + for (final ObjectType objectType : attributeType.getReferences()) { + final RpslObjectInfo result = getAttributeReference(objectType, referenceValue); + if (result != null) { + return result; + } + } + + return null; + } + + private RpslObjectInfo getAttributeReference(final ObjectType objectType, final CIString keyValue) { + final ObjectTemplate referenceTemplate = ObjectTemplate.getTemplate(objectType); + final Set referenceKeyAttributes = referenceTemplate.getKeyAttributes(); + Validate.isTrue(referenceKeyAttributes.size() == 1, "We can never have a reference to a composed key"); + final IndexStrategy indexStrategy = IndexStrategies.get(referenceKeyAttributes.iterator().next()); + final List result = indexStrategy.findInIndex(jdbcTemplate, keyValue); + return CollectionHelper.uniqueResult(result); + } +} diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java index 4e3ec5c931..dc822f0d53 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDao.java @@ -5,7 +5,6 @@ import com.google.common.collect.Maps; import com.google.common.collect.Sets; import net.ripe.db.whois.common.aspects.RetryFor; -import net.ripe.db.whois.common.collect.CollectionHelper; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.dao.jdbc.domain.RpslObjectInfoResultSetExtractor; @@ -53,8 +52,6 @@ import java.util.Map; import java.util.Set; -import static net.ripe.db.whois.common.domain.CIString.ciString; - @Repository @Primary @RetryFor(RecoverableDataAccessException.class) @@ -350,117 +347,4 @@ public Collection relatedTo(final RpslObject identifiable, final return result; } - - @Override - public boolean isReferenced(final RpslObject object) { - for (final RpslAttribute attribute : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { - for (final IndexStrategy indexStrategy : IndexStrategies.getReferencing(object.getType())) { - for (final CIString value : attribute.getReferenceValues()) { - for (final RpslObjectInfo result : indexStrategy.findInIndex(jdbcTemplate, value)) { - if (object.getKey().equals(ciString(result.getKey())) && result.getObjectType().equals(object.getType())) { - continue; - } - - return true; - } - } - } - } - return false; - } - - @Override - public Set getReferences(final RpslObject object) { - Set references = Sets.newHashSet(); - final List indexStrategies = IndexStrategies.getReferencing(object.getType()); - - // for route(6), individually check each key - for (final RpslAttribute keyAttr : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { - for (final IndexStrategy indexStrategy : indexStrategies) { - for (final CIString value : keyAttr.getReferenceValues()) { - final List results = indexStrategy.findInIndex(jdbcTemplate, value); - for (final RpslObjectInfo result : results) { - if (object.getKey().equals(result.getKey()) && result.getObjectType().equals(object.getType())) { - continue; - } - - references.add(result); - } - } - } - } - - return references; - } - @Override - public Map> getInvalidReferences(final RpslObject object) { - final Map> invalidReferenceMap = Maps.newHashMap(); - - for (final RpslAttribute attribute : object.getAttributes()) { - final Set invalidReferenceValues = getInvalidReferences(object, attribute); - if (!invalidReferenceValues.isEmpty()) { - invalidReferenceMap.put(attribute, invalidReferenceValues); - } - } - - return invalidReferenceMap; - } - - private Set getInvalidReferences(final RpslObject object, final RpslAttribute attribute) { - AttributeType attributeType = attribute.getType(); - if (attributeType != null && attributeType.getReferences().isEmpty()) { - return Collections.emptySet(); - } - - final Set invalidReferences = Sets.newLinkedHashSet(); - for (final CIString value : attribute.getReferenceValues()) { - if (isInvalidReference(object, attributeType, value)) { - invalidReferences.add(value); - } - } - - return invalidReferences; - } - - private boolean isInvalidReference(final RpslObject object, final AttributeType attributeType, final CIString referenceValue) { - final Set references = attributeType.getReferences(referenceValue); - if (references.isEmpty()) { - return false; - } - - for (final ObjectType reference : references) { - if (reference.equals(object.getType()) && object.getKey().equals(referenceValue)) { - return false; - } - - if (getAttributeReference(reference, referenceValue) != null) { - return false; - } - } - - return true; - } - - @CheckForNull - public RpslObjectInfo getAttributeReference(final AttributeType attributeType, final CIString value) { - final CIString referenceValue = new RpslAttribute(attributeType, value.toString()).getReferenceValue(); - for (final ObjectType objectType : attributeType.getReferences()) { - final RpslObjectInfo result = getAttributeReference(objectType, referenceValue); - if (result != null) { - return result; - } - } - - return null; - } - - private RpslObjectInfo getAttributeReference(final ObjectType objectType, final CIString keyValue) { - final ObjectTemplate referenceTemplate = ObjectTemplate.getTemplate(objectType); - final Set referenceKeyAttributes = referenceTemplate.getKeyAttributes(); - Validate.isTrue(referenceKeyAttributes.size() == 1, "We can never have a reference to a composed key"); - final IndexStrategy indexStrategy = IndexStrategies.get(referenceKeyAttributes.iterator().next()); - final List result = indexStrategy.findInIndex(jdbcTemplate, keyValue); - return CollectionHelper.uniqueResult(result); - } - } diff --git a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDaoIntegrationTest.java b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDaoIntegrationTest.java new file mode 100644 index 0000000000..5777dd3608 --- /dev/null +++ b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDaoIntegrationTest.java @@ -0,0 +1,133 @@ +package net.ripe.db.whois.common.dao.jdbc; + +import net.ripe.db.whois.common.dao.ReferencesDao; +import net.ripe.db.whois.common.dao.RpslObjectInfo; +import net.ripe.db.whois.common.domain.CIString; +import net.ripe.db.whois.common.rpsl.AttributeType; +import net.ripe.db.whois.common.rpsl.RpslAttribute; +import net.ripe.db.whois.common.rpsl.RpslObject; +import net.ripe.db.whois.common.source.Source; +import net.ripe.db.whois.common.support.AbstractDaoIntegrationTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; + +import java.util.Map; +import java.util.Set; + +import static net.ripe.db.whois.common.domain.CIString.ciString; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + +@org.junit.jupiter.api.Tag("IntegrationTest") +public class JdbcReferencesDaoIntegrationTest extends AbstractDaoIntegrationTest { + + @Autowired ReferencesDao subject; + @Value("${whois.source}") protected String source; + + @BeforeEach + public void setup() { + sourceContext.setCurrent(Source.slave(source)); + } + + @AfterEach + public void cleanup() { + sourceContext.removeCurrentSource(); + } + + @Test + public void invalid_reference() { + final RpslObject maintainer = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC-TEST"); + + final Map> invalidReferences = subject.getInvalidReferences(maintainer); + assertThat(invalidReferences.keySet(), contains(maintainer.findAttribute(AttributeType.ADMIN_C))); + assertThat(invalidReferences.get(maintainer.findAttribute(AttributeType.ADMIN_C)), contains(ciString("NIC-TEST"))); + } + + @Test + public void valid_reference() { + final RpslObject noInvalidRefPerson = RpslObject.parse("person: other person\nnic-hdl:NIC2-TEST"); + + assertThat(subject.getInvalidReferences(noInvalidRefPerson).keySet(), hasSize(0)); + } + + @Test + public void valid_persisted_reference() { + databaseHelper.addObject(RpslObject.parse("person: person\nnic-hdl:NIC1-TEST")); + final RpslObject maintainer2 = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC1-TEST"); + + assertThat(subject.getInvalidReferences(maintainer2).keySet(), hasSize(0)); + } + + @Test + public void is_referenced() { + final RpslObject referenced = RpslObject.parse("person:person\nnic-hdl:NIC3-TEST"); + databaseHelper.addObject(referenced); + + final RpslObject mntRpsl = RpslObject.parse("mntner:MNT-TEST\nmnt-by:MNT-TEST\nadmin-c:NIC3-TEST"); + databaseHelper.addObject(mntRpsl); + + assertThat(subject.isReferenced(referenced), is(true)); + } + + @Test + public void is_not_referenced() { + final RpslObject person2 = RpslObject.parse("person: person\nnic-hdl:NIC5-TEST"); + databaseHelper.addObject(person2); + + assertThat(subject.isReferenced(person2), is(false)); + } + + @Test + public void getOrgAttributeReference() { + final RpslObject object = databaseHelper.addObject(RpslObject.parse("organisation: ORG-TEST")); + + final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\norg:ORG-TEST\nnetname: TEST-RIPE"); + databaseHelper.addObject(inet); + + final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.ORG, ciString("ORG-TEST")); + assertThat(attributeReference.getObjectId(), is(object.getObjectId())); + } + + @Test + public void getSponsoringOrgReference() { + final RpslObject object = databaseHelper.addObject(RpslObject.parse("organisation: ORG-TEST")); + + final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\nsponsoring-org:ORG-TEST\nnetname: TEST-RIPE"); + databaseHelper.addObject(inet); + + final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.SPONSORING_ORG, ciString("ORG-TEST")); + assertThat(attributeReference.getObjectId(), is(object.getObjectId())); + } + + @Test + public void getReferences() { + final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); + databaseHelper.addObject(role); + + final RpslObject org = RpslObject.parse("organisation: ORG-TEST\nabuse-c: NIC-TEST"); + databaseHelper.addObject(org); + + final Set roleReferences = subject.getReferences(role); + + assertThat(roleReferences.size(), is(1)); + assertThat(roleReferences.iterator().next().getKey(), is("ORG-TEST")); + } + + @Test + public void getReferences_none_found() { + final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); + databaseHelper.addObject(role); + + final RpslObject org = RpslObject.parse("organisation: ORG-TEST"); + databaseHelper.addObject(org); + + final Set roleReferences = subject.getReferences(role); + + assertThat(roleReferences.size(), is(0)); + } +} diff --git a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java index e93a7a1193..9d13d62f6f 100644 --- a/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java +++ b/whois-commons/src/test/java/net/ripe/db/whois/common/dao/jdbc/JdbcRpslObjectDaoIntegrationTest.java @@ -4,9 +4,7 @@ import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.domain.CIString; -import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; -import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.common.source.Source; import net.ripe.db.whois.common.support.AbstractDaoIntegrationTest; @@ -21,11 +19,8 @@ import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Map; -import java.util.Set; import static net.ripe.db.whois.common.domain.CIString.ciSet; -import static net.ripe.db.whois.common.domain.CIString.ciString; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.hasSize; @@ -423,96 +418,4 @@ public void getById() { final RpslObject object = subject.getById(1); assertThat(object, is(rpslObject)); } - - @Test - public void invalid_reference() { - final RpslObject maintainer = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC-TEST"); - - final Map> invalidReferences = subject.getInvalidReferences(maintainer); - assertThat(invalidReferences.keySet(), contains(maintainer.findAttribute(AttributeType.ADMIN_C))); - assertThat(invalidReferences.get(maintainer.findAttribute(AttributeType.ADMIN_C)), contains(ciString("NIC-TEST"))); - } - - @Test - public void valid_reference() { - final RpslObject noInvalidRefPerson = RpslObject.parse("person: other person\nnic-hdl:NIC2-TEST"); - - assertThat(subject.getInvalidReferences(noInvalidRefPerson).keySet(), hasSize(0)); - } - - @Test - public void valid_persisted_reference() { - databaseHelper.addObject(RpslObject.parse("person: person\nnic-hdl:NIC1-TEST")); - final RpslObject maintainer2 = RpslObject.parse("mntner: TEST-MNT\nmnt-by:TEST-MNT\nadmin-c:NIC1-TEST"); - - assertThat(subject.getInvalidReferences(maintainer2).keySet(), hasSize(0)); - } - - @Test - public void is_referenced() { - final RpslObject referenced = RpslObject.parse("person:person\nnic-hdl:NIC3-TEST"); - databaseHelper.addObject(referenced); - - final RpslObject mntRpsl = RpslObject.parse("mntner:MNT-TEST\nmnt-by:MNT-TEST\nadmin-c:NIC3-TEST"); - databaseHelper.addObject(mntRpsl); - - assertThat(subject.isReferenced(referenced), is(true)); - } - - @Test - public void is_not_referenced() { - final RpslObject person2 = RpslObject.parse("person: person\nnic-hdl:NIC5-TEST"); - databaseHelper.addObject(person2); - - assertThat(subject.isReferenced(person2), is(false)); - } - - @Test - public void getOrgAttributeReference() { - final RpslObject object = databaseHelper.addObject(RpslObject.parse("organisation: ORG-TEST")); - - final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\norg:ORG-TEST\nnetname: TEST-RIPE"); - databaseHelper.addObject(inet); - - final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.ORG, ciString("ORG-TEST")); - assertThat(attributeReference.getObjectId(), is(object.getObjectId())); - } - - @Test - public void getSponsoringOrgReference() { - final RpslObject object = databaseHelper.addObject(RpslObject.parse("organisation: ORG-TEST")); - - final RpslObject inet = RpslObject.parse("inetnum: 192.168.0.0 - 192.168.0.255\nsponsoring-org:ORG-TEST\nnetname: TEST-RIPE"); - databaseHelper.addObject(inet); - - final RpslObjectInfo attributeReference = subject.getAttributeReference(AttributeType.SPONSORING_ORG, ciString("ORG-TEST")); - assertThat(attributeReference.getObjectId(), is(object.getObjectId())); - } - - @Test - public void getReferences() { - final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); - databaseHelper.addObject(role); - - final RpslObject org = RpslObject.parse("organisation: ORG-TEST\nabuse-c: NIC-TEST"); - databaseHelper.addObject(org); - - final Set roleReferences = subject.getReferences(role); - - assertThat(roleReferences.size(), is(1)); - assertThat(roleReferences.iterator().next().getKey(), is("ORG-TEST")); - } - - @Test - public void getReferences_none_found() { - final RpslObject role = RpslObject.parse("role: Role\nnic-hdl: NIC-TEST\nabuse-mailbox:abuse@ripe.net"); - databaseHelper.addObject(role); - - final RpslObject org = RpslObject.parse("organisation: ORG-TEST"); - databaseHelper.addObject(org); - - final Set roleReferences = subject.getReferences(role); - - assertThat(roleReferences.size(), is(0)); - } } diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java index 0f612666ac..586ec4e37e 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidator.java @@ -1,7 +1,7 @@ package net.ripe.db.whois.update.handler.validator.common; import com.google.common.collect.ImmutableList; -import net.ripe.db.whois.common.dao.RpslObjectDao; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.update.domain.Action; import net.ripe.db.whois.update.domain.PreparedUpdate; @@ -16,11 +16,11 @@ public class ObjectReferencedValidator implements BusinessRuleValidator { private static final ImmutableList ACTIONS = ImmutableList.of(Action.DELETE); private static final ImmutableList TYPES = ImmutableList.copyOf(ObjectType.values()); - private final RpslObjectDao rpslObjectDao; + private final ReferencesDao referencesDao; @Autowired - public ObjectReferencedValidator(final RpslObjectDao rpslObjectDao) { - this.rpslObjectDao = rpslObjectDao; + public ObjectReferencedValidator(final ReferencesDao referencesDao) { + this.referencesDao = referencesDao; } @Override @@ -29,7 +29,7 @@ public void validate(final PreparedUpdate update, final UpdateContext updateCont return; } - if (rpslObjectDao.isReferenced(update.getReferenceObject())) { + if (referencesDao.isReferenced(update.getReferenceObject())) { updateContext.addMessage(update, UpdateMessages.objectInUse(update.getReferenceObject())); } } diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java index 1af234dd70..a0e5b6bf82 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidator.java @@ -1,7 +1,7 @@ package net.ripe.db.whois.update.handler.validator.common; import com.google.common.collect.ImmutableList; -import net.ripe.db.whois.common.dao.RpslObjectDao; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.rpsl.ObjectMessages; import net.ripe.db.whois.common.rpsl.ObjectType; @@ -25,18 +25,18 @@ public class ReferencedObjectsExistValidator implements BusinessRuleValidator { private static final ImmutableList ACTIONS = ImmutableList.of(Action.CREATE, Action.MODIFY); private static final ImmutableList TYPES = ImmutableList.copyOf(ObjectType.values()); - private final RpslObjectDao rpslObjectDao; + private final ReferencesDao referencesDao; @Autowired - public ReferencedObjectsExistValidator(final RpslObjectDao rpslObjectDao) { - this.rpslObjectDao = rpslObjectDao; + public ReferencedObjectsExistValidator(final ReferencesDao referencesDao) { + this.referencesDao = referencesDao; } @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); - final Map> invalidReferences = rpslObjectDao.getInvalidReferences(updatedObject); + final Map> invalidReferences = referencesDao.getInvalidReferences(updatedObject); final ObjectMessages objectMessages = updateContext.getMessages(update); for (final Map.Entry> invalidReferenceEntry : invalidReferences.entrySet()) { final RpslAttribute attribute = invalidReferenceEntry.getKey(); diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java index 1621936585..6844461ddf 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheck.java @@ -1,9 +1,9 @@ package net.ripe.db.whois.update.handler.validator.inetnum; import com.google.common.collect.ImmutableList; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; -import net.ripe.db.whois.common.dao.RpslObjectUpdateDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.rpsl.AttributeType; import net.ripe.db.whois.common.rpsl.ObjectType; @@ -30,13 +30,13 @@ public class ReferenceCheck implements BusinessRuleValidator { private static final ImmutableList ACTIONS = ImmutableList.of(Action.CREATE, Action.MODIFY); private static final ImmutableList TYPES = ImmutableList.of(ObjectType.INETNUM, ObjectType.INET6NUM); - private final RpslObjectUpdateDao rpslObjectUpdateDao; private final RpslObjectDao rpslObjectDao; + private final ReferencesDao referencesDao; @Autowired - public ReferenceCheck(final RpslObjectUpdateDao rpslObjectUpdateDao, final RpslObjectDao rpslObjectDao) { - this.rpslObjectUpdateDao = rpslObjectUpdateDao; + public ReferenceCheck(final RpslObjectDao rpslObjectDao, final ReferencesDao referencesDao) { this.rpslObjectDao = rpslObjectDao; + this.referencesDao = referencesDao; } @Override @@ -72,7 +72,7 @@ public void validate(final PreparedUpdate update, final UpdateContext updateCont } private RpslObject findOrgReference(final RpslAttribute org) { - final RpslObjectInfo referencedOrganisationInfo = rpslObjectDao.getAttributeReference(org.getType(), org.getCleanValue()); + final RpslObjectInfo referencedOrganisationInfo = referencesDao.getAttributeReference(org.getType(), org.getCleanValue()); return (referencedOrganisationInfo == null ? null : rpslObjectDao.getByKey(ObjectType.ORGANISATION, referencedOrganisationInfo.getKey())); } diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java index 3819ea06fa..81e6f850ef 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidator.java @@ -1,6 +1,7 @@ package net.ripe.db.whois.update.handler.validator.organisation; import com.google.common.collect.ImmutableList; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.domain.Maintainers; @@ -36,11 +37,13 @@ public class AbuseValidator implements BusinessRuleValidator { private final RpslObjectDao objectDao; private Maintainers maintainers; + private ReferencesDao referencesDao; @Autowired - public AbuseValidator(final RpslObjectDao objectDao, final Maintainers maintainers) { + public AbuseValidator(final RpslObjectDao objectDao, final Maintainers maintainers, final ReferencesDao referencesDao) { this.objectDao = objectDao; this.maintainers = maintainers; + this.referencesDao = referencesDao; } @Override @@ -103,7 +106,7 @@ private boolean isAbuseCRemoved(final RpslObject updatedObject, final PreparedUp } private boolean isOrgReferencedByRsMaintainedResources(final RpslObject updatedObject) { - return objectDao.getReferences(updatedObject) + return referencesDao.getReferences(updatedObject) .stream() .filter(Objects::nonNull) .filter(rpslObjectInfo -> ObjectType.RESOURCE_TYPES.contains(rpslObjectInfo.getObjectType())) diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java index 73a41c1c77..6872d0e886 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidator.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.domain.CIString; @@ -36,11 +37,13 @@ public class OrgNameAndCountryAttrValidator implements BusinessRuleValidator { private final RpslObjectDao objectDao; private final Maintainers maintainers; + private final ReferencesDao referencesDao; @Autowired - public OrgNameAndCountryAttrValidator(final RpslObjectDao objectDao, final Maintainers maintainers) { + public OrgNameAndCountryAttrValidator(final RpslObjectDao objectDao, final Maintainers maintainers, final ReferencesDao referencesDao) { this.objectDao = objectDao; this.maintainers = maintainers; + this.referencesDao = referencesDao; } @Override @@ -80,7 +83,7 @@ private boolean isLir(final RpslObject organisation) { } private boolean isReferencedByRsMaintainedResource(final RpslObject rpslObject) { - for (RpslObjectInfo referencedObjectInfo : objectDao.getReferences(rpslObject)) { + for (RpslObjectInfo referencedObjectInfo : referencesDao.getReferences(rpslObject)) { if (RESOURCE_OBJECT_TYPES.contains(referencedObjectInfo.getObjectType())) { final RpslObject referencedObject = objectDao.getById(referencedObjectInfo.getObjectId()); if (isMaintainedByRs(referencedObject)) { diff --git a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java index 38c3d1f95b..c29eab225a 100644 --- a/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java +++ b/whois-update/src/main/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidator.java @@ -2,6 +2,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.domain.CIString; @@ -31,10 +32,12 @@ public class MustKeepAbuseMailboxIfReferencedValidator implements BusinessRuleVa ); private final RpslObjectDao objectDao; + private final ReferencesDao referencesDao; @Autowired - public MustKeepAbuseMailboxIfReferencedValidator(final RpslObjectDao objectDao) { + public MustKeepAbuseMailboxIfReferencedValidator(final RpslObjectDao objectDao, final ReferencesDao referencesDao) { this.objectDao = objectDao; + this.referencesDao = referencesDao; } @Override @@ -46,7 +49,7 @@ public void validate(final PreparedUpdate update, final UpdateContext updateCont return; } - for (final RpslObjectInfo referenceInfo : objectDao.getReferences(update.getUpdatedObject())) { + for (final RpslObjectInfo referenceInfo : referencesDao.getReferences(update.getUpdatedObject())) { if (REFERENCED_OBJECT_TYPES.contains(referenceInfo.getObjectType())) { final Set abuseCAttributes = objectDao.getById(referenceInfo.getObjectId()).getValuesForAttribute(AttributeType.ABUSE_C); if (!abuseCAttributes.isEmpty() && abuseCAttributes.contains(update.getUpdatedObject().getValueForAttribute(AttributeType.NIC_HDL))) { diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java index bc8e749707..af0962d0a4 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ObjectReferencedValidatorTest.java @@ -1,6 +1,6 @@ package net.ripe.db.whois.update.handler.validator.common; -import net.ripe.db.whois.common.dao.RpslObjectDao; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.update.domain.Action; @@ -13,6 +13,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.mockito.Mockito.lenient; @@ -25,7 +27,7 @@ public class ObjectReferencedValidatorTest { @Mock PreparedUpdate update; @Mock UpdateContext updateContext; - @Mock RpslObjectDao rpslObjectDao; + @Mock ReferencesDao referencesDao; @InjectMocks ObjectReferencedValidator subject; @Test @@ -43,6 +45,9 @@ public void validate_no_original_object() { @Test public void validate_not_referenced() { final RpslObject object = RpslObject.parse("mntner: TST-MNT"); + lenient().when(update.getReferenceObject()).thenReturn(object); + lenient().when(referencesDao.getInvalidReferences(object)).thenReturn(Collections.emptyMap()); + subject.validate(update, updateContext); verifyNoMoreInteractions(updateContext); @@ -55,7 +60,7 @@ public void validate_referenced() { when(update.getType()).thenReturn(ObjectType.MNTNER); when(update.hasOriginalObject()).thenReturn(true); when(update.getReferenceObject()).thenReturn(object); - when(rpslObjectDao.isReferenced(object)).thenReturn(true); + when(referencesDao.isReferenced(object)).thenReturn(true); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.objectInUse(object)); @@ -68,7 +73,7 @@ public void validate_referenced_autnum() { when(update.getType()).thenReturn(ObjectType.AUT_NUM); when(update.hasOriginalObject()).thenReturn(true); lenient().when(update.getReferenceObject()).thenReturn(object); - lenient().when(rpslObjectDao.isReferenced(object)).thenReturn(true); + lenient().when(referencesDao.isReferenced(object)).thenReturn(true); subject.validate(update, updateContext); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java index 3dad63cae0..613c7aa403 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/common/ReferencedObjectsExistValidatorTest.java @@ -1,7 +1,7 @@ package net.ripe.db.whois.update.handler.validator.common; import com.google.common.collect.Maps; -import net.ripe.db.whois.common.dao.RpslObjectDao; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.rpsl.ObjectMessages; import net.ripe.db.whois.common.rpsl.RpslAttribute; @@ -32,7 +32,7 @@ public class ReferencedObjectsExistValidatorTest { @Mock PreparedUpdate update; @Mock UpdateContext updateContext; - @Mock RpslObjectDao rpslObjectDao; + @Mock ReferencesDao referencesDao; @InjectMocks ReferencedObjectsExistValidator subject; private RpslObject object; @@ -50,7 +50,7 @@ public void getActions() { @Test public void validate_no_invalid_references() { when(update.getUpdatedObject()).thenReturn(object); - when(rpslObjectDao.getInvalidReferences(object)).thenReturn(Collections.>emptyMap()); + when(referencesDao.getInvalidReferences(object)).thenReturn(Collections.emptyMap()); subject.validate(update, updateContext); verify(updateContext).getMessages(update); @@ -68,7 +68,7 @@ public void validate_invalid_references() { invalidReferences.put(invalidAttribute, invalidAttribute.getCleanValues()); when(updateContext.getMessages(update)).thenReturn(new ObjectMessages()); - when(rpslObjectDao.getInvalidReferences(object)).thenReturn(invalidReferences); + when(referencesDao.getInvalidReferences(object)).thenReturn(invalidReferences); subject.validate(update, updateContext); verify(updateContext).addMessage(update, invalidAttribute, UpdateMessages.unknownObjectReferenced("ADMIN_NC")); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java index 882c5d7006..ec0c278f89 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/inetnum/ReferenceCheckTest.java @@ -1,5 +1,6 @@ package net.ripe.db.whois.update.handler.validator.inetnum; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.rpsl.AttributeType; @@ -33,6 +34,7 @@ public class ReferenceCheckTest { @Mock private PreparedUpdate update; @Mock private UpdateContext updateContext; @Mock private RpslObjectDao rpslObjectDao; + @Mock private ReferencesDao referencesDao; @Mock private RpslObjectInfo rpslObjectInfo; @Mock Subject updateSubject; @@ -67,7 +69,7 @@ public void modify_org_reference_missing_override() { @Test public void modify_org_reference_not_found_in_db() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); subject.validate(update, updateContext); @@ -87,7 +89,7 @@ public void modify_org_reference_not_found_in_db_override() { public void modify_wrong_orgtype_on_found_org() { lenient().when(update.getAction()).thenReturn(Action.MODIFY); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: OTHER")); @@ -109,7 +111,7 @@ public void modify_wrong_orgtype_on_found_org_override() { @Test public void modify_right_orgtype_on_found_org() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: LIR")); @@ -141,7 +143,7 @@ public void create_org_reference_missing_override() { public void create_org_reference_not_found_in_db() { lenient().when(update.getAction()).thenReturn(Action.CREATE); lenient().when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(null); subject.validate(update, updateContext); @@ -161,7 +163,7 @@ public void create_org_reference_not_found_in_db_override() { @Test public void create_wrong_orgtype_on_found_org() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: OTHER")); @@ -173,7 +175,7 @@ public void create_wrong_orgtype_on_found_org() { @Test public void create_wrong_orgtype_on_found_org_direct_assignment() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); lenient().when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: DIRECT_ASSIGNMENT")); @@ -195,7 +197,7 @@ public void create_wrong_orgtype_on_found_org_override() { @Test public void create_right_orgtype_on_found_org() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ALLOCATED PA\norg: ORG1")); - when(rpslObjectDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); + when(referencesDao.getAttributeReference(AttributeType.ORG, ciString("ORG1"))).thenReturn(rpslObjectInfo); when(rpslObjectInfo.getKey()).thenReturn("ORG1"); when(rpslObjectDao.getByKey(ObjectType.ORGANISATION, "ORG1")).thenReturn(RpslObject.parse("organisation: ORG1\norg-type: LIR")); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java index 79ef629ba8..e730af81ec 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/AbuseValidatorTest.java @@ -1,9 +1,9 @@ package net.ripe.db.whois.update.handler.validator.organisation; import com.google.common.collect.Sets; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectDao; -import net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectUpdateDao; import net.ripe.db.whois.common.domain.CIString; import net.ripe.db.whois.common.domain.Maintainers; import net.ripe.db.whois.common.rpsl.ObjectType; @@ -47,7 +47,7 @@ public class AbuseValidatorTest { @Mock PreparedUpdate update; @Mock UpdateContext updateContext; @Mock JdbcRpslObjectDao objectDao; - @Mock JdbcRpslObjectUpdateDao updateDao; + @Mock ReferencesDao referencesDao; @Mock Maintainers maintainers; @InjectMocks AbuseValidator subject; @@ -129,7 +129,7 @@ public void remove_abuse_contact_LIR_org() { public void allow_remove_abuse_contact_when_non_LIR_no_referencing_objects() { when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Collections.EMPTY_SET); + when(referencesDao.getReferences(update.getUpdatedObject())).thenReturn(Collections.EMPTY_SET); subject.validate(update, updateContext); @@ -143,7 +143,7 @@ public void allow_remove_abusec_when_referencing_object_is_not_resource() { final RpslObjectInfo info = new RpslObjectInfo(1, ObjectType.PERSON, "AS6"); when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); + when(referencesDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); subject.validate(update, updateContext); @@ -156,7 +156,7 @@ public void allow_remove_abuse_contact_when_referencing_resources_is_not_rs_main final RpslObjectInfo info = new RpslObjectInfo(1, ObjectType.AUT_NUM, "AS6"); when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); + when(referencesDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); when(objectDao.getById(info.getObjectId())).thenReturn(RESOURCE_NOT_RS_MAINTAINED); subject.validate(update, updateContext); @@ -173,7 +173,7 @@ public void do_not_allow_remove_abuse_contact_when_a_referencing_resource_is_rs_ final RpslObjectInfo info = new RpslObjectInfo(1, ObjectType.AUT_NUM, "AS6"); when(update.getReferenceObject()).thenReturn(OTHER_ORG_WITH_ABUSE_C); when(update.getUpdatedObject()).thenReturn(OTHER_ORG_WITHOUT_ABUSE_C); - when(objectDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); + when(referencesDao.getReferences(update.getUpdatedObject())).thenReturn(Sets.newHashSet(info)); when(objectDao.getById(info.getObjectId())).thenReturn(RESOURCE_RS_MAINTAINED); subject.validate(update, updateContext); diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java index 5933efc5b9..d078a7859a 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/organisation/OrgNameAndCountryAttrValidatorTest.java @@ -1,6 +1,7 @@ package net.ripe.db.whois.update.handler.validator.organisation; import com.google.common.collect.Sets; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.domain.CIString; @@ -42,6 +43,7 @@ public class OrgNameAndCountryAttrValidatorTest { @Mock private PreparedUpdate update; @Mock private Subject subjectObject; @Mock private RpslObjectDao objectDao; + @Mock private ReferencesDao referencesDao; @Mock private Maintainers maintainers; @InjectMocks private OrgNameAndCountryAttrValidator subject; @@ -127,7 +129,7 @@ public void orgname_not_changed() { public void orgname_changed_not_referenced_at_all() { when(update.getReferenceObject()).thenReturn(ORIGINAL_ORG); when(update.getUpdatedObject()).thenReturn(UPDATED_ORG_NEW_NAME); - when(objectDao.getReferences(ORIGINAL_ORG)).thenReturn(Collections.EMPTY_SET); + when(referencesDao.getReferences(ORIGINAL_ORG)).thenReturn(Collections.EMPTY_SET); subject.validate(update, updateContext); @@ -155,7 +157,7 @@ public void orgname_changed_for_lir() { public void orgname_changed_not_referenced_by_resource() { when(update.getReferenceObject()).thenReturn(ORIGINAL_ORG); when(update.getUpdatedObject()).thenReturn(UPDATED_ORG_NEW_NAME); - when(objectDao.getReferences(ORIGINAL_ORG)).thenReturn(Sets.newHashSet(new RpslObjectInfo(5, ObjectType.PERSON, "TEST-NIC"))); + when(referencesDao.getReferences(ORIGINAL_ORG)).thenReturn(Sets.newHashSet(new RpslObjectInfo(5, ObjectType.PERSON, "TEST-NIC"))); subject.validate(update, updateContext); @@ -329,6 +331,6 @@ private void presetReferrers(RpslObject... referrerObjects) { lenient().when(objectDao.getById(referrerObject.getObjectId())).thenReturn(referrerObject); } - lenient().when(objectDao.getReferences(ORIGINAL_ORG)).thenReturn(rpslObjectInfos); + lenient().when(referencesDao.getReferences(ORIGINAL_ORG)).thenReturn(rpslObjectInfos); } } diff --git a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java index dccd6581e4..9206d224fe 100644 --- a/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java +++ b/whois-update/src/test/java/net/ripe/db/whois/update/handler/validator/personrole/MustKeepAbuseMailboxIfReferencedValidatorTest.java @@ -1,9 +1,9 @@ package net.ripe.db.whois.update.handler.validator.personrole; import com.google.common.collect.Sets; +import net.ripe.db.whois.common.dao.ReferencesDao; import net.ripe.db.whois.common.dao.RpslObjectInfo; import net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectDao; -import net.ripe.db.whois.common.dao.jdbc.JdbcRpslObjectUpdateDao; import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.update.domain.Action; @@ -11,15 +11,14 @@ import net.ripe.db.whois.update.domain.UpdateContext; import net.ripe.db.whois.update.domain.UpdateMessages; import org.junit.jupiter.api.Test; - +import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.junit.jupiter.MockitoExtension; +import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -28,7 +27,7 @@ public class MustKeepAbuseMailboxIfReferencedValidatorTest { @Mock PreparedUpdate update; @Mock UpdateContext updateContext; - @Mock JdbcRpslObjectUpdateDao updateDao; + @Mock ReferencesDao referencesDao; @Mock JdbcRpslObjectDao objectDao; @InjectMocks MustKeepAbuseMailboxIfReferencedValidator subject; @@ -60,7 +59,7 @@ public void remove_unreferenced_abuse_mailbox() { when(update.getReferenceObject()).thenReturn(originalObject); when(update.getUpdatedObject()).thenReturn(updatedObject); - when(objectDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet()); + when(referencesDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet()); subject.validate(update, updateContext); verifyNoMoreInteractions(updateContext); @@ -86,7 +85,7 @@ public void remove_referenced_abuse_mailbox() { when(update.getReferenceObject()).thenReturn(originalObject); when(update.getUpdatedObject()).thenReturn(updatedObject); - when(objectDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet(new RpslObjectInfo(1, ObjectType.ORGANISATION, "ORG-TEST1"))); + when(referencesDao.getReferences(updatedObject)).thenReturn(Sets.newHashSet(new RpslObjectInfo(1, ObjectType.ORGANISATION, "ORG-TEST1"))); when(objectDao.getById(1)).thenReturn(RpslObject.parse("organisation: ORG-TEST1\nabuse-c: TEST-NIC")); subject.validate(update, updateContext); From 88d3327b94322b0b007cc9e25e37d705e5b452bd Mon Sep 17 00:00:00 2001 From: Ed Shryane Date: Fri, 22 Jul 2022 14:29:33 +0200 Subject: [PATCH 4/6] Specify whoisSlaveDataSource in the References DAO to make sure the queries are local. The sourceAwareDataSource seems to default to the *master* in some cases, even for queries. --- .../net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java index c318f3d033..51c73d737e 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java @@ -37,7 +37,7 @@ public class JdbcReferencesDao implements ReferencesDao { private final JdbcTemplate jdbcTemplate; @Autowired - public JdbcReferencesDao(@Qualifier("sourceAwareDataSource") final DataSource dataSource) { + public JdbcReferencesDao(@Qualifier("whoisSlaveDataSource") final DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } From 4f04f9cc46d210aca5e86db3ab54ce13e37ae0d9 Mon Sep 17 00:00:00 2001 From: Ed Shryane Date: Mon, 25 Jul 2022 17:53:54 +0200 Subject: [PATCH 5/6] Extend JdbcRpslObjectDao --- .../ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java index 51c73d737e..e6e0e9c08e 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java @@ -14,6 +14,7 @@ import net.ripe.db.whois.common.rpsl.ObjectType; import net.ripe.db.whois.common.rpsl.RpslAttribute; import net.ripe.db.whois.common.rpsl.RpslObject; +import net.ripe.db.whois.common.source.SourceContext; import org.apache.commons.lang.Validate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -32,12 +33,13 @@ @Repository @RetryFor(RecoverableDataAccessException.class) -public class JdbcReferencesDao implements ReferencesDao { +public class JdbcReferencesDao extends JdbcRpslObjectDao implements ReferencesDao { private final JdbcTemplate jdbcTemplate; @Autowired - public JdbcReferencesDao(@Qualifier("whoisSlaveDataSource") final DataSource dataSource) { + public JdbcReferencesDao(@Qualifier("sourceAwareDataSource") final DataSource dataSource, final SourceContext sourceContext) { + super(dataSource, sourceContext); this.jdbcTemplate = new JdbcTemplate(dataSource); } From 062c494185adcff4c2d00c01e202b4307c415515 Mon Sep 17 00:00:00 2001 From: Ed Shryane Date: Wed, 31 Aug 2022 12:22:02 +0200 Subject: [PATCH 6/6] WIP: added log messages for debugging. --- .../common/dao/jdbc/JdbcReferencesDao.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java index e6e0e9c08e..8618263734 100644 --- a/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java +++ b/whois-commons/src/main/java/net/ripe/db/whois/common/dao/jdbc/JdbcReferencesDao.java @@ -16,6 +16,8 @@ import net.ripe.db.whois.common.rpsl.RpslObject; import net.ripe.db.whois.common.source.SourceContext; import org.apache.commons.lang.Validate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.dao.RecoverableDataAccessException; @@ -33,18 +35,27 @@ @Repository @RetryFor(RecoverableDataAccessException.class) +// TODO: [ES] sourceaware only seems to use the correct source when extending JdbcRpslObjectDao? public class JdbcReferencesDao extends JdbcRpslObjectDao implements ReferencesDao { + private static final Logger LOGGER = LoggerFactory.getLogger(JdbcReferencesDao.class); + + final SourceContext sourceContext; // TODO: [ES] debug only private final JdbcTemplate jdbcTemplate; + // TODO: [ES] if we use a hardcoded @Qualifier("whoisSlaveDataSource") final DataSource dataSource + // then whois-update will use the possibly out-of-date slave source when validating references. @Autowired public JdbcReferencesDao(@Qualifier("sourceAwareDataSource") final DataSource dataSource, final SourceContext sourceContext) { super(dataSource, sourceContext); this.jdbcTemplate = new JdbcTemplate(dataSource); + this.sourceContext = sourceContext; } @Override public boolean isReferenced(final RpslObject object) { + LOGGER.warn("*** isReferenced: currentSource = {} ***", sourceContext.getCurrentSource()); + for (final RpslAttribute attribute : object.findAttributes(ObjectTemplate.getTemplate(object.getType()).getKeyAttributes())) { for (final IndexStrategy indexStrategy : IndexStrategies.getReferencing(object.getType())) { for (final CIString value : attribute.getReferenceValues()) { @@ -63,7 +74,9 @@ public boolean isReferenced(final RpslObject object) { @Override public Set getReferences(final RpslObject object) { - Set references = Sets.newHashSet(); + LOGGER.warn("*** getReferences: currentSource = {} ***", sourceContext.getCurrentSource()); + + final Set references = Sets.newHashSet(); final List indexStrategies = IndexStrategies.getReferencing(object.getType()); // for route(6), individually check each key @@ -87,6 +100,8 @@ public Set getReferences(final RpslObject object) { @Override public Map> getInvalidReferences(final RpslObject object) { + LOGGER.warn("*** getInvalidReferences: currentSource = {} ***", sourceContext.getCurrentSource()); + final Map> invalidReferenceMap = Maps.newHashMap(); for (final RpslAttribute attribute : object.getAttributes()) { @@ -136,6 +151,8 @@ private boolean isInvalidReference(final RpslObject object, final AttributeType @CheckForNull public RpslObjectInfo getAttributeReference(final AttributeType attributeType, final CIString value) { + LOGGER.warn("*** getAttributeReference: currentSource = {} ***", sourceContext.getCurrentSource()); + final CIString referenceValue = new RpslAttribute(attributeType, value.toString()).getReferenceValue(); for (final ObjectType objectType : attributeType.getReferences()) { final RpslObjectInfo result = getAttributeReference(objectType, referenceValue);