method2testcases
stringlengths
118
6.63k
### Question: SetNotReferencedValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired SetNotReferencedValidator(final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.DELETE)); }
### Question: SetNotReferencedValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired SetNotReferencedValidator(final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.AS_SET, ObjectType.ROUTE_SET, ObjectType.RTR_SET)); }
### Question: SetNotReferencedValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); final List<RpslObjectInfo> incomingReferences = objectDao.findMemberOfByObjectTypeWithoutMbrsByRef(updatedObject.getType(), updatedObject.getKey().toString()); if (!incomingReferences.isEmpty()) { updateContext.addMessage(update, UpdateMessages.objectInUse(updatedObject)); } } @Autowired SetNotReferencedValidator(final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void set_that_has_incoming_references() { final RpslObject routeSet = RpslObject.parse("route-set: rs-AH"); when(update.getUpdatedObject()).thenReturn(routeSet); when(objectDao.findMemberOfByObjectTypeWithoutMbrsByRef(ObjectType.ROUTE_SET, "rs-AH")).thenReturn(Lists.newArrayList(new RpslObjectInfo(1, ObjectType.ROUTE, "192.168.0.1/32"))); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.objectInUse(routeSet)); } @Test public void set_that_has_no_incoming_references() { final RpslObject asSet = RpslObject.parse("as-set: AS1325:AS-lopp"); when(update.getUpdatedObject()).thenReturn(asSet); when(objectDao.findMemberOfByObjectTypeWithoutMbrsByRef(ObjectType.AS_SET, "rs-AH")).thenReturn(Lists.<RpslObjectInfo>newArrayList()); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(update, UpdateMessages.objectInUse(asSet)); }
### Question: PeeringSetAttributeMustBePresent implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } PeeringSetAttributeMustBePresent(); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: PeeringSetAttributeMustBePresent implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } PeeringSetAttributeMustBePresent(); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetTypes() { assertThat(subject.getTypes(), Matchers.contains(ObjectType.PEERING_SET, ObjectType.FILTER_SET)); }
### Question: SetnameMustExistValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired SetnameMustExistValidator(final RpslObjectDao objectDao, final AuthenticationModule authenticationModule); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: SetnameMustExistValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired SetnameMustExistValidator(final RpslObjectDao objectDao, final AuthenticationModule authenticationModule); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.AS_SET, ObjectType.FILTER_SET, ObjectType.PEERING_SET, ObjectType.ROUTE_SET, ObjectType.RTR_SET)); }
### Question: OrganisationTypeValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions().size(), is(2)); assertThat(subject.getActions().contains(Action.MODIFY), is(true)); assertThat(subject.getActions().contains(Action.CREATE), is(true)); }
### Question: OrganisationTypeValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes().size(), is(1)); assertThat(subject.getTypes().get(0), is(ObjectType.ORGANISATION)); }
### Question: OrgNameNotChangedValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired OrgNameNotChangedValidator(final RpslObjectUpdateDao objectUpdateDao, final RpslObjectDao objectDao, final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(Action.MODIFY)); }
### Question: OrgNameNotChangedValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired OrgNameNotChangedValidator(final RpslObjectUpdateDao objectUpdateDao, final RpslObjectDao objectDao, final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), contains(ObjectType.ORGANISATION)); }
### Question: LirUserMaintainedAttributesValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions().size(), is(1)); assertTrue(subject.getActions().contains(Action.MODIFY)); }
### Question: LirUserMaintainedAttributesValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes().size(), is(1)); assertTrue(subject.getTypes().contains(ObjectType.ORGANISATION)); }
### Question: AbuseCNoLimitWarningValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: AbuseCNoLimitWarningValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.ROLE)); }
### Question: AbuseCNoLimitWarningValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { if (update.getUpdatedObject().containsAttribute(AttributeType.ABUSE_MAILBOX) && (!update.hasOriginalObject() || !update.getReferenceObject().containsAttribute(AttributeType.ABUSE_MAILBOX))) { updateContext.addMessage(update, UpdateMessages.abuseCNoLimitWarning()); } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void role_has_added_abuse_mailbox_create() { final RpslObject role = RpslObject.parse("role: Some Role\nnic-hdl: TEST-NIC\nabuse-mailbox: abuse@ripe.net"); when(update.hasOriginalObject()).thenReturn(false); when(update.getReferenceObject()).thenReturn(role); when(update.getUpdatedObject()).thenReturn(role); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.abuseCNoLimitWarning()); } @Test public void role_has_added_abuse_mailbox_modify() { when(update.hasOriginalObject()).thenReturn(true); when(update.getReferenceObject()).thenReturn(RpslObject.parse("role: Some Role\nnic-hdl: TEST-NIC")); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("role: Some Role\nnic-hdl: TEST-NIC\nabuse-mailbox: abuse@ripe.net")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.abuseCNoLimitWarning()); } @Test public void role_has_not_added_abuse_mailbox() { when(update.hasOriginalObject()).thenReturn(true); when(update.getReferenceObject()).thenReturn(RpslObject.parse("role: Some Role\nnic-hdl: TEST-NIC\nabuse-mailbox: abuse@ripe.net")); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("role: Some Role\nnic-hdl: TEST-NIC\nabuse-mailbox: abuse@ripe.net")); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(update, UpdateMessages.abuseCNoLimitWarning()); }
### Question: LirMntByAttributeCountValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired LirMntByAttributeCountValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions().size(), is(2)); assertTrue(subject.getActions().contains(Action.CREATE)); assertTrue(subject.getActions().contains(Action.MODIFY)); }
### Question: LirMntByAttributeCountValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired LirMntByAttributeCountValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes().size(), is(1)); assertTrue(subject.getTypes().contains(ObjectType.ORGANISATION)); }
### Question: LirMntByAttributeCountValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject originalObject = update.getReferenceObject(); if (!isLir(originalObject)) { return; } final RpslObject updatedObject = update.getUpdatedObject(); final Collection<CIString> userMntner = filterUserMntner(updatedObject); if (userMntner.size() > 1) { updateContext.addMessage(update, UpdateMessages.multipleUserMntBy(userMntner)); } } @Autowired LirMntByAttributeCountValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void update_of_not_lir_with_single_mntner() { when(update.getReferenceObject()).thenReturn(NON_LIR_ORG); when(authenticationSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(false); when(update.getUpdatedObject()).thenReturn(NON_LIR_ORG_SINGLE_USER_MNTNER); subject.validate(update, updateContext); verify(update).getReferenceObject(); verifyNoMoreInteractions(update); verifyZeroInteractions(maintainers, updateContext); } @Test public void update_of_not_lir_with_multiple_mntner() { when(update.getReferenceObject()).thenReturn(NON_LIR_ORG); when(authenticationSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(false); when(update.getUpdatedObject()).thenReturn(NON_LIR_ORG_MULTIPLE_USER_MNTNER); subject.validate(update, updateContext); verify(update).getReferenceObject(); verifyNoMoreInteractions(update); verifyZeroInteractions(maintainers, updateContext); } @Test public void update_of_lir_with_single_mntner() { when(maintainers.isRsMaintainer(ciString("MNT1-LIR"))).thenReturn(false); when(update.getReferenceObject()).thenReturn(LIR_ORG); when(authenticationSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(false); when(update.getUpdatedObject()).thenReturn(LIR_ORG_SINGLE_USER_MNTNER); subject.validate(update, updateContext); verify(maintainers).isRsMaintainer(ciString("MNT1-LIR")); verifyNoMoreInteractions(maintainers); verify(update).getReferenceObject(); verify(update).getUpdatedObject(); verifyNoMoreInteractions(update); verifyZeroInteractions(updateContext); } @Test public void update_of_lir_with_multiple_mntner() { when(maintainers.isRsMaintainer(ciString("MNT1-LIR"))).thenReturn(false); when(maintainers.isRsMaintainer(ciString("MNT2-LIR"))).thenReturn(false); when(update.getReferenceObject()).thenReturn(LIR_ORG); when(authenticationSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(false); when(update.getUpdatedObject()).thenReturn(LIR_ORG_MULTIPLE_USER_MNTNER); subject.validate(update, updateContext); verify(maintainers).isRsMaintainer(ciString("MNT1-LIR")); verify(maintainers).isRsMaintainer(ciString("MNT2-LIR")); verifyNoMoreInteractions(maintainers); verify(update).getReferenceObject(); verify(update).getUpdatedObject(); verify(updateContext).addMessage(update, UpdateMessages.multipleUserMntBy(ImmutableList.of(ciString("MNT1-LIR"), ciString("MNT2-LIR")))); verifyNoMoreInteractions(update); verifyNoMoreInteractions(updateContext); }
### Question: ExportFileWriterFactory { public boolean isExportDir(final File dir) { final File[] files = dir.listFiles(); if (files == null) { return false; } for (final File file : files) { if (file.isFile()) { return false; } final String fileName = file.getName(); if (! (fileName.equals(externalExportDir) || fileName.equals(internalExportDir) || fileName.equals(legacyExternalExportDir))) { return false; } } return true; } @Autowired ExportFileWriterFactory(final DummifierNrtm dummifierNrtm, final DummifierCurrent dummifierCurrent, @Value("${dir.rpsl.export.internal}") final String internalExportDir, @Value("${dir.rpsl.export.external}") final String externalExportDir, @Value("${dir.rpsl.export.external.legacy}") final String legacyExternalExportDir, @Value("${whois.source}") final String source, @Value("${whois.nonauth.source}") final String nonAuthSource); List<ExportFileWriter> createExportFileWriters(final File baseDir, final int lastSerial); boolean isExportDir(final File dir); }### Answer: @Test public void isExportDir_empty() { assertThat(subject.isExportDir(folder.getRoot()), Matchers.is(true)); } @Test public void isExportDir_home() { final File homeDir = new File(System.getProperty("user.home")); assertThat(homeDir.exists(), Matchers.is(true)); assertThat(homeDir.isDirectory(), Matchers.is(true)); assertThat(homeDir.canWrite(), Matchers.is(true)); assertThat(subject.isExportDir(homeDir), Matchers.is(false)); }
### Question: LirRipeMaintainedAttributesValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions().size(), is(1)); assertTrue(subject.getActions().contains(Action.MODIFY)); }
### Question: LirRipeMaintainedAttributesValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes().size(), is(1)); assertTrue(subject.getTypes().contains(ObjectType.ORGANISATION)); }
### Question: AggregatedByLirStatusValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired AggregatedByLirStatusValidator(final Ipv6Tree ipv6Tree, final RpslObjectDao rpslObjectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(Action.CREATE, Action.MODIFY)); }
### Question: AggregatedByLirStatusValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired AggregatedByLirStatusValidator(final Ipv6Tree ipv6Tree, final RpslObjectDao rpslObjectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), contains(ObjectType.INET6NUM)); }
### Question: SponsoringOrgValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired SponsoringOrgValidator(final RpslObjectDao objectDao, final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(CREATE, MODIFY)); }
### Question: SponsoringOrgValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired SponsoringOrgValidator(final RpslObjectDao objectDao, final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(INETNUM, INET6NUM, AUT_NUM)); }
### Question: StatusValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { if (update.getAction() == DELETE) { if (update.getType().equals(INETNUM)) { validateDelete(update, updateContext, ipv4Tree); } else { validateDelete(update, updateContext, ipv6Tree); } } else { validateModify(update, updateContext); } } @Autowired StatusValidator( final RpslObjectDao objectDao, final Ipv4Tree ipv4Tree, final Ipv6Tree ipv6Tree, final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void delete_inetnum_w_legacy_not_allowed_under_unspecified_w_non_rs_maintainer() { when(update.getAction()).thenReturn(Action.DELETE); when(ipv4Tree.findFirstLessSpecific(any(Ipv4Resource.class))).thenReturn(Lists.newArrayList(new Ipv4Entry(Ipv4Resource.parse("0/0"), 1))); when(objectDao.getById(1)).thenReturn(RpslObject.parse("" + "inetnum: 0.0.0.0 - 255.255.255.255\n" + "status: ALLOCATED UNSPECIFIED")); when(update.getType()).thenReturn(ObjectType.INETNUM); when(update.getReferenceObject()).thenReturn(RpslObject.parse("" + "inetnum: 192.0/24\n" + "status: LEGACY\n" + "mnt-by: TEST-MNT\n" + "password: update")); subject.validate(update, updateContext); verify(updateContext, times(1)).addMessage(update, UpdateMessages.inetnumStatusLegacy()); } @Test public void modify_status_change() { when(update.getAction()).thenReturn(Action.MODIFY); when(update.getReferenceObject()).thenReturn(RpslObject.parse("" + "inetnum: 192.0/24\n" + "status: ASSIGNED PI")); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "inetnum: 192.0/24\n" + "status: ASSIGNED PA")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.statusChange()); }
### Question: MntRouteRangeValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void supports() { final List<ObjectType> supportedTypes = subject.getTypes(); assertThat(supportedTypes, containsInAnyOrder(ObjectType.INETNUM, ObjectType.INET6NUM)); }
### Question: MntRouteRangeValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void supports_actions_create_modify() { final List<Action> actions = subject.getActions(); assertThat(actions.size(), is(2)); assertThat(actions.contains(Action.CREATE), is(true)); assertThat(actions.contains(Action.MODIFY), is(true)); }
### Question: LirMntByValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired LirMntByValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(Action.MODIFY)); }
### Question: LirMntByValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired LirMntByValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.INETNUM, ObjectType.INET6NUM)); }
### Question: IntersectionValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired IntersectionValidator(final Ipv4Tree ipv4Tree, final Ipv6Tree ipv6Tree); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(Action.CREATE)); }
### Question: IntersectionValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired IntersectionValidator(final Ipv4Tree ipv4Tree, final Ipv6Tree ipv6Tree); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.INETNUM, ObjectType.INET6NUM)); }
### Question: IntersectionValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final IpInterval ipInterval = IpInterval.parse(update.getReferenceObject().getKey()); if (ipInterval instanceof Ipv4Resource) { validateIntersections(update, updateContext, ipInterval, ipv4Tree); } else { validateIntersections(update, updateContext, ipInterval, ipv6Tree); } } @Autowired IntersectionValidator(final Ipv4Tree ipv4Tree, final Ipv6Tree ipv6Tree); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_no_children_ipv4() { final RpslObject object = RpslObject.parse("inetnum: 193.0.0.0"); when(ipv4Tree.findFirstMoreSpecific(parentIpv4Key)).thenReturn(Lists.<Ipv4Entry>newArrayList()); when(update.getReferenceObject()).thenReturn(object); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void validate_no_children_ipv6() { final RpslObject object = RpslObject.parse("inet6num: 2001:0658:021A::/48"); when(ipv6Tree.findFirstMoreSpecific(parentIpv6Key)).thenReturn(Lists.<Ipv6Entry>newArrayList()); when(update.getReferenceObject()).thenReturn(object); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void validate_no_intersections_ipv4() { final RpslObject object = RpslObject.parse("inetnum: 193.0.0.0 - 193.0.0.10"); when(ipv4Tree.findFirstMoreSpecific(parentIpv4Key)).thenReturn(Lists.newArrayList( new Ipv4Entry(Ipv4Resource.parse("192/8"), 1), new Ipv4Entry(Ipv4Resource.parse("193.0.0.1"), 2), new Ipv4Entry(Ipv4Resource.parse("193.0.1/24"), 3), new Ipv4Entry(Ipv4Resource.parse("193.0.0.1 - 193.0.0.2"), 4) )); when(update.getReferenceObject()).thenReturn(object); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void validate_intersections_ipv4() { final RpslObject object = RpslObject.parse("inetnum: 193.0.0.0 - 193.0.0.10"); when(ipv4Tree.findFirstMoreSpecific(parentIpv4Key)).thenReturn(Lists.newArrayList( new Ipv4Entry(Ipv4Resource.parse("193.0.0.10 - 193.0.0.12"), 1), new Ipv4Entry(Ipv4Resource.parse("193.0.1/24"), 2) )); when(update.getReferenceObject()).thenReturn(object); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.intersectingRange(Ipv4Resource.parse("193.0.0.10 - 193.0.0.12"))); verifyNoMoreInteractions(updateContext); } @Test public void validate_invalid_parent_interval() throws Exception { when(ipv6Tree.findFirstLessSpecific(any(Ipv6Resource.class))).thenReturn(Collections.<Ipv6Entry>emptyList()); when(update.getReferenceObject()).thenReturn(RpslObject.parse("inet6num: fe80::/32")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.invalidParentEntryForInterval(Ipv6Resource.parse("fe80::/32"))); verifyNoMoreInteractions(updateContext); }
### Question: EndUserMaintainerChecks implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final Subject subject = updateContext.getSubject(update); if (subject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)) { return; } if (subject.hasPrincipal(Principal.ENDUSER_MAINTAINER)) { final boolean hasEnduserMaintainers = maintainers.isEnduserMaintainer( update.getUpdatedObject().getValuesForAttribute(AttributeType.MNT_BY)); if (!hasEnduserMaintainers) { updateContext.addMessage(update, UpdateMessages.adminMaintainerRemoved()); } } } @Autowired EndUserMaintainerChecks(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void modify_has_no_endusermntner() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ASSIGNED ANYCAST\nmnt-by: TEST-MNT")); when(updateContext.getSubject(update)).thenReturn(principalSubject); when(principalSubject.hasPrincipal(Principal.ENDUSER_MAINTAINER)).thenReturn(true); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.adminMaintainerRemoved()); verify(maintainers).isEnduserMaintainer(ciSet("TEST-MNT")); verifyNoMoreInteractions(maintainers); } @Test public void modify_has_no_endusermntner_override() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ASSIGNED ANYCAST\nmnt-by: TEST-MNT")); when(updateContext.getSubject(update)).thenReturn(principalSubject); when(principalSubject.hasPrincipal(any(Principal.class))).thenReturn(true); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(eq(update), any(Message.class)); verifyZeroInteractions(maintainers); } @Test public void modify_succeeds() { when(principalSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(true); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 192.0/24\nstatus: ASSIGNED ANYCAST\nmnt-by: END-MNT")); when(updateContext.getSubject(update)).thenReturn(principalSubject); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(eq(update), any(Message.class)); verifyZeroInteractions(maintainers); }
### Question: OrgAttributeNotChangedValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired OrgAttributeNotChangedValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(Action.MODIFY)); }
### Question: OrgAttributeNotChangedValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired OrgAttributeNotChangedValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.INETNUM, ObjectType.INET6NUM, ObjectType.AUT_NUM)); }
### Question: AttributeSanitizer { public CIString sanitizeKey(final RpslObject originalObject) { final List<RpslAttribute> keyRelatedAttributes = getKeyRelatedAttributes(originalObject); return new RpslObject(sanitizeKeyAttributes(keyRelatedAttributes)).getKey(); } @Autowired AttributeSanitizer(DateTimeProvider dateTimeProvider); CIString sanitizeKey(final RpslObject originalObject); RpslObject sanitize(final RpslObject object, final ObjectMessages objectMessages); }### Answer: @Test public void transform_key_inetrtr_no_dot() { final RpslObject rpslObject = RpslObject.parse("" + "inet-rtr: test.ripe.net\n" + "alias: alias.ripe.net"); final CIString result = attributeSanitizer.sanitizeKey(rpslObject); assertThat(result.toString(), is("test.ripe.net")); verifyNoMoreInteractions(objectMessages); } @Test public void transform_key_inetrtr_with_trailing_dot() { final RpslObject rpslObject = RpslObject.parse("" + "inet-rtr: test.ripe.net.\n" + "alias: alias.ripe.net"); final CIString result = attributeSanitizer.sanitizeKey(rpslObject); assertThat(result.toString(), is("test.ripe.net")); verifyNoMoreInteractions(objectMessages); } @Test public void transform_key_domain_no_dot() { final RpslObject rpslObject = RpslObject.parse("domain: 17.45.212.in-addr.arpa"); final CIString result = attributeSanitizer.sanitizeKey(rpslObject); assertThat(result.toString(), is("17.45.212.in-addr.arpa")); } @Test public void transform_keys_domain_with_trailing_dot() { final RpslObject rpslObject = RpslObject.parse("domain: 17.45.212.in-addr.arpa."); final CIString result = attributeSanitizer.sanitizeKey(rpslObject); assertThat(result.toString(), is("17.45.212.in-addr.arpa")); }
### Question: AsblockByRsMaintainersOnlyValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: AsblockByRsMaintainersOnlyValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.AS_BLOCK)); }
### Question: AsblockByRsMaintainersOnlyValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final boolean authenticatedByOverride = updateContext.getSubject(update).hasPrincipal(Principal.OVERRIDE_MAINTAINER); final boolean authenticatedByDbmMaintainer = updateContext.getSubject(update).hasPrincipal(Principal.DBM_MAINTAINER); if (!(authenticatedByOverride || authenticatedByDbmMaintainer)) { updateContext.addMessage(update, UpdateMessages.asblockIsMaintainedByRipe()); } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_override_succeeds() { when(update.isOverride()).thenReturn(true); when(updateContext.getSubject(update)).thenReturn(subjectObject); when(subjectObject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(true); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(update, UpdateMessages.asblockIsMaintainedByRipe()); } @Test public void validate_authenticatedByDbmMaintainer_succeeds() { when(update.isOverride()).thenReturn(false); when(updateContext.getSubject(update)).thenReturn(subjectObject); when(subjectObject.hasPrincipal(Principal.DBM_MAINTAINER)).thenReturn(true); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(update, UpdateMessages.asblockIsMaintainedByRipe()); } @Test public void validate_not_authenticatedByDbmMaintainer_or_override_fails() { when(update.isOverride()).thenReturn(false); when(updateContext.getSubject(update)).thenReturn(subjectObject); when(subjectObject.hasPrincipal(Principal.DBM_MAINTAINER)).thenReturn(false); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.asblockIsMaintainedByRipe()); }
### Question: AsblockHierarchyValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired AsblockHierarchyValidator(final RpslObjectDao rpslObjectDao); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); }### Answer: @Test public void testGetActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: AsblockHierarchyValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired AsblockHierarchyValidator(final RpslObjectDao rpslObjectDao); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); }### Answer: @Test public void testGetTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.AS_BLOCK)); }
### Question: PoemHasOnlyPublicMaintainerValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: PoemHasOnlyPublicMaintainerValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.POEM)); }
### Question: PoemHasOnlyPublicMaintainerValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslAttribute mntByAttribute = update.getUpdatedObject().findAttribute(AttributeType.MNT_BY); if (!mntByAttribute.getCleanValue().equals(POEM_MAINTAINER)) { updateContext.addMessage(update, mntByAttribute, UpdateMessages.poemRequiresPublicMaintainer()); } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_LIM_MNT() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "poem: POEM-FORM-LIMERICK\n" + "mnt-by: LIM-MNT\n")); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void validate_other_maintainer() { final RpslObject poem = RpslObject.parse("" + "poem: POEM-FORM-LIMERICK\n" + "mnt-by: DEV-MNT\n"); when(update.getUpdatedObject()).thenReturn(poem); subject.validate(update, updateContext); verify(updateContext).addMessage(update, poem.findAttribute(AttributeType.MNT_BY), UpdateMessages.poemRequiresPublicMaintainer()); }
### Question: BogonValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); if (updatedObject == null) { return; } getPrefix(updatedObject).ifPresent(prefix -> { if (isBogon(prefix.toString())) { updateContext.addMessage(update, UpdateMessages.bogonPrefixNotAllowed(prefix.toString())); } }); } @Autowired BogonValidator(@Value("${ipranges.bogons:}") final String ... bogons); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void ipv6_exact_match_bogon() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("route6: 2001:2::/48\norigin: AS3333\nsource: TEST")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.bogonPrefixNotAllowed("2001:2::/48")); verifyNoMoreInteractions(updateContext); } @Test public void ipv4_exact_match_bogon() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("route: 192.0.2.0/24\norigin: AS3333\nsource: TEST")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.bogonPrefixNotAllowed("192.0.2.0/24")); verifyNoMoreInteractions(updateContext); } @Test public void ipv6_more_specific_bogon() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("route6: 2001:2:0:1::/64\norigin: AS3333\nsource: TEST")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.bogonPrefixNotAllowed("2001:2:0:1::/64")); verifyNoMoreInteractions(updateContext); } @Test public void ipv4_more_specific_bogon() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("route: 192.0.2.1/32\norigin: AS3333\nsource: TEST")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.bogonPrefixNotAllowed("192.0.2.1/32")); verifyNoMoreInteractions(updateContext); } @Test public void ipv6_not_bogon() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("route6: 2002:1:2:3::/64\norigin: AS3333\nsource: TEST")); subject.validate(update, updateContext); verifyNoMoreInteractions(updateContext); } @Test public void ipv4_not_bogon() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("route: 193.201.1.0/24\norigin: AS3333\nsource: TEST")); subject.validate(update, updateContext); verifyNoMoreInteractions(updateContext); }
### Question: ValueWithinPrefixValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetActions() throws Exception { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: ValueWithinPrefixValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void testGetTypes() throws Exception { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.ROUTE, ObjectType.ROUTE6)); }
### Question: MaintainerNameValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: AddOrRemoveRipeNccMaintainerValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired AddOrRemoveRipeNccMaintainerValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: AddOrRemoveRipeNccMaintainerValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired AddOrRemoveRipeNccMaintainerValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.values())); }
### Question: ExportFileWriter { public void write(final RpslObject object, final List<Tag> tags) throws IOException { if (exportFilter.shouldExport(object)) { final String filename = filenameStrategy.getFilename(object.getType()); if (filename != null) { final Writer writer = getWriter(filename); final RpslObject decoratedObject = decorationStrategy.decorate(object); if (decoratedObject != null) { writer.write('\n'); decoratedObject.writeTo(writer); if (!tags.isEmpty()) { writer.write('\n'); writer.write(new TagResponseObject(decoratedObject.getKey(), tags).toString()); } } } } } ExportFileWriter(final File baseDir, final FilenameStrategy filenameStrategy, final DecorationStrategy decorationStrategy, final ExportFilter exportFilter); void write(final RpslObject object, final List<Tag> tags); void close(); }### Answer: @SuppressWarnings("unchecked") @Test public void write() throws IOException { subject.write(RpslObject.parse("mntner: DEV-MNT1"), Collections.EMPTY_LIST); subject.write(RpslObject.parse("mntner: DEV-MNT2"), Collections.EMPTY_LIST); subject.write(RpslObject.parse("mntner: DEV-MNT3"), Collections.EMPTY_LIST); subject.write(RpslObject.parse("mntner: DEV-MNT4"), Collections.EMPTY_LIST); subject.write(RpslObject.parse("inetnum: 193.0.0.0 - 193.0.0.10"), Collections.EMPTY_LIST); subject.write(RpslObject.parse("route: 193.0.0.0 - 193.0.0.10\norigin: AS12"), Lists.newArrayList(new Tag(CIString.ciString("foo"), 3, "bar"))); subject.close(); final File[] files = folder.getRoot().listFiles(); Assert.assertNotNull(files); Assert.assertThat(files.length, Matchers.is(21)); for (final File file : files) { final String fileName = file.getName(); if (fileName.endsWith("mntner.gz")) { checkFile(file, "" + "mntner: DEV-MNT1\n" + "\n" + "mntner: DEV-MNT2\n" + "\n" + "mntner: DEV-MNT3\n" + "\n" + "mntner: DEV-MNT4\n"); } else if (fileName.endsWith("inetnum.gz")) { checkFile(file, "" + "inetnum: 193.0.0.0 - 193.0.0.10\n"); } else if (fileName.endsWith("route.gz")) { checkFile(file, "" + "route: 193.0.0.0 - 193.0.0.10\n" + "origin: AS12\n\n" + QueryMessages.tagInfoStart("193.0.0.0 - 193.0.0.10AS12") + QueryMessages.tagInfo("foo", "bar")); } } }
### Question: DeleteRsMaintainedObjectValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired DeleteRsMaintainedObjectValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.DELETE)); }
### Question: DeleteRsMaintainedObjectValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired DeleteRsMaintainedObjectValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.values())); }
### Question: DeleteRsMaintainedObjectValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final Subject subject = updateContext.getSubject(update); if (subject.hasPrincipal(Principal.OVERRIDE_MAINTAINER) || subject.hasPrincipal(Principal.RS_MAINTAINER)) { return; } final Set<CIString> mntBys = update.getUpdatedObject().getValuesForAttribute(AttributeType.MNT_BY); if (maintainers.isRsMaintainer(mntBys)) { updateContext.addMessage(update, UpdateMessages.authorisationRequiredForDeleteRsMaintainedObject()); } } @Autowired DeleteRsMaintainedObjectValidator(final Maintainers maintainers); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_no_rs_auth_no_rs_maintainer() { when(authSubject.hasPrincipal(Principal.RS_MAINTAINER)).thenReturn(false); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "mntner: DEV-MNT\n" + "mnt-by: DEV-MNT\n")); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(eq(update), any(Message.class)); verify(maintainers).isRsMaintainer(ciSet("DEV-MNT")); verifyNoMoreInteractions(maintainers); } @Test public void validate_no_rs_auth_rs_maintainer() { when(authSubject.hasPrincipal(Principal.RS_MAINTAINER)).thenReturn(false); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "mntner: DEV-MNT\n" + "mnt-by: DEV-MNT\n" + "mnt-by: RS-MNT\n")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.authorisationRequiredForDeleteRsMaintainedObject()); verify(maintainers).isRsMaintainer(ciSet("DEV-MNT", "RS-MNT")); verifyNoMoreInteractions(maintainers); } @Test public void validate_no_rs_auth_rs_maintainer_override() { when(authSubject.hasPrincipal(Principal.RS_MAINTAINER)).thenReturn(false); when(authSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(true); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "mntner: DEV-MNT\n" + "mnt-by: DEV-MNT\n" + "mnt-by: RS-MNT\n")); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(eq(update), any(Message.class)); verifyZeroInteractions(maintainers); } @Test public void validate_rs_auth_rs_maintainer() { when(authSubject.hasPrincipal(Principal.RS_MAINTAINER)).thenReturn(true); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "mntner: DEV-MNT\n" + "mnt-by: DEV-MNT\n" + "mnt-by: RS-MNT\n")); subject.validate(update, updateContext); verify(updateContext, never()).addMessage(eq(update), any(Message.class)); verifyZeroInteractions(maintainers); }
### Question: ReservedWordValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: ReservedWordValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); if (updatedObject == null) { return; } final CIString primaryKey = updatedObject.getKey(); if (RESERVED_WORDS.contains(primaryKey)) { updateContext.addMessage(update, UpdateMessages.reservedNameUsed(primaryKey.toLowerCase())); return; } for (Map.Entry<CIString, ObjectType> entry : RESERVED_PREFIXES.entrySet()) { if (primaryKey.startsWith(entry.getKey()) && (!updatedObject.getType().equals(entry.getValue()))) { updateContext.addMessage(update, UpdateMessages.reservedPrefixUsed(entry.getKey(), entry.getValue())); return; } } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void not_reserved_word() { mockUpdate("mntner: OWNER-MNT\nsource: TEST"); subject.validate(update, updateContext); verifyOk(); } @Test public void reserved_word_as_any_not_ok() { mockUpdate("as-set: AS-ANy\nsource: TEST"); subject.validate(update, updateContext); verifyReservedName("as-any"); } @Test public void reserved_prefix_as_not_ok() { mockUpdate("mntner: AS-TEST\nsource: TEST"); subject.validate(update, updateContext); verifyReservedPrefixUsed("as-", ObjectType.AS_SET); } @Test public void reserved_prefix_org_not_ok() { mockUpdate("mntner: ORG-TEST\nsource: TEST"); subject.validate(update, updateContext); verifyReservedPrefixUsed("org-", ObjectType.ORGANISATION); } @Test public void reserved_prefix_as_ok() { mockUpdate("as-set: AS-TEST\nsource: TEST"); subject.validate(update, updateContext); verifyOk(); }
### Question: MaintainedReferencedMaintainerPersonRolesValidator extends AbstractObjectIsMaintainedValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired MaintainedReferencedMaintainerPersonRolesValidator(final RpslObjectDao rpslObjectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: MntRoutesValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), contains(Action.CREATE, Action.MODIFY)); }
### Question: MntRoutesValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), contains(ObjectType.INET6NUM, ObjectType.INETNUM, ObjectType.ROUTE, ObjectType.ROUTE6)); }
### Question: NewKeywordValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: NewKeywordValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { if (update.hasOriginalObject()) { updateContext.addMessage(update, UpdateMessages.newKeywordAndObjectExists()); } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_create_with_existing() { when(preparedUpdate.getAction()).thenReturn(Action.CREATE); when(preparedUpdate.hasOriginalObject()).thenReturn(true); subject.validate(preparedUpdate, updateContext); verify(updateContext).addMessage(preparedUpdate, UpdateMessages.newKeywordAndObjectExists()); } @Test public void validate_create_without() { when(preparedUpdate.getAction()).thenReturn(Action.CREATE); when(preparedUpdate.hasOriginalObject()).thenReturn(false); subject.validate(preparedUpdate, updateContext); verifyZeroInteractions(updateContext); }
### Question: MaintainedReferencedPersonRolesValidator extends AbstractObjectIsMaintainedValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired MaintainedReferencedPersonRolesValidator(final RpslObjectDao rpslObjectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: SourceCommentValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: SourceCommentValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.values())); }
### Question: SourceCommentValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final String source = update.getUpdatedObject().findAttribute(SOURCE).getValue(); if (REMARK_PATTERN.matcher(source).matches()) { updateContext.addMessage(update, UpdateMessages.commentInSourceNotAllowed()); } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void source_attribute_has_comment() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("mntner: TEST-MNT\nsource: TEST # comment")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.commentInSourceNotAllowed()); } @Test public void source_attribute_has_no_comment() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("mntner: TEST-MNT\nsource: TEST")); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); }
### Question: ReferencedObjectsExistValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired ReferencedObjectsExistValidator(final RpslObjectUpdateDao rpslObjectUpdateDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: ReferencedObjectsExistValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); final Map<RpslAttribute, Set<CIString>> invalidReferences = rpslObjectUpdateDao.getInvalidReferences(updatedObject); final ObjectMessages objectMessages = updateContext.getMessages(update); for (final Map.Entry<RpslAttribute, Set<CIString>> invalidReferenceEntry : invalidReferences.entrySet()) { final RpslAttribute attribute = invalidReferenceEntry.getKey(); if (objectMessages.getMessages(attribute).getErrors().isEmpty()) { updateContext.addMessage(update, attribute, UpdateMessages.unknownObjectReferenced(StringUtils.join(invalidReferenceEntry.getValue(), ','))); } } } @Autowired ReferencedObjectsExistValidator(final RpslObjectUpdateDao rpslObjectUpdateDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_no_invalid_references() { when(rpslObjectUpdateDao.getInvalidReferences(object)).thenReturn(Collections.<RpslAttribute, Set<CIString>>emptyMap()); subject.validate(update, updateContext); verify(updateContext).getMessages(update); verifyNoMoreInteractions(updateContext); } @Test public void validate_invalid_references() { final RpslAttribute invalidAttribute = object.getAttributes().get(1); final Map<RpslAttribute, Set<CIString>> invalidReferences = Maps.newHashMap(); invalidReferences.put(invalidAttribute, invalidAttribute.getCleanValues()); when(updateContext.getMessages(update)).thenReturn(new ObjectMessages()); when(rpslObjectUpdateDao.getInvalidReferences(object)).thenReturn(invalidReferences); subject.validate(update, updateContext); verify(updateContext).addMessage(update, invalidAttribute, UpdateMessages.unknownObjectReferenced("ADMIN_NC")); }
### Question: PasswordFilter { public static String filterPasswordsInContents(final String contents) { String result = contents; if( contents != null ) { final Matcher matcher = PASSWORD_PATTERN_FOR_CONTENT.matcher(contents); result = replacePassword(matcher); } return result; } static String filterPasswordsInContents(final String contents); static String filterPasswordsInUrl(final String url); }### Answer: @Test public void testFilterPasswordsInMessage() { final String input = "" + "red: adsfasdf\n" + "blue: asdfasdfasdf\n" + "yellow%3A++asdfasdfasdf\n" + "green: asdfasdfasdf # password: test\n" + "purple: password\n" + "password: test1 \n" + "password:test2\n" + "password: test3\n" + "password%3A++test4\n" + "password%3A++test5\n" + "delete: adsf\n"; assertThat(PasswordFilter.filterPasswordsInContents(input), containsString("" + "red: adsfasdf\n" + "blue: asdfasdfasdf\n" + "yellow%3A++asdfasdfasdf\n" + "green: asdfasdfasdf # password: test\n" + "purple: password\n" + "password:FILTERED\n" + "password:FILTERED\n" + "password:FILTERED\n" + "password%3AFILTERED\n" + "password%3AFILTERED\n" + "delete: adsf\n")); } @Test public void testFilterOverridePasswordsInMessage() { final String input = "" + "red: adsfasdf\n" + "blue: asdfasdfasdf\n" + "yellow%3A++asdfasdfasdf\n" + "green: asdfasdfasdf # override: test\n" + "purple: override\n" + "override:user,pass\n" + "override:user,pass,reason\n" + "override: user,pass\n" + "override%3A++user,pass\n" + "delete: adsf\n"; assertThat(PasswordFilter.filterPasswordsInContents(input), containsString("" + "red: adsfasdf\n" + "blue: asdfasdfasdf\n" + "yellow%3A++asdfasdfasdf\n" + "green: asdfasdfasdf # override: test\n" + "purple: override\n" + "override:user,FILTERED\n" + "override:user,FILTERED,reason\n" + "override:user,FILTERED\n" + "override%3A++user,FILTERED\n" + "delete: adsf\n")); } @Test public void testFilterOverrideAndPasswordsInMessage() { final String input = "" + "red: adsfasdf\n" + "purple: override\n" + "override:user,pass\n" + "password:test\n"; assertThat(PasswordFilter.filterPasswordsInContents(input), containsString("" + "red: adsfasdf\n" + "purple: override\n" + "override:user,FILTERED\n" + "password:FILTERED")); }
### Question: ExcludedEmailValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final Subject subject = updateContext.getSubject(update); if (subject.hasPrincipal(Principal.OVERRIDE_MAINTAINER) || subject.hasPrincipal(Principal.RS_MAINTAINER)) { return; } final RpslObject updatedObject = update.getUpdatedObject(); for (final RpslAttribute attribute : updatedObject.getAttributes()) { if (EMAIL_ATTRIBUTES.contains(attribute.getType())) { try { final CIString address = CIString.ciString(getAddress(attribute.getValue())); if (excludedEmailAddresses.contains(address)) { updateContext.addMessage(update, attribute, UpdateMessages.emailAddressCannotBeUsed(address)); } } catch (IllegalArgumentException e) { LOGGER.debug("Skipped {} attribute in {} due to: {}", attribute.getType().getName(), updatedObject.getKey(), e.getMessage()); } } } } @Autowired ExcludedEmailValidator( @Value("#{'${email.excluded:}'.split(',')}") final List<String> excludedEmailAddresses); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_allowed_address() { when(preparedUpdate.getUpdatedObject()).thenReturn(RpslObject.parse("mntner: OWNER-MNT\nupd-to: user@host.org\nsource: TEST")); excludedEmailValidator.validate(preparedUpdate, updateContext); verify(updateContext, never()).addMessage(Matchers.<Update>anyObject(), Matchers.<RpslAttribute>anyObject(), Matchers.<Message>anyObject()); } @Test public void validate_excluded_address() { when(preparedUpdate.getUpdatedObject()).thenReturn(RpslObject.parse("mntner: OWNER-MNT\nupd-to: ripe-dbm@ripe.net\nsource: TEST")); excludedEmailValidator.validate(preparedUpdate, updateContext); verify(updateContext).addMessage(Matchers.<Update>anyObject(), Matchers.<RpslAttribute>anyObject(), Matchers.<Message>anyObject()); } @Test public void validate_excluded_address_is_case_insensitive() { when(preparedUpdate.getUpdatedObject()).thenReturn(RpslObject.parse("mntner: OWNER-MNT\nupd-to: RIPE-DbM@ripe.net\nsource: TEST")); excludedEmailValidator.validate(preparedUpdate, updateContext); verify(updateContext).addMessage(Matchers.<Update>anyObject(), Matchers.<RpslAttribute>anyObject(), Matchers.<Message>anyObject()); } @Test public void validate_excluded_name_and_address() { when(preparedUpdate.getUpdatedObject()).thenReturn(RpslObject.parse("mntner: OWNER-MNT\nupd-to: RIPE DBM <ripe-dbm@ripe.net>\nsource: TEST")); excludedEmailValidator.validate(preparedUpdate, updateContext); verify(updateContext).addMessage(Matchers.<Update>anyObject(), Matchers.<RpslAttribute>anyObject(), Matchers.<Message>anyObject()); }
### Question: ObjectReferencedValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired ObjectReferencedValidator(final RpslObjectUpdateDao rpslObjectUpdateDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.DELETE)); }
### Question: ObjectReferencedValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { if (!update.hasOriginalObject() || update.getType().equals(ObjectType.AUT_NUM)) { return; } if (rpslObjectUpdateDao.isReferenced(update.getReferenceObject())) { updateContext.addMessage(update, UpdateMessages.objectInUse(update.getReferenceObject())); } } @Autowired ObjectReferencedValidator(final RpslObjectUpdateDao rpslObjectUpdateDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_no_original_object() { subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void validate_not_referenced() { final RpslObject object = RpslObject.parse("mntner: TST-MNT"); when(update.getReferenceObject()).thenReturn(object); when(rpslObjectUpdateDao.getInvalidReferences(object)).thenReturn(Collections.<RpslAttribute, Set<CIString>>emptyMap()); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void validate_referenced() { final RpslObject object = RpslObject.parse("mntner: TST-MNT\nadmin-c: ADMIN-NC"); when(update.getType()).thenReturn(ObjectType.MNTNER); when(update.hasOriginalObject()).thenReturn(true); when(update.getReferenceObject()).thenReturn(object); when(rpslObjectUpdateDao.isReferenced(object)).thenReturn(true); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.objectInUse(object)); } @Test public void validate_referenced_autnum() { final RpslObject object = RpslObject.parse("aut-num: AS1"); when(update.getType()).thenReturn(ObjectType.AUT_NUM); when(update.hasOriginalObject()).thenReturn(true); when(update.getReferenceObject()).thenReturn(object); when(rpslObjectUpdateDao.isReferenced(object)).thenReturn(true); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); }
### Question: CountryValidator implements BusinessRuleValidator { @Override public void validate(PreparedUpdate update, UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); if (!updatedObject.containsAttribute(AttributeType.COUNTRY)) { return; } final Set<CIString> countryCodes = countryCodeRepository.getCountryCodes(); for (final RpslAttribute attribute : updatedObject.findAttributes(AttributeType.COUNTRY)) { if (!countryCodes.contains(attribute.getCleanValue())) { updateContext.addMessage(update, attribute, UpdateMessages.countryNotRecognised(attribute.getCleanValue())); } } } @Autowired CountryValidator(final CountryCodeRepository countryCodeRepository); @Override void validate(PreparedUpdate update, UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void valid_country() { when(repository.getCountryCodes()).thenReturn(CIString.ciSet("DK", "UK")); final RpslObject rpslObject = RpslObject.parse("inetnum: 193.0/32\ncountry:DK"); when(update.getUpdatedObject()).thenReturn(rpslObject); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void invalid_country() { when(repository.getCountryCodes()).thenReturn(CIString.ciSet("DK", "UK")); final RpslObject rpslObject = RpslObject.parse("inetnum: 193.0/32\ncountry:AB"); when(update.getUpdatedObject()).thenReturn(rpslObject); subject.validate(update, updateContext); verify(updateContext).addMessage(update, rpslObject.findAttribute(AttributeType.COUNTRY), UpdateMessages.countryNotRecognised(ciString("AB"))); }
### Question: LanguageValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final RpslObject updatedObject = update.getUpdatedObject(); if (!updatedObject.containsAttribute(AttributeType.LANGUAGE)) { return; } final Set<CIString> languageCodes = languageRepository.getLanguageCodes(); for (final RpslAttribute attribute : updatedObject.findAttributes(AttributeType.LANGUAGE)) { if (!languageCodes.contains(attribute.getCleanValue())) { updateContext.addMessage(update, UpdateMessages.languageNotRecognised(attribute.getCleanValue())); } } } @Autowired LanguageValidator(final LanguageCodeRepository repository); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void valid_language() { when(repository.getLanguageCodes()).thenReturn(ciSet("DK", "UK")); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 193.0/32\nlanguage:DK")); subject.validate(update, updateContext); verifyZeroInteractions(updateContext); } @Test public void invalid_language() { when(repository.getLanguageCodes()).thenReturn(ciSet("DK", "UK")); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("inetnum: 193.0/32\nlanguage:AB")); subject.validate(update, updateContext); verify(updateContext).addMessage(update, UpdateMessages.languageNotRecognised("AB")); }
### Question: MemberOfValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired MemberOfValidator(final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions().size(), is(2)); assertThat(subject.getActions().contains(Action.MODIFY), is(true)); assertThat(subject.getActions().contains(Action.CREATE), is(true)); }
### Question: MemberOfValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired MemberOfValidator(final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes().size(), is(4)); assertThat(subject.getTypes().contains(ObjectType.AUT_NUM), is(true)); assertThat(subject.getTypes().contains(ObjectType.ROUTE), is(true)); assertThat(subject.getTypes().contains(ObjectType.ROUTE6), is(true)); assertThat(subject.getTypes().contains(ObjectType.INET_RTR), is(true)); }
### Question: ObjectMismatchValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.DELETE)); }
### Question: SelfReferencePreventionValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.MODIFY, Action.CREATE)); }
### Question: SelfReferencePreventionValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), contains(ObjectType.ROLE)); }
### Question: SelfReferencePreventionValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { errorOnSelfReference(update, updateContext, AttributeType.ADMIN_C); errorOnSelfReference(update, updateContext, AttributeType.TECH_C); } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void not_self_referenced() { when(preparedUpdate.getUpdate()).thenReturn(update); when(update.getSubmittedObject()).thenReturn(RpslObject.parse("role: Some Role\nnic-hdl: NIC-TEST\nadmin-c: OTHER-TEST\ntech-c: TECH-TEST")); subject.validate(preparedUpdate, updateContext); verify(updateContext, never()).addMessage(preparedUpdate, UpdateMessages.selfReferenceError(AttributeType.ADMIN_C)); verify(updateContext, never()).addMessage(preparedUpdate, UpdateMessages.selfReferenceError(AttributeType.TECH_C)); } @Test public void self_referenced_adminC() { final RpslObject role = RpslObject.parse("role: Some Role\nnic-hdl: NIC-TEST\nadmin-c: NIC-TEST\ntech-c: TECH-TEST"); when(preparedUpdate.getUpdate()).thenReturn(update); when(update.getSubmittedObject()).thenReturn(role); subject.validate(preparedUpdate, updateContext); verify(updateContext, times(1)).addMessage(preparedUpdate, role.findAttribute(AttributeType.ADMIN_C), UpdateMessages.selfReferenceError(AttributeType.ADMIN_C)); verify(updateContext, never()).addMessage(preparedUpdate, role.findAttribute(AttributeType.TECH_C), UpdateMessages.selfReferenceError(AttributeType.TECH_C)); } @Test public void self_referenced_techC() { final RpslObject role = RpslObject.parse("role: Some Role\nnic-hdl: NIC-TEST\nadmin-c: OTHER-TEST\ntech-c: NIC-TEST"); when(preparedUpdate.getUpdate()).thenReturn(update); when(update.getSubmittedObject()).thenReturn(role); subject.validate(preparedUpdate, updateContext); verify(updateContext, never()).addMessage(preparedUpdate, role.findAttribute(AttributeType.ADMIN_C), UpdateMessages.selfReferenceError(AttributeType.ADMIN_C)); verify(updateContext, times(1)).addMessage(preparedUpdate, role.findAttribute(AttributeType.TECH_C), UpdateMessages.selfReferenceError(AttributeType.TECH_C)); } @Test public void self_referenced_techC_adminC() { final RpslObject role = RpslObject.parse("role: Some Role\nnic-hdl: NIC-TEST\nadmin-c: NIC-TEST\ntech-c: NIC-TEST"); when(preparedUpdate.getUpdate()).thenReturn(update); when(update.getSubmittedObject()).thenReturn(role); subject.validate(preparedUpdate, updateContext); verify(updateContext, times(1)).addMessage(preparedUpdate, role.findAttribute(AttributeType.ADMIN_C), UpdateMessages.selfReferenceError(AttributeType.ADMIN_C)); verify(updateContext, times(1)).addMessage(preparedUpdate, role.findAttribute(AttributeType.TECH_C), UpdateMessages.selfReferenceError(AttributeType.TECH_C)); }
### Question: MustKeepAbuseMailboxIfReferencedValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired MustKeepAbuseMailboxIfReferencedValidator(final RpslObjectUpdateDao updateObjectDao, final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.DELETE, Action.MODIFY)); }
### Question: MustKeepAbuseMailboxIfReferencedValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired MustKeepAbuseMailboxIfReferencedValidator(final RpslObjectUpdateDao updateObjectDao, final RpslObjectDao objectDao); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), contains(ObjectType.ROLE)); }
### Question: KeycertValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired KeycertValidator(final KeyWrapperFactory keyWrapperFactory, final X509AutoKeyFactory x509AutoKeyFactory); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: KeycertValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired KeycertValidator(final KeyWrapperFactory keyWrapperFactory, final X509AutoKeyFactory x509AutoKeyFactory); @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.KEY_CERT)); }
### Question: IpDomainUniqueHierarchyValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Autowired IpDomainUniqueHierarchyValidator(final Ipv4DomainTree ipv4DomainTree, final Ipv6DomainTree ipv6DomainTree); @SuppressWarnings("unchecked") @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: IpDomainUniqueHierarchyValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Autowired IpDomainUniqueHierarchyValidator(final Ipv4DomainTree ipv4DomainTree, final Ipv6DomainTree ipv6DomainTree); @SuppressWarnings("unchecked") @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.DOMAIN)); }
### Question: ChannelUtil { public static InetAddress getRemoteAddress(final Channel channel) { final InetAddress inetAddress = ((InetSocketAddress) channel.getRemoteAddress()).getAddress(); if (inetAddress instanceof Inet6Address) { try { return InetAddress.getByAddress(inetAddress.getAddress()); } catch (UnknownHostException ignored) { LOGGER.debug("{}: {}", ignored.getClass().getName(), ignored.getMessage()); } } return inetAddress; } private ChannelUtil(); static InetAddress getRemoteAddress(final Channel channel); static final Charset BYTE_ENCODING; }### Answer: @Test public void shouldGetRemoteAddressFromChannel() { InetAddress remoteAddress = ChannelUtil.getRemoteAddress(new StubbedChannel("192.168.0.1")); assertThat(remoteAddress.getHostAddress(), is("192.168.0.1")); } @Test public void shouldGetRemoteAddressFromIpv6AddressChannel() { InetAddress remoteAddress = ChannelUtil.getRemoteAddress(new StubbedChannel("2001:67c:2e8:13:1146:e6f4:bfd7:c324")); assertThat(remoteAddress.getHostAddress(), is("2001:67c:2e8:13:1146:e6f4:bfd7:c324")); } @Test public void shouldGetRemoteAddressFromIpv6WithoutInterface() { InetAddress remoteAddress = ChannelUtil.getRemoteAddress(new StubbedChannel("2001:67c:2e8:13:1146:e6f4:bfd7:c324", true)); assertThat(remoteAddress.getHostAddress(), is("2001:67c:2e8:13:1146:e6f4:bfd7:c324")); }
### Question: DatabaseTextExport implements DailyScheduledTask { @Override @Scheduled(cron = "0 0 0 * * *") @SchedulerLock(name = "DatabaseTextExport") public void run() { rpslObjectsExporter.export(); } @Autowired DatabaseTextExport(final RpslObjectsExporter rpslObjectsExporter); @Override @Scheduled(cron = "0 0 0 * * *") @SchedulerLock(name = "DatabaseTextExport") void run(); }### Answer: @Test public void run() { subject.run(); verify(rpslObjectsExporter).export(); }
### Question: EnumDomainAuthorisationValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE)); }
### Question: EnumDomainAuthorisationValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.DOMAIN)); }
### Question: EnumDomainAuthorisationValidator implements BusinessRuleValidator { @Override public void validate(final PreparedUpdate update, final UpdateContext updateContext) { final Subject subject = updateContext.getSubject(update); if (subject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)) { return; } final RpslObject rpslObject = update.getUpdatedObject(); final CIString domainString = rpslObject.getKey(); final Domain domain = Domain.parse(domainString); if (domain.getType() == Domain.Type.E164) { if (!subject.hasPrincipal(Principal.ENUM_MAINTAINER)) { updateContext.addMessage(update, UpdateMessages.authorisationRequiredForEnumDomain()); } } } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void validate_non_enum() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "domain: 200.193.193.in-addr.arpa")); subject.validate(update, updateContext); verify(updateContext).getSubject(any(UpdateContainer.class)); verifyNoMoreInteractions(updateContext); } @Test public void validate_override() { when(authSubject.hasPrincipal(Principal.OVERRIDE_MAINTAINER)).thenReturn(true); when(update.getUpdatedObject()).thenReturn(RpslObject.parse("domain: 2.1.2.1.5.5.5.2.0.2.1.e164.arpa")); when(authSubject.hasPrincipal(Principal.ENUM_MAINTAINER)).thenReturn(false); subject.validate(update, updateContext); verify(updateContext).getSubject(any(UpdateContainer.class)); verifyNoMoreInteractions(updateContext); } @Test public void validate_enum_no_enum_maintainer() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "domain: 2.1.2.1.5.5.5.2.0.2.1.e164.arpa")); when(authSubject.hasPrincipal(Principal.ENUM_MAINTAINER)).thenReturn(false); subject.validate(update, updateContext); verify(authSubject).hasPrincipal(Principal.ENUM_MAINTAINER); verify(updateContext).addMessage(update, UpdateMessages.authorisationRequiredForEnumDomain()); } @Test public void validate_enum_with_enum_maintainer() { when(update.getUpdatedObject()).thenReturn(RpslObject.parse("" + "domain: 2.1.2.1.5.5.5.2.0.2.1.e164.arpa")); when(authSubject.hasPrincipal(Principal.ENUM_MAINTAINER)).thenReturn(true); subject.validate(update, updateContext); verify(authSubject).hasPrincipal(Principal.ENUM_MAINTAINER); verify(updateContext, never()).addMessage(update, UpdateMessages.authorisationRequiredForEnumDomain()); }
### Question: NServerValidator implements BusinessRuleValidator { @Override public ImmutableList<Action> getActions() { return ACTIONS; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getActions() { assertThat(subject.getActions(), containsInAnyOrder(Action.CREATE, Action.MODIFY)); }
### Question: NServerValidator implements BusinessRuleValidator { @Override public ImmutableList<ObjectType> getTypes() { return TYPES; } @Override void validate(final PreparedUpdate update, final UpdateContext updateContext); @Override ImmutableList<Action> getActions(); @Override ImmutableList<ObjectType> getTypes(); }### Answer: @Test public void getTypes() { assertThat(subject.getTypes(), containsInAnyOrder(ObjectType.DOMAIN)); }
### Question: MaintenanceModeJmx extends JmxBase { @ManagedOperation(description = "Sets maintenance mode") @ManagedOperationParameters({ @ManagedOperationParameter(name = "mode", description = "Access rights for 'world,trusted' (values: FULL/READONLY/NONE) (e.g. 'none,full')"), }) public void setMaintenanceMode(final String mode) { invokeOperation("Set maintenance mode", mode, new Callable<Void>() { @Override public Void call() throws Exception { maintenanceMode.set(mode); return null; } }); } @Autowired MaintenanceModeJmx(final MaintenanceMode maintenanceMode, IpRanges ipRanges); @ManagedOperation(description = "Sets maintenance mode") @ManagedOperationParameters({ @ManagedOperationParameter(name = "mode", description = "Access rights for 'world,trusted' (values: FULL/READONLY/NONE) (e.g. 'none,full')"), }) void setMaintenanceMode(final String mode); @ManagedOperation(description = "Sets trusted range (ipranges.trusted)") @ManagedOperationParameters({ @ManagedOperationParameter(name = "ranges", description = "Comma-separated list of IP prefixes/ranges (e.g. '10/8,::1/64)"), }) void setTrustedIpRanges(final String ranges); }### Answer: @Test public void maintenance_mode_set() { subject.setMaintenanceMode("FULL, FULL"); verify(maintenanceMode, times(1)).set(anyString()); }
### Question: CountryCodeRepository { public Set<CIString> getCountryCodes() { return countryCodes; } @Autowired CountryCodeRepository(@Value("${whois.countrycodes}") final String[] countryCodes); Set<CIString> getCountryCodes(); }### Answer: @Test public void getCountryCodes() { assertThat(subject.getCountryCodes(), containsInAnyOrder(ciString("NL"), ciString("EN"))); } @Test(expected = UnsupportedOperationException.class) public void getCountryCodes_immutable() { subject.getCountryCodes().add(ciString("DE")); }
### Question: X509AutoKeyFactory implements AutoKeyFactory<X509KeycertId> { @Override public AttributeType getAttributeType() { return AttributeType.KEY_CERT; } @Autowired X509AutoKeyFactory(final X509Repository repository, @Value("${whois.source}") String source); @Override AttributeType getAttributeType(); @Override boolean isKeyPlaceHolder(final CharSequence s); @Override CIString getKeyPlaceholder(final CharSequence s); @Override X509KeycertId claim(final String key); @Override X509KeycertId generate(final String keyPlaceHolder, final RpslObject object); @Override boolean isApplicableFor(final RpslObject object); }### Answer: @Test public void attributeType() { assertThat(subject.getAttributeType(), is(AttributeType.KEY_CERT)); }
### Question: X509AutoKeyFactory implements AutoKeyFactory<X509KeycertId> { @Override public boolean isKeyPlaceHolder(final CharSequence s) { return AUTO_PATTERN.matcher(s).matches(); } @Autowired X509AutoKeyFactory(final X509Repository repository, @Value("${whois.source}") String source); @Override AttributeType getAttributeType(); @Override boolean isKeyPlaceHolder(final CharSequence s); @Override CIString getKeyPlaceholder(final CharSequence s); @Override X509KeycertId claim(final String key); @Override X509KeycertId generate(final String keyPlaceHolder, final RpslObject object); @Override boolean isApplicableFor(final RpslObject object); }### Answer: @Test public void correct_keyPlaceHolder() { assertThat(subject.isKeyPlaceHolder("AUTO-100"), is(true)); } @Test public void incorrect_keyPlaceHolder() { assertThat(subject.isKeyPlaceHolder("AUTO-100NL"), is(false)); assertThat(subject.isKeyPlaceHolder("AUTO-"), is(false)); assertThat(subject.isKeyPlaceHolder("AUTO"), is(false)); assertThat(subject.isKeyPlaceHolder("AUTO-100-NL"), is(false)); }
### Question: X509AutoKeyFactory implements AutoKeyFactory<X509KeycertId> { @Override public X509KeycertId claim(final String key) throws ClaimException { throw new ClaimException(ValidationMessages.syntaxError(key, "must be AUTO-nnn for create")); } @Autowired X509AutoKeyFactory(final X509Repository repository, @Value("${whois.source}") String source); @Override AttributeType getAttributeType(); @Override boolean isKeyPlaceHolder(final CharSequence s); @Override CIString getKeyPlaceholder(final CharSequence s); @Override X509KeycertId claim(final String key); @Override X509KeycertId generate(final String keyPlaceHolder, final RpslObject object); @Override boolean isApplicableFor(final RpslObject object); }### Answer: @Test(expected = ClaimException.class) public void claim_not_supported() throws ClaimException { subject.claim("irrelevant here"); }
### Question: X509AutoKeyFactory implements AutoKeyFactory<X509KeycertId> { @Override public X509KeycertId generate(final String keyPlaceHolder, final RpslObject object) { Validate.notEmpty(object.getValueForAttribute(AttributeType.KEY_CERT).toString(), "Name must not be empty"); final Matcher matcher = AUTO_PATTERN.matcher(keyPlaceHolder); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid key request: " + keyPlaceHolder); } return repository.claimNextAvailableIndex(SPACE, source.toString()); } @Autowired X509AutoKeyFactory(final X509Repository repository, @Value("${whois.source}") String source); @Override AttributeType getAttributeType(); @Override boolean isKeyPlaceHolder(final CharSequence s); @Override CIString getKeyPlaceholder(final CharSequence s); @Override X509KeycertId claim(final String key); @Override X509KeycertId generate(final String keyPlaceHolder, final RpslObject object); @Override boolean isApplicableFor(final RpslObject object); }### Answer: @Test(expected = IllegalArgumentException.class) public void generate_invalid_placeHolder() { subject.generate("AUTO", RpslObject.parse("key-cert: AUTO")); } @Test public void generate_correct() { when(x509Repository.claimNextAvailableIndex("X509", "TEST")).thenReturn(new X509KeycertId("X509", 2, "TEST")); final X509KeycertId generated = subject.generate("AuTO-1", RpslObject.parse("kEy-Cert: auto\nremarks: optional")); assertThat(generated.toString(), is("X509-2")); }