OneCollectorIngestion implements Ingestion { @Override public ServiceCall sendAsync(String appSecret, UUID installId, LogContainer logContainer, ServiceCallback serviceCallback) throws IllegalArgumentException { Map<String, String> headers = new HashMap<>(); Set<String> apiKeys = new LinkedHashSet<>(); for (Log log : logContainer.getLogs()) { apiKeys.addAll(log.getTransmissionTargetTokens()); } StringBuilder apiKey = new StringBuilder(); for (String targetToken : apiKeys) { apiKey.append(targetToken).append(","); } if (!apiKeys.isEmpty()) { apiKey.deleteCharAt(apiKey.length() - 1); } headers.put(API_KEY, apiKey.toString()); JSONObject tickets = new JSONObject(); for (Log log : logContainer.getLogs()) { List<String> ticketKeys = ((CommonSchemaLog) log).getExt().getProtocol().getTicketKeys(); if (ticketKeys != null) { for (String ticketKey : ticketKeys) { String token = TicketCache.getTicket(ticketKey); if (token != null) { try { tickets.put(ticketKey, token); } catch (JSONException e) { AppCenterLog.error(LOG_TAG, "Cannot serialize tickets, sending log anonymously", e); break; } } } } } if (tickets.length() > 0) { headers.put(TICKETS, tickets.toString()); if (Constants.APPLICATION_DEBUGGABLE) { headers.put(STRICT, Boolean.TRUE.toString()); } } headers.put(CONTENT_TYPE_KEY, CONTENT_TYPE_VALUE); String sdkVersion = com.microsoft.appcenter.BuildConfig.VERSION_NAME; headers.put(CLIENT_VERSION_KEY, String.format(CLIENT_VERSION_FORMAT, sdkVersion)); headers.put(UPLOAD_TIME_KEY, String.valueOf(System.currentTimeMillis())); HttpClient.CallTemplate callTemplate = new IngestionCallTemplate(mLogSerializer, logContainer); return mHttpClient.callAsync(mLogUrl, METHOD_POST, headers, callTemplate, serviceCallback); } OneCollectorIngestion(@NonNull HttpClient httpClient, @NonNull LogSerializer logSerializer); @Override ServiceCall sendAsync(String appSecret, UUID installId, LogContainer logContainer, ServiceCallback serviceCallback); @Override @SuppressWarnings("SameParameterValue") void setLogUrl(@NonNull String logUrl); @Override void reopen(); @Override void close();  }
@Test public void sendAsync() throws Exception { mockStatic(System.class); when(System.currentTimeMillis()).thenReturn(1234L); Extensions ext = new Extensions() {{ setProtocol(new ProtocolExtension()); }}; final CommonSchemaLog log1 = mock(CommonSchemaLog.class); when(log1.getExt()).thenReturn(ext); when(log1.getTransmissionTargetTokens()).thenReturn(Collections.singleton("token1")); final CommonSchemaLog log2 = mock(CommonSchemaLog.class); when(log2.getExt()).thenReturn(ext); when(log2.getTransmissionTargetTokens()).thenReturn(new HashSet<>(Arrays.asList("token2", "token3"))); LogContainer container = new LogContainer() {{ setLogs(new ArrayList<Log>() {{ add(log1); add(log2); }}); }}; LogSerializer serializer = mock(LogSerializer.class); when(serializer.serializeLog(log1)).thenReturn("mockPayload1"); when(serializer.serializeLog(log2)).thenReturn("mockPayload2"); ServiceCall call = mock(ServiceCall.class); ArgumentCaptor<HttpClient.CallTemplate> callTemplate = ArgumentCaptor.forClass(HttpClient.CallTemplate.class); when(mHttpClient.callAsync(anyString(), anyString(), anyMapOf(String.class, String.class), callTemplate.capture(), any(ServiceCallback.class))).thenReturn(call); OneCollectorIngestion ingestion = new OneCollectorIngestion(mHttpClient, serializer); ingestion.setLogUrl("http: ServiceCallback serviceCallback = mock(ServiceCallback.class); assertEquals(call, ingestion.sendAsync(null, null, container, serviceCallback)); HashMap<String, String> expectedHeaders = new HashMap<>(); expectedHeaders.put(OneCollectorIngestion.API_KEY, "token1,token2,token3"); expectedHeaders.put(OneCollectorIngestion.CLIENT_VERSION_KEY, String.format("ACS-Android-Java-no-%s-no", VERSION_NAME)); expectedHeaders.put(OneCollectorIngestion.UPLOAD_TIME_KEY, "1234"); expectedHeaders.put(DefaultHttpClient.CONTENT_TYPE_KEY, "application/x-json-stream; charset=utf-8"); verify(mHttpClient).callAsync(eq("http: assertNotNull(callTemplate.getValue()); assertEquals("mockPayload1\nmockPayload2\n", callTemplate.getValue().buildRequestBody()); ingestion.close(); verify(mHttpClient).close(); ingestion.reopen(); verify(mHttpClient).reopen(); }