@Test
	public void testTwoDaysDataSimulation() throws Exception {
		final DateTime start = new DateTime(2013, 12, 30, 23, 27);
		final DateTime end = start.plusDays(2);
		DateTime now = start;

		int total = 0;

		while (now.isBefore(end)) {
			int minute = now.getMinuteOfHour();
			aggregateCounterRepository.increment(counterName, minute, now);
			now = now.plusMinutes(1);
			total += minute;
		}
		// Include end time's metric
		int endMinute = end.getMinuteOfHour();
		aggregateCounterRepository.increment(counterName, endMinute, now);
		total += endMinute;

		// Check the total
		assertEquals(total, aggregateCounterRepository.findOne(counterName).getValue());

		// Query the entire period
		Interval queryInterval = new Interval(start, end);
		AggregateCount aggregateCount = aggregateCounterRepository.getCounts(counterName, queryInterval,
				AggregateCountResolution.minute);
		assertEquals(counterName, aggregateCount.getName());
		assertEquals(queryInterval, aggregateCount.getInterval());
		long[] counts = aggregateCount.getCounts();
		// counts.length should include the end time's minute
		assertEquals((2 * 24 * 60) + 1, counts.length);
		assertEquals(27, counts[0]);
		assertEquals(28, counts[1]);
		assertEquals(59, counts[32]);
		for (int i = 33; i < counts.length; i++) {
			int expect = (i - 33) % 60;
			assertEquals("Count at index " + i + " should be " + expect, expect, counts[i]);
		}

		// Query a 24 hour period in minutes
		now = start.plusHours(5).withMinuteOfHour(0);
		queryInterval = new Interval(now, now.plusHours(24));

		aggregateCount = aggregateCounterRepository.getCounts(counterName, queryInterval, AggregateCountResolution.minute);
		counts = aggregateCount.getCounts();
		// Add 'now.plusHours(24)' minute time to the count
		assertEquals((24 * 60) + 1, counts.length);
		assertEquals(0, counts[0]); // on an hour boundary
		for (int i = 1; i < counts.length; i++) {
			int expect = i % 60;
			assertEquals("Count at index " + i + " should be " + expect, expect, counts[i]);
		}

		// Query the entire period in hours
		queryInterval = new Interval(start, end);
		aggregateCount = aggregateCounterRepository.getCounts(counterName, queryInterval, AggregateCountResolution.hour);
		counts = aggregateCount.getCounts();
		assertEquals(49, counts.length);
		// The first hour starts before the first counts are added
		assertEquals(1419, counts[0]); // sum [27..59]
		for (int i = 1; i < (counts.length - 1); i++) {
			assertEquals(1770, counts[i]); // sum [0..59]
		}
		// The last hour ends at 27th minute
		assertEquals(378, counts[counts.length - 1]); // sum [0..27]

		// Query the entire period in days
		aggregateCount = aggregateCounterRepository.getCounts(counterName, queryInterval, AggregateCountResolution.day);
		counts = aggregateCount.getCounts();
		assertEquals(3, counts.length);

		// Query beyond the period where we have data
		DateTime newStart = start.withYear(2012);
		aggregateCount = aggregateCounterRepository.getCounts(counterName, queryInterval.withStart(newStart), AggregateCountResolution.day);
		counts = aggregateCount.getCounts();
		assertEquals(368, counts.length);

		aggregateCount = aggregateCounterRepository.getCounts(counterName, queryInterval.withStart(newStart), AggregateCountResolution.month);
	}
@author Luke Taylor
@author Gary Russell
@author Ilayaperumal Gopinathan
spring-xd-analytics/src/test/java/org/springframework/xd/analytics/metrics/AbstractAggregateCounterTests.java