@Override public Mappings getUserDomainMappings(MappingSource source) throws RecipientRewriteTableException { return MappingsImpl.empty(); }
@Override protected Mappings mapAddress(String user, Domain domain) { return OptionalUtils.orSuppliers( () -> retrieveMappings(MappingSource.fromUser(user, domain)), () -> retrieveMappings(MappingSource.fromDomain(domain))) .orElse(MappingsImpl.empty()); }
@Test public void toOptionalShouldBeAbsentWhenNoData() { MappingsImpl mappings = MappingsImpl.empty(); Optional<Mappings> optional = mappings.toOptional(); assertThat(optional.isPresent()).isFalse(); }
@Override protected Mappings mapAddress(String user, Domain domain) { return OptionalUtils.orSuppliers( () -> retrieveMappings(MappingSource.fromUser(User.fromLocalPartWithDomain(user, domain))), () -> retrieveMappings(MappingSource.fromDomain(domain))) .orElse(MappingsImpl.empty()); }
@Test public void serializeShouldReturnEmptyStringWhenEmpty() { assertThat(MappingsImpl.empty()).isEmpty(); }
@Test(expected = IllegalStateException.class) public void unionShouldThrowWhenMappingsNull() { MappingsImpl.empty().union(null); }
private Mappings getMappings(User user, int mappingLimit) throws ErrorMappingException, RecipientRewriteTableException { // We have to much mappings throw ErrorMappingException to avoid // infinity loop if (mappingLimit == 0) { throw new TooManyMappingException("554 Too many mappings to process"); } Mappings targetMappings = mapAddress(user.getLocalPart(), user.getDomainPart().get()); try { return MappingsImpl.fromMappings( targetMappings.asStream() .flatMap(Throwing.function((Mapping target) -> convertAndRecurseMapping(user, target, mappingLimit)).sneakyThrow())); } catch (SkipMappingProcessingException e) { return MappingsImpl.empty(); } }
@Test public void listUserDomainMappingsCommandShouldWork() throws Exception { String user = "user@domain"; String domain = "domain"; String[] arguments = { "-h", "127.0.0.1", "-p", "9999", CmdType.LISTUSERDOMAINMAPPINGS.getCommand(), user, domain}; CommandLine commandLine = ServerCmd.parseCommandLine(arguments); when(dataProbe.listUserDomainMappings(user, domain)).thenReturn(MappingsImpl.empty()); testee.executeCommandLine(commandLine); }
protected void addMappingToConfiguration(MappingSource source, String mapping, Type type) throws RecipientRewriteTableException { Mappings mappings = Optional.ofNullable(virtualUserTable.getUserDomainMappings(source)) .orElse(MappingsImpl.empty()); Mappings updatedMappings = MappingsImpl.from(mappings) .add(Mapping.of(type, mapping)) .build(); updateConfiguration(source, mappings, updatedMappings); }
@Test void getSpecificDomainMappingShouldRespondWithNotFoundWhenHasEmptyAliases() throws RecipientRewriteTableException { String domain = "from.com"; when(recipientRewriteTable.getUserDomainMappings(any())).thenReturn(MappingsImpl.empty()); when() .get(domain) .then() .contentType(ContentType.JSON) .statusCode(HttpStatus.NOT_FOUND_404) .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType())) .body("statusCode", is(HttpStatus.NOT_FOUND_404)) .body("message", is("Cannot find mappings for " + domain)); }
@Test void getSpecificDomainMappingShouldRespondWithNotFoundWhenHasNoAliases() throws RecipientRewriteTableException { String domain = "from.com"; when(recipientRewriteTable.getUserDomainMappings(any())).thenReturn(MappingsImpl.empty()); when() .get(domain) .then() .contentType(ContentType.JSON) .statusCode(HttpStatus.NOT_FOUND_404) .body("type", is(ErrorResponder.ErrorType.NOT_FOUND.getType())) .body("statusCode", is(HttpStatus.NOT_FOUND_404)) .body("message", is("Cannot find mappings for " + domain)); }
@Test public void testStoreAndRetrieveErrorMapping() throws ErrorMappingException, RecipientRewriteTableException { String user = "test"; Domain domain = Domain.LOCALHOST; MappingSource source = MappingSource.fromUser(user, domain); String error = "bounce!"; assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); virtualUserTable.addMapping(source, Mapping.error(error)); assertThat(virtualUserTable.getAllMappings()).describedAs("One mappingline").hasSize(1); assertThatThrownBy(() -> virtualUserTable.getMappings(user, domain)) .describedAs("Exception thrown on to many mappings") .isInstanceOf(ErrorMappingException.class); virtualUserTable.removeMapping(source, Mapping.error(error)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); assertThat(virtualUserTable.getAllMappings()).describedAs("No mapping").isEmpty(); }
@Test public void excludeShouldReturnEmptyCollectionWhenOnlyMatchingMapping() { MappingsImpl mappings = MappingsImpl.builder() .add(Mapping.address("toto")) .add(Mapping.address("tata")) .build(); assertThat(mappings.exclude(Mapping.Type.Address)).isEqualTo(MappingsImpl.empty()); }
@Test public void testStoreAndRetrieveWildCardAddressMapping() throws ErrorMappingException, RecipientRewriteTableException { String user = "test"; String user2 = "test2"; Domain domain = Domain.LOCALHOST; String address = "test@localhost2"; String address2 = "test@james"; MappingSource source = MappingSource.fromUser(user, domain); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); virtualUserTable.addMapping(MappingSource.fromDomain(domain), Mapping.address(address)); virtualUserTable.addMapping(source, Mapping.address(address2)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("One mappings").hasSize(1); assertThat(virtualUserTable.getMappings(user2, domain)).describedAs("One mappings").hasSize(1); virtualUserTable.removeMapping(source, Mapping.address(address2)); virtualUserTable.removeMapping(MappingSource.fromDomain(domain), Mapping.address(address)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); assertThat(virtualUserTable.getMappings(user2, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); }
@Test public void testStoreAndRetrieveAddressMapping() throws ErrorMappingException, RecipientRewriteTableException { String user = "test"; Domain domain = Domain.LOCALHOST; MappingSource source = MappingSource.fromUser(user, domain); String address = "test@localhost2"; String address2 = "test@james"; assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); virtualUserTable.addMapping(source, Mapping.address(address)); virtualUserTable.addMapping(source, Mapping.address(address2)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("Two mappings").hasSize(2); assertThat(virtualUserTable.getAllMappings()).describedAs("One mappingline").hasSize(1); virtualUserTable.removeMapping(source, Mapping.address(address)); virtualUserTable.removeMapping(source, Mapping.address(address2)); assertThat(virtualUserTable.getMappings(user, domain)).describedAs("No mapping") .isEqualTo(MappingsImpl.empty()); assertThat(virtualUserTable.getAllMappings()).describedAs("No mapping").isEmpty(); }
@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 removeForwardMappingShouldDelete() throws ErrorMappingException, RecipientRewriteTableException { String user = "test"; Domain domain = Domain.LOCALHOST; String address = "test@localhost2"; String address2 = "test@james"; MappingSource source = MappingSource.fromUser(user, domain); virtualUserTable.addMapping(source, Mapping.forward(address)); virtualUserTable.addMapping(source, Mapping.forward(address2)); virtualUserTable.removeMapping(source, Mapping.forward(address)); virtualUserTable.removeMapping(source, Mapping.forward(address2)); assertThat(virtualUserTable.getMappings(user, domain)) .isEqualTo(MappingsImpl.empty()); }
@Test public void removeGroupMappingShouldDelete() throws ErrorMappingException, RecipientRewriteTableException { String user = "test"; Domain domain = Domain.LOCALHOST; String address = "test@localhost2"; String address2 = "test@james"; MappingSource source = MappingSource.fromUser(user, domain); virtualUserTable.addMapping(source, Mapping.group(address)); virtualUserTable.addMapping(source, Mapping.group(address2)); virtualUserTable.removeMapping(source, Mapping.group(address)); virtualUserTable.removeMapping(source, Mapping.group(address2)); assertThat(virtualUserTable.getMappings(user, domain)) .isEqualTo(MappingsImpl.empty()); } }
@Test void getDomainMappingsEmptyMappingsAreFilteredOut() throws RecipientRewriteTableException { MappingSource nonEmptyMapping = MappingSource.fromDomain(Domain.of("abc.com")); MappingSource emptyMapping = MappingSource.fromDomain(Domain.of("def.com")); Map<MappingSource, Mappings> mappings = ImmutableMap.of( nonEmptyMapping, MappingsImpl.fromRawString("domain:a.com"), emptyMapping, MappingsImpl.empty() ); when(recipientRewriteTable.getAllMappings()).thenReturn(mappings); Map<String, List<String>> map = when() .get() .then() .contentType(ContentType.JSON) .statusCode(HttpStatus.OK_200) .extract() .body() .jsonPath() .getMap("."); assertThat(map) .containsKey(nonEmptyMapping.asString()) .doesNotContainKey(emptyMapping.asString()); }