SqlReporter implements Reporter { @Override public Report[] retrieve(ReportQuery query) { Session session = sessionFactory.openSession(); session.beginTransaction(); Query hql = session.createQuery(buildQuery(query)) .setParameter("ts_start", query.getStartDate()) .setParameter("ts_stop", query.getEndDate()); if (query.getAgentNames() != null) { hql.setParameterList("agents", query.getAgentNames()); } if (query.getLimit() != ReportQuery.NO_LIMIT) { hql.setMaxResults(query.getLimit()); } @SuppressWarnings("unchecked") List<SqlReport> reports = hql.list(); session.getTransaction().commit(); session.close(); Report[] rtnReports = new Report[reports.size()]; for (int i = 0; i < reports.size(); i++) { rtnReports[i] = new Report(reports.get(i)); } return rtnReports; } SqlReporter(SessionFactory sessionFactory); @Override void store(Report report); @Override Report[] retrieve(ReportQuery query);  }
@Test public void testDateRangeRetrieve() throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); SqlReporter reporter = new SqlReporter(sessionFactory); loadData(reporter); ReportQuery rq = new ReportQuery(); rq.setStartDate(df.parse("2016-02-04T12:00:00UTC")); rq.setEndDate(df.parse("2016-02-06T12:00:00UTC")); Report[] reports = reporter.retrieve(rq); assertEquals(2, reports.length); assertEquals(df.parse("2016-02-05T15:43:34UTC"), reports[1].getTimestamp()); }
@Test public void testLimitRetrieve() throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); SqlReporter reporter = new SqlReporter(sessionFactory); loadData(reporter); ReportQuery rq = new ReportQuery(); rq.setLimit(3); Report[] reports = reporter.retrieve(rq); assertEquals(3, reports.length); assertEquals(df.parse("2016-02-02T15:43:34UTC"), reports[0].getTimestamp()); assertEquals(df.parse("2016-02-03T15:43:34UTC"), reports[1].getTimestamp()); assertEquals(df.parse("2016-02-04T15:43:34UTC"), reports[2].getTimestamp()); }
@Test public void testAgentFilterRetrieve() throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); SqlReporter reporter = new SqlReporter(sessionFactory); loadData(reporter); ReportQuery rq = new ReportQuery(); rq.setAgentName("all"); Report[] reports = reporter.retrieve(rq); assertEquals(3, reports.length); assertEquals(df.parse("2016-02-02T15:43:34UTC"), reports[0].getTimestamp()); assertEquals(df.parse("2016-02-05T15:43:34UTC"), reports[1].getTimestamp()); assertEquals(df.parse("2016-02-06T15:43:34UTC"), reports[2].getTimestamp()); }
@Test public void testAgentArrayFilterRetrieve() throws ParseException { DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); SqlReporter reporter = new SqlReporter(sessionFactory); loadData(reporter); ReportQuery rq = new ReportQuery(); rq.setAgentNames(new String[]{"time", "date"}); Report[] reports = reporter.retrieve(rq); assertEquals(2, reports.length); assertEquals(df.parse("2016-02-03T15:43:34UTC"), reports[0].getTimestamp()); assertEquals(df.parse("2016-02-04T15:43:34UTC"), reports[1].getTimestamp()); }