@Override public Mappings getUserDomainMappings(MappingSource source) throws RecipientRewriteTableException { return MappingsImpl.empty(); }
private Mappings getMapping(String user, Domain domain, String queryName) throws RecipientRewriteTableException { EntityManager entityManager = entityManagerFactory.createEntityManager(); final EntityTransaction transaction = entityManager.getTransaction(); try { transaction.begin(); @SuppressWarnings("unchecked") List<JPARecipientRewrite> virtualUsers = entityManager .createNamedQuery(queryName) .setParameter("user", user) .setParameter("domain", domain.asString()) .getResultList(); transaction.commit(); if (virtualUsers.size() > 0) { return MappingsImpl.fromRawString(virtualUsers.get(0).getTargetAddress()); } return MappingsImpl.empty(); } catch (PersistenceException e) { LOGGER.debug("Failed to find mapping for user={} and domain={}", user, domain, e); if (transaction.isActive()) { transaction.rollback(); } throw new RecipientRewriteTableException("Error while retrieve mappings", e); } finally { entityManager.close(); } }
@Test public void mergeShouldThrowWhenRightIsNull() { MappingsImpl.Builder right = null; assertThatThrownBy(() -> MappingsImpl.Builder.merge(MappingsImpl.builder(), right)) .isInstanceOf(NullPointerException.class); }
private Optional<Mappings> retrieveMappings(MappingSource mappingSource) { Stream<Mapping> userEntries = mappingEntries.stream() .filter(mappingEntry -> mappingEntry.source.equals(mappingSource)) .map(InMemoryMappingEntry::getMapping); return MappingsImpl.fromMappings(userEntries).toOptional(); }
public Mappings toMapping() { return MappingsImpl.fromRawString(getMapping()); }
@Test public void toOptionalShouldBeAbsentWhenNoData() { MappingsImpl mappings = MappingsImpl.empty(); Optional<Mappings> optional = mappings.toOptional(); assertThat(optional.isPresent()).isFalse(); }
@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()); }
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(); } }
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 public void toOptionalShouldBePresentWhenContainingData() { MappingsImpl mappings = MappingsImpl.builder().add("toto").build(); Optional<Mappings> optional = mappings.toOptional(); assertThat(optional.isPresent()).isTrue(); }
@Test public void fromRawStringShouldReturnEmptyCollectionWhenEmptyString() { MappingsImpl actual = MappingsImpl.fromRawString(""); assertThat(actual.asStrings()).isEmpty(); }
@Test public void unionShouldReturnMergedWhenBothContainsData() { Mappings mappings = MappingsImpl.fromRawString("toto").union(MappingsImpl.fromRawString("tata")); assertThat(mappings).containsOnly(Mapping.address("toto"),Mapping.address("tata")); }
private Optional<Mappings> retrieveMappings(MappingSource source) { List<String> mappings = executor.execute(retrieveMappingStatement.bind() .setString(USER, source.getFixedUser()) .setString(DOMAIN, source.getFixedDomain())) .thenApply(resultSet -> cassandraUtils.convertToStream(resultSet) .map(row -> row.getString(MAPPING)) .collect(Guavate.toImmutableList())) .join(); return MappingsImpl.fromCollection(mappings).toOptional(); }