OneCollectorChannelListener extends AbstractChannelListener { @Override public void onPreparedLog(@NonNull Log log, @NonNull String groupName, int flags) { if (!isOneCollectorCompatible(log)) { return; } Collection<CommonSchemaLog> commonSchemaLogs; try { commonSchemaLogs = mLogSerializer.toCommonSchemaLog(log); } catch (IllegalArgumentException e) { AppCenterLog.error(LOG_TAG, "Cannot send a log to one collector: " + e.getMessage()); return; } for (CommonSchemaLog commonSchemaLog : commonSchemaLogs) { commonSchemaLog.setFlags((long) flags); EpochAndSeq epochAndSeq = mEpochsAndSeqsByIKey.get(commonSchemaLog.getIKey()); if (epochAndSeq == null) { epochAndSeq = new EpochAndSeq(UUID.randomUUID().toString()); mEpochsAndSeqsByIKey.put(commonSchemaLog.getIKey(), epochAndSeq); } SdkExtension sdk = commonSchemaLog.getExt().getSdk(); sdk.setEpoch(epochAndSeq.epoch); sdk.setSeq(++epochAndSeq.seq); sdk.setInstallId(mInstallId); } String oneCollectorGroupName = getOneCollectorGroupName(groupName); for (CommonSchemaLog commonSchemaLog : commonSchemaLogs) { mChannel.enqueue(commonSchemaLog, oneCollectorGroupName, flags); } } OneCollectorChannelListener(@NonNull Channel channel, @NonNull LogSerializer logSerializer, @NonNull HttpClient httpClient, @NonNull UUID installId); @VisibleForTesting  OneCollectorChannelListener(@NonNull OneCollectorIngestion ingestion, @NonNull Channel channel, @NonNull LogSerializer logSerializer, @NonNull UUID installId); void setLogUrl(@NonNull String logUrl); @Override void onGroupAdded(@NonNull String groupName, Channel.GroupListener groupListener, long batchTimeInterval); @Override void onGroupRemoved(@NonNull String groupName); @Override void onPreparedLog(@NonNull Log log, @NonNull String groupName, int flags); @Override boolean shouldFilter(@NonNull Log log); @Override void onClear(@NonNull String groupName); @Override void onPaused(@NonNull String groupName, String targetToken); @Override void onResumed(@NonNull String groupName, String targetToken); @Override void onGloballyEnabled(boolean isEnabled);  }
@Test public void validateCommonSchemaLogs() { Channel channel = mock(Channel.class); LogSerializer logSerializer = mock(LogSerializer.class); when(logSerializer.toCommonSchemaLog(any(Log.class))).thenThrow(new IllegalArgumentException()); Log log = mock(Log.class); when(log.getTransmissionTargetTokens()).thenReturn(Collections.singleton("token")); OneCollectorChannelListener listener = new OneCollectorChannelListener(channel, logSerializer, createHttpClient(mock(Context.class)), UUID.randomUUID()); listener.onPreparedLog(log, TEST_GROUP, DEFAULTS); verify(logSerializer).toCommonSchemaLog(any(Log.class)); verify(channel, never()).enqueue(any(Log.class), anyString(), anyInt()); }
@Test public void dontConvertCommonSchemaLogs() { Channel channel = mock(Channel.class); LogSerializer logSerializer = mock(LogSerializer.class); OneCollectorChannelListener listener = new OneCollectorChannelListener(channel, logSerializer, createHttpClient(mock(Context.class)), UUID.randomUUID()); listener.onPreparedLog(mock(CommonSchemaLog.class), TEST_GROUP, DEFAULTS); verify(logSerializer, never()).toCommonSchemaLog(any(Log.class)); verify(channel, never()).enqueue(any(Log.class), anyString(), anyInt()); }