MultiServiceInternalStatisticsReceiver implements InternalStatisticsReceiver { @Override public void putEvents(final List<InternalStatisticEvent> statisticEvents) { try { Map<InternalStatisticsService, Map<DocRef, List<InternalStatisticEvent>>> serviceToEventsMapMap = statisticEvents.stream() .flatMap(event -> internalStatisticsConfig.getEnabledDocRefs(event.getKey()) .stream() .map(docRef -> Tuple.of(getServiceForType(docRef.getType()), docRef, event)) .filter(serviceDocRefEventTuple3 -> serviceDocRefEventTuple3._1() != null)) .collect(Collectors.groupingBy( Tuple3::_1, Collectors.groupingBy( Tuple3::_2, Collectors.mapping( Tuple3::_3, Collectors.toList())))); if (LOGGER.isTraceEnabled()) { LOGGER.trace("Putting {} events to {} services", statisticEvents.size(), serviceToEventsMapMap.size()); } serviceToEventsMapMap.forEach(this::putEvents); } catch (final RuntimeException e) { LOGGER.error("Error sending internal stats", e); } } MultiServiceInternalStatisticsReceiver(final Map<String, InternalStatisticsService> docRefTypeToServiceMap,
                                           final InternalStatisticsConfig internalStatisticsConfig); @Override void putEvent(final InternalStatisticEvent event); @Override void putEvents(final List<InternalStatisticEvent> statisticEvents);  }
@Test void putEvents() { Mockito.when(internalStatisticsConfig.getEnabledDocRefs(Mockito.eq(STAT_KEY_A))) .thenReturn(Arrays.asList(DOC_REF_A1, DOC_REF_A2)); assertThat(internalStatisticsConfig.getEnabledDocRefs(STAT_KEY_A)) .containsExactly(DOC_REF_A1, DOC_REF_A2); Mockito.when(internalStatisticsConfig.getEnabledDocRefs(Mockito.eq(STAT_KEY_B))) .thenReturn(Arrays.asList(DOC_REF_B1)); assertThat(internalStatisticsConfig.getEnabledDocRefs(STAT_KEY_B)) .containsExactly(DOC_REF_B1); Map<String, InternalStatisticsService> docRefTypeToServiceMap = ImmutableMap.of( DOC_REF_TYPE_1, internalStatisticsService1, DOC_REF_TYPE_2, internalStatisticsService2); MultiServiceInternalStatisticsReceiver facade = new MultiServiceInternalStatisticsReceiver( docRefTypeToServiceMap, internalStatisticsConfig); facade.putEvents(Arrays.asList( EVENT_A601, EVENT_A602, EVENT_A603, EVENT_B701, EVENT_B702, EVENT_B703)); Mockito.verify(internalStatisticsService1, Mockito.times(1)) .putEvents(argCaptor1.capture()); assertThat(argCaptor1.getValue().keySet()) .containsExactlyInAnyOrder(DOC_REF_A1, DOC_REF_B1); assertThat(argCaptor1.getValue().get(DOC_REF_A1)) .containsExactly(EVENT_A601, EVENT_A602, EVENT_A603); assertThat(argCaptor1.getValue().get(DOC_REF_B1)) .containsExactly(EVENT_B701, EVENT_B702, EVENT_B703); Mockito.verify(internalStatisticsService2, Mockito.times(1)) .putEvents(argCaptor2.capture()); assertThat(argCaptor2.getValue().keySet()) .containsExactly(DOC_REF_A2); assertThat(argCaptor2.getValue().get(DOC_REF_A2)) .containsExactly(EVENT_A601, EVENT_A602, EVENT_A603); }