@Override public void removeRegexMapping(MappingSource source, String regex) throws RecipientRewriteTableException { LOGGER.info("Remove regex mapping => {} for source: {}", regex, source.asString()); removeMapping(source, Mapping.regex(regex)); }
@Override public void addRegexMapping(MappingSource source, String regex) throws RecipientRewriteTableException { try { Pattern.compile(regex); } catch (PatternSyntaxException e) { throw new RecipientRewriteTableException("Invalid regex: " + regex, e); } Mapping mapping = Mapping.regex(regex); checkDuplicateMapping(source, mapping); LOGGER.info("Add regex mapping => {} for source {}", regex, source.asString()); addMapping(source, mapping); }
@Test public void fromRawStringShouldNotUseColonDelimiterWhenValueStartsWithRegex() { MappingsImpl actual = MappingsImpl.fromRawString("regex:test"); assertThat(actual).containsOnly(Mapping.regex("test")); }
@Test public void containsShouldReturnFalseWhenNoMatchingMapping() { MappingsImpl mappings = MappingsImpl.builder().add(Mapping.regex("toto")).build(); assertThat(mappings.contains(Mapping.Type.Error)).isFalse(); }
@Test public void containsShouldReturnTrueWhenMatchingMapping() { MappingsImpl mappings = MappingsImpl.builder().add(Mapping.regex("toto")).build(); assertThat(mappings.contains(Mapping.Type.Regex)).isTrue(); }
@Test(expected = NullPointerException.class) public void containsShouldThrowWhenNull() { MappingsImpl mappings = MappingsImpl.builder().add(Mapping.regex("toto")).build(); assertThat(mappings.contains((Mapping.Type)null)); }
@Test(expected = NullPointerException.class) public void selectShouldThrowWhenNull() { MappingsImpl mappings = MappingsImpl.builder().add(Mapping.regex("toto")).build(); assertThat(mappings.select((Mapping.Type)null)); }
@Test(expected = NullPointerException.class) public void excludeShouldThrowWhenNull() { MappingsImpl mappings = MappingsImpl.builder().add(Mapping.regex("toto")).build(); assertThat(mappings.exclude((Mapping.Type)null)); }
@Test public void selectShouldReturnMatchingElementsInOrderWhenMatchingMapping() { MappingsImpl mappings = MappingsImpl.builder() .add(Mapping.regex("toto")) .add(Mapping.address("toto")) .add(Mapping.domain(Domain.of("domain"))) .add(Mapping.regex("tata")) .build(); MappingsImpl expected = MappingsImpl.builder() .add(Mapping.regex("toto")) .add(Mapping.regex("tata")) .build(); assertThat(mappings.select(Mapping.Type.Regex)).isEqualTo(expected); }
@Test public void testStoreAndGetMappings() throws ErrorMappingException, RecipientRewriteTableException { Domain domain = Domain.of("test"); virtualUserTable.addMapping(MappingSource.fromDomain(domain), Mapping.regex("prefix_.*:admin@test")); assertThat(virtualUserTable.getMappings("prefix_abc", domain)).isNotEmpty(); }
@Test public void addMappingShouldNotThrowWhenMappingAlreadyExistsWithAnOtherType() throws Exception { String user = "test"; Domain domain = Domain.LOCALHOST; String address = "test@localhost2"; MappingSource source = MappingSource.fromUser(user, domain); virtualUserTable.addMapping(source, Mapping.address(address)); virtualUserTable.addMapping(source, Mapping.regex(address)); assertThat(virtualUserTable.getMappings(user, domain)).hasSize(2); }
@Test public void getAllMappingsShouldListAllEntries() throws Exception { String user = "test"; String user2 = "test2"; String regex = "(.*)@localhost"; String regex2 = "(.+)@test"; MappingSource source1 = MappingSource.fromUser(user, Domain.LOCALHOST); MappingSource source2 = MappingSource.fromUser(user2, Domain.LOCALHOST); virtualUserTable.addMapping(source1, Mapping.regex(regex)); virtualUserTable.addMapping(source1, Mapping.regex(regex2)); virtualUserTable.addMapping(source2, Mapping.address(user + "@" + Domain.LOCALHOST.asString())); assertThat(virtualUserTable.getAllMappings()) .describedAs("One mappingline") .containsOnly( Pair.of(source1, MappingsImpl.builder() .add(Mapping.regex(regex)) .add(Mapping.regex(regex2)) .build()), Pair.of(source2, MappingsImpl.builder() .add(Mapping.address(user + "@" + Domain.LOCALHOST.asString())) .build())); }
@Test public void testStoreAndRetrieveRegexMapping() throws ErrorMappingException, RecipientRewriteTableException { String user = "test"; Domain domain = Domain.LOCALHOST; MappingSource source = MappingSource.fromUser(user, domain); // String regex = "(.*):{$1}@localhost"; // String regex2 = "(.+):{$1}@test"; String regex = "(.*)@localhost"; String regex2 = "(.+)@test"; String invalidRegex = ".*):"; assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); virtualUserTable.addMapping(source, Mapping.regex(regex)); virtualUserTable.addMapping(source, Mapping.regex(regex2)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("Two mappings").hasSize(2); assertThat(virtualUserTable.getAllMappings()).describedAs("One mappingline").hasSize(1); virtualUserTable.removeMapping(source, Mapping.regex(regex)); assertThatThrownBy(() -> virtualUserTable.addRegexMapping(source, invalidRegex)) .describedAs("Invalid Mapping throw exception") .isInstanceOf(RecipientRewriteTableException.class); virtualUserTable.removeMapping(source, Mapping.regex(regex2)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); assertThat(virtualUserTable.getAllMappings()).describedAs("No mapping").isEmpty(); }
@Test public void builderShouldPutDomainAliasFirstThenForwardWhenVariousMappings() { Mapping regexMapping = Mapping.regex("regex"); Mapping forwardMapping = Mapping.forward("forward"); Mapping domainMapping = Mapping.domain(Domain.of("domain")); MappingsImpl mappingsImpl = MappingsImpl.builder() .add(regexMapping) .add(forwardMapping) .add(domainMapping) .build(); assertThat(mappingsImpl).containsExactly(domainMapping, forwardMapping, regexMapping); }
@Test public void selectShouldReturnEmptyCollectionWhenNoMatchingMapping() { MappingsImpl mappings = MappingsImpl.builder() .add(Mapping.regex("toto")) .add(Mapping.address("toto")) .add(Mapping.address("tata")) .build(); assertThat(mappings.select(Mapping.Type.Domain)).isEqualTo(MappingsImpl.empty()); }
@Test public void excludeShouldReturnNonMatchingElementsInOrderWhenNonMatchingMapping() { MappingsImpl mappings = MappingsImpl.builder() .add(Mapping.regex("toto")) .add(Mapping.address("toto")) .add(Mapping.domain(Domain.of("domain"))) .add(Mapping.regex("tata")) .build(); MappingsImpl expected = MappingsImpl.builder() .add(Mapping.address("toto")) .add(Mapping.domain(Domain.of("domain"))) .build(); assertThat(mappings.exclude(Mapping.Type.Regex)).isEqualTo(expected); }
@Test public void builderShouldPutGroupsBetweenDomainAndForward() { Mapping regexMapping = Mapping.regex("regex"); Mapping forwardMapping = Mapping.forward("forward"); Mapping domainMapping = Mapping.domain(Domain.of("domain")); Mapping groupMapping = Mapping.group("group"); MappingsImpl mappingsImpl = MappingsImpl.builder() .add(regexMapping) .add(forwardMapping) .add(domainMapping) .add(groupMapping) .build(); assertThat(mappingsImpl).containsExactly(domainMapping, groupMapping, forwardMapping, regexMapping); } }