@Test public void combineWithoutSubTopicTest() throws AlertException { TopLevelActionCombiner topLevelActionCombiner = new TopLevelActionCombiner(); String commonProviderValue = "common_provider"; Long commonProviderId = 1L; String commonProviderConfig = "common_provider_config"; String commonTopicName = "common topic name"; String commonTopicValue = "common topic value"; String commonSubTopicName = "common_sub_topic_name"; String commonSubTopicValue = "common.sub.topic.value"; ProviderMessageContent addAction = new ProviderMessageContent.Builder() .applyNotificationId(1L) .applyProvider(commonProviderValue, commonProviderId, commonProviderConfig) .applyTopic(commonTopicName, commonTopicValue) .applySubTopic(commonSubTopicName, commonSubTopicValue) .applyAction(ItemOperation.ADD) .build(); ProviderMessageContent updateAction = new ProviderMessageContent.Builder() .applyNotificationId(1L) .applyProvider(commonProviderValue, commonProviderId, commonProviderConfig) .applyTopic(commonTopicName, commonTopicValue) .applySubTopic(commonSubTopicName, commonSubTopicValue) .applyAction(ItemOperation.UPDATE) .build(); ProviderMessageContent infoAction = new ProviderMessageContent.Builder() .applyNotificationId(1L) .applyProvider(commonProviderValue, commonProviderId, commonProviderConfig) .applyTopic(commonTopicName, commonTopicValue) .applySubTopic(commonSubTopicName, commonSubTopicValue) .applyAction(ItemOperation.INFO) .build(); ProviderMessageContent unrelatedTopicAdd = new ProviderMessageContent.Builder() .applyNotificationId(1L) .applyProvider(commonProviderValue, commonProviderId, commonProviderConfig) .applyTopic("uncommon topic name 123", "random value") .applySubTopic(commonSubTopicName, commonSubTopicValue) .applyAction(ItemOperation.ADD) .build(); ProviderMessageContent deleteAction = new ProviderMessageContent.Builder() .applyNotificationId(1L) .applyProvider(commonProviderValue, commonProviderId, commonProviderConfig) .applyTopic(commonTopicName, commonTopicValue) .applyAction(ItemOperation.DELETE) .build(); List<ProviderMessageContent> orderedActionMessages = List.of(addAction, updateAction, infoAction, unrelatedTopicAdd, deleteAction); List<ProviderMessageContent> combinedMessages = topLevelActionCombiner.combine(orderedActionMessages); assertEquals(1, combinedMessages.size()); }
@Override public List<ProviderMessageContent> combine(List<ProviderMessageContent> messages) { Map<String, List<ProviderMessageContent>> providerMessagesCache = new LinkedHashMap<>(); for (ProviderMessageContent message : messages) { String cacheKey = flattenProviderAndTopic(message); if (isTopLevelDelete(message)) { List<ProviderMessageContent> cachedMessages = providerMessagesCache.get(cacheKey); if (null != cachedMessages) { boolean didUpdate = updateCache(message, cachedMessages); if (didUpdate) { continue; } } } providerMessagesCache.computeIfAbsent(cacheKey, ignored -> new LinkedList<>()).add(message); } return Stream .of(providerMessagesCache.values()) .flatMap(Collection::stream) .flatMap(List::stream) .collect(Collectors.toList()); }
TopLevelActionCombiner implements MessageCombiner { @Override public List<ProviderMessageContent> combine(List<ProviderMessageContent> messages) { Map<String, List<ProviderMessageContent>> providerMessagesCache = new LinkedHashMap<>(); for (ProviderMessageContent message : messages) { String cacheKey = flattenProviderAndTopic(message); if (isTopLevelDelete(message)) { List<ProviderMessageContent> cachedMessages = providerMessagesCache.get(cacheKey); if (null != cachedMessages) { boolean didUpdate = updateCache(message, cachedMessages); if (didUpdate) { continue; } } } providerMessagesCache.computeIfAbsent(cacheKey, ignored -> new LinkedList<>()).add(message); } return Stream .of(providerMessagesCache.values()) .flatMap(Collection::stream) .flatMap(List::stream) .collect(Collectors.toList()); } }
TopLevelActionCombiner implements MessageCombiner { @Override public List<ProviderMessageContent> combine(List<ProviderMessageContent> messages) { Map<String, List<ProviderMessageContent>> providerMessagesCache = new LinkedHashMap<>(); for (ProviderMessageContent message : messages) { String cacheKey = flattenProviderAndTopic(message); if (isTopLevelDelete(message)) { List<ProviderMessageContent> cachedMessages = providerMessagesCache.get(cacheKey); if (null != cachedMessages) { boolean didUpdate = updateCache(message, cachedMessages); if (didUpdate) { continue; } } } providerMessagesCache.computeIfAbsent(cacheKey, ignored -> new LinkedList<>()).add(message); } return Stream .of(providerMessagesCache.values()) .flatMap(Collection::stream) .flatMap(List::stream) .collect(Collectors.toList()); }  }
TopLevelActionCombiner implements MessageCombiner { @Override public List<ProviderMessageContent> combine(List<ProviderMessageContent> messages) { Map<String, List<ProviderMessageContent>> providerMessagesCache = new LinkedHashMap<>(); for (ProviderMessageContent message : messages) { String cacheKey = flattenProviderAndTopic(message); if (isTopLevelDelete(message)) { List<ProviderMessageContent> cachedMessages = providerMessagesCache.get(cacheKey); if (null != cachedMessages) { boolean didUpdate = updateCache(message, cachedMessages); if (didUpdate) { continue; } } } providerMessagesCache.computeIfAbsent(cacheKey, ignored -> new LinkedList<>()).add(message); } return Stream .of(providerMessagesCache.values()) .flatMap(Collection::stream) .flatMap(List::stream) .collect(Collectors.toList()); }  @Override List<ProviderMessageContent> combine(List<ProviderMessageContent> messages); }
TopLevelActionCombiner implements MessageCombiner { @Override public List<ProviderMessageContent> combine(List<ProviderMessageContent> messages) { Map<String, List<ProviderMessageContent>> providerMessagesCache = new LinkedHashMap<>(); for (ProviderMessageContent message : messages) { String cacheKey = flattenProviderAndTopic(message); if (isTopLevelDelete(message)) { List<ProviderMessageContent> cachedMessages = providerMessagesCache.get(cacheKey); if (null != cachedMessages) { boolean didUpdate = updateCache(message, cachedMessages); if (didUpdate) { continue; } } } providerMessagesCache.computeIfAbsent(cacheKey, ignored -> new LinkedList<>()).add(message); } return Stream .of(providerMessagesCache.values()) .flatMap(Collection::stream) .flatMap(List::stream) .collect(Collectors.toList()); }  @Override List<ProviderMessageContent> combine(List<ProviderMessageContent> messages);  }