@Override public void removeAliasDomainMapping(MappingSource source, Domain realDomain) throws RecipientRewriteTableException { LOGGER.info("Remove domain mapping: {} => {}", source.asDomain().map(Domain::asString).orElse("null"), realDomain); removeMapping(source, Mapping.domain(realDomain)); }
@Override public void addAliasDomainMapping(MappingSource source, Domain realDomain) throws RecipientRewriteTableException { LOGGER.info("Add domain mapping: {} => {}", source.asDomain().map(Domain::asString).orElse("null"), realDomain); addMapping(source, Mapping.domain(realDomain)); }
@Test public void fromRawStringShouldNotUseColonDelimiterWhenValueStartsWithDomain() { MappingsImpl actual = MappingsImpl.fromRawString("domain:test"); assertThat(actual).containsOnly(Mapping.domain(Domain.of("test"))); }
@Test public void builderShouldPutDomainAliasFirstWhenVariousMappings() { Mapping addressMapping = Mapping.address("aaa"); Mapping errorMapping = Mapping.error("error"); Mapping domainMapping = Mapping.domain(Domain.of("domain")); Mapping domain2Mapping = Mapping.domain(Domain.of("domain2")); MappingsImpl mappingsImpl = MappingsImpl.builder() .add(domainMapping) .add(addressMapping) .add(errorMapping) .add(domain2Mapping) .build(); assertThat(mappingsImpl).containsExactly(domainMapping, domain2Mapping, addressMapping, errorMapping); }
@Test void getSpecificDomainMappingShouldReturnDomainMappings() throws RecipientRewriteTableException { String domain = "abc.com"; String aliasDomain = "a.com"; Mappings mappings = MappingsImpl.fromMappings(Mapping.domain(Domain.of(aliasDomain))); when(recipientRewriteTable.getUserDomainMappings(any())).thenReturn(mappings); List<String> body = when() .get(domain) .then() .contentType(ContentType.JSON) .statusCode(HttpStatus.OK_200) .extract() .jsonPath() .getList("."); assertThat(body).contains(aliasDomain); } }
@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 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); } }
@Test public void testAliasDomainMapping() throws ErrorMappingException, RecipientRewriteTableException { String domain = "realdomain"; Domain aliasDomain = Domain.of("aliasdomain"); String user = "user"; String user2 = "user2"; assertThat(virtualUserTable.getAllMappings()).describedAs("No mappings").isEmpty(); virtualUserTable.addMapping(MappingSource.fromDomain(aliasDomain), Mapping.address(user2 + "@" + domain)); virtualUserTable.addMapping(MappingSource.fromDomain(aliasDomain), Mapping.domain(Domain.of(domain))); assertThat(virtualUserTable.getMappings(user, aliasDomain)) .describedAs("Domain mapped as first, Address mapped as second") .isEqualTo(MappingsImpl.builder() .add(Mapping.address(user + "@" + domain)) .add(Mapping.address(user2 + "@" + domain)) .build()); virtualUserTable.removeMapping(MappingSource.fromDomain(aliasDomain), Mapping.address(user2 + "@" + domain)); virtualUserTable.removeMapping(MappingSource.fromDomain(aliasDomain), Mapping.domain(Domain.of(domain))); }
@Test void getForwardShouldReturnNotFoundWhenNonForwardMappings() { memoryRecipientRewriteTable.addMapping( MappingSource.fromDomain(DOMAIN), Mapping.domain(Domain.of("target.tld"))); Map<String, Object> errors = when() .get(ALICE) .then() .statusCode(HttpStatus.NOT_FOUND_404) .contentType(ContentType.JSON) .extract() .body() .jsonPath() .getMap("."); assertThat(errors) .containsEntry("statusCode", HttpStatus.NOT_FOUND_404) .containsEntry("type", "InvalidArgument") .containsEntry("message", "The forward does not exist"); }
@Test void getGroupShouldReturnNotFoundWhenNonGroupMappings() { memoryRecipientRewriteTable.addMapping( MappingSource.fromDomain(DOMAIN), Mapping.domain(Domain.of("target.tld"))); Map<String, Object> errors = when() .get(GROUP1) .then() .statusCode(HttpStatus.NOT_FOUND_404) .contentType(ContentType.JSON) .extract() .body() .jsonPath() .getMap("."); assertThat(errors) .containsEntry("statusCode", HttpStatus.NOT_FOUND_404) .containsEntry("type", "InvalidArgument") .containsEntry("message", "The group does not exist"); }
@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); }