### Question:
ApmServerLogShipper extends AbstractIntakeApiHandler implements FileChangeListener { private void write(OutputStream os, byte[] line, int offset, int length, boolean eol) throws IOException { os.write(line, offset, length); if (eol) { currentlyTransmitting++; os.write('\n'); if (shouldEndRequest()) { endRequest(); } } } ApmServerLogShipper(ApmServerClient apmServerClient, ReporterConfiguration reporterConfiguration, MetaData metaData, PayloadSerializer payloadSerializer); @Override boolean onLineAvailable(TailableFile tailableFile, byte[] line, int offset, int length, boolean eol); @Override void onIdle(); @Override void onShutdownInitiated(); @Override void onShutdownComplete(); static final String LOGS_ENDPOINT; }### Answer:
@Test public void testSendLogs() throws Exception { Files.write(logFile.toPath(), List.of("foo")); assertThat(tailableFile.tail(buffer, logShipper, 100)).isEqualTo(1); logShipper.endRequest(); List<String> events = getEvents(); mockApmServer.verify(postRequestedFor(urlEqualTo(ApmServerLogShipper.LOGS_ENDPOINT))); assertThat(events).hasSize(3); JsonNode fileMetadata = new ObjectMapper().readTree(events.get(1)).get("metadata").get("log").get("file"); assertThat(fileMetadata.get("name").textValue()).isEqualTo(logFile.getName()); assertThat(fileMetadata.get("path").textValue()).isEqualTo(logFile.getAbsolutePath()); assertThat(events.get(2)).isEqualTo("foo"); }
@Test public void testSendLogsAfterServerUrlsSet() throws Exception { apmServerClient.start(Lists.emptyList()); Files.write(logFile.toPath(), List.of("foo")); assertThat(logShipper.getErrorCount()).isEqualTo(0); Future<Integer> readLinesFuture = Executors.newSingleThreadExecutor().submit(() -> tailableFile.tail(buffer, logShipper, 100)); Awaitility.await() .pollInterval(1, TimeUnit.MILLISECONDS) .timeout(100, TimeUnit.MILLISECONDS) .untilAsserted(() -> assertThat(logShipper.getErrorCount()).isGreaterThan(0)); startClientWithValidUrls(); assertThat(readLinesFuture.get(1500, TimeUnit.MILLISECONDS)).isEqualTo(1); assertThat(logShipper.getErrorCount()).isGreaterThan(0); logShipper.endRequest(); assertThat(logShipper.getErrorCount()).isEqualTo(0); List<String> events = getEvents(); mockApmServer.verify(postRequestedFor(urlEqualTo(ApmServerLogShipper.LOGS_ENDPOINT))); assertThat(events).hasSize(3); JsonNode fileMetadata = new ObjectMapper().readTree(events.get(1)).get("metadata").get("log").get("file"); assertThat(fileMetadata.get("name").textValue()).isEqualTo(logFile.getName()); assertThat(fileMetadata.get("path").textValue()).isEqualTo(logFile.getAbsolutePath()); assertThat(events.get(2)).isEqualTo("foo"); }