FileReporter implements Reporter { @Override public void store(Report report) { if (csvWriter == null) { logger.warn("Unable to store report because the csv writer is not initialized."); return; } try { synchronized (writeLock) { csvWriter.write(report, FIELDS, WRITE_PROCESSORS); csvWriter.flush(); } } catch (IOException e) { logger.warn("Unable to store report because of internal csv writer error.", e); return; } } FileReporter(String filename); @Override void store(Report report); @Override Report[] retrieve(ReportQuery query); static final String ISO_8601; }
@Test public void testWritingCSV() throws IOException { File file = getTempFile(); Request req = new Request("What is today's date?", "request-1"); Agent agent = mock(Agent.class); when(agent.getName()).thenReturn("date"); Report r = new Report(req); r.setAgent(agent); r.setRouteTime(30, 47); r.setAgentTime(78, 450000); r.setResponse(new Response("yesterday")); Reporter reporter = new FileReporter(file.toString()); reporter.store(r); String expected = "request-1,What is today's date?,date,yesterday,0,17,449922"; List<String> lines = Files.readLines(file, Charsets.UTF_8); String actual = lines.get(1); actual = actual.substring(actual.indexOf(',') + 1); assertEquals(expected, actual); }