try {
setupResource(); // inserts initial UNKNOWN Availability at epoch
commitAndClose();
Availability avail;
long now = System.currentTimeMillis();
// add a report that says the resource is down
avail = new Availability(theResource, DOWN);
AvailabilityReport report = new AvailabilityReport(false, theAgent.getName());
report.addAvailability(avail);
availabilityManager.mergeAvailabilityReport(report);
// now pretend the agent sent us a report from a previous time period - should insert this in the past
avail = new Availability(theResource, (now - 600000), UP);
report = new AvailabilityReport(false, theAgent.getName());
report.addAvailability(avail);
availabilityManager.mergeAvailabilityReport(report);
assert getPointInTime(new Date(avail.getStartTime() - 2)) == UNKNOWN;
assert getPointInTime(new Date(avail.getStartTime())) == UP;
assert getPointInTime(new Date(avail.getStartTime() + 2)) == UP;
// it's still down though - since we've received a more recent report saying it was down
assert availabilityManager.getCurrentAvailabilityTypeForResource(overlord, theResource.getId()) == DOWN;
// now pretend the agent sent us reports from inbetween our existing time periods
// this UP record combines with the UP we added previously
avail = new Availability(theResource, (now - 300000), UP);
report = new AvailabilityReport(false, theAgent.getName());
report.addAvailability(avail);
availabilityManager.mergeAvailabilityReport(report);
assert getPointInTime(new Date(avail.getStartTime() - 2)) == UP;
assert getPointInTime(new Date(avail.getStartTime())) == UP;
assert getPointInTime(new Date(avail.getStartTime() + 2)) == UP;
// its still down though - since we've received a more recent report saying it was down
assert availabilityManager.getCurrentAvailabilityTypeForResource(overlord, theResource.getId()) == DOWN;
// this DOWN record combines with the current DOWN
avail = new Availability(theResource, (now - 100000), DOWN);
report = new AvailabilityReport(false, theAgent.getName());
report.addAvailability(avail);
availabilityManager.mergeAvailabilityReport(report);
assert getPointInTime(new Date(avail.getStartTime() - 2)) == UP;
assert getPointInTime(new Date(avail.getStartTime())) == DOWN;
assert getPointInTime(new Date(avail.getStartTime() + 2)) == DOWN;
// this DOWN record is between the two UPs we added earlier. However, because we are RLE,
// we actually lost the information that we had an UP at both -60000 and -30000. We just
// have a RLE interval of UP starting at -60000. This new DOWN record will add a new row
// that will indicate we were only UP from -60000 to -45000 and DOWN thereafter. This is
// an odd test and probably will never occur in the wild (why would an agent tell us
// we were one status in the past but another status further back in the past?)
avail = new Availability(theResource, (now - 450000), DOWN);
report = new AvailabilityReport(false, theAgent.getName());
report.addAvailability(avail);
availabilityManager.mergeAvailabilityReport(report);
assert getPointInTime(new Date(avail.getStartTime() - 2)) == UP;
assert getPointInTime(new Date(avail.getStartTime())) == DOWN;
assert getPointInTime(new Date(avail.getStartTime() + 2)) == DOWN;
// its still down
assert availabilityManager.getCurrentAvailabilityTypeForResource(overlord, theResource.getId()) == DOWN;
// let's insert one in the very beginning that is the same type as the current first interval
avail = new Availability(theResource, (now - 700000), UP);
report = new AvailabilityReport(false, theAgent.getName());
report.addAvailability(avail);
availabilityManager.mergeAvailabilityReport(report);
assert getPointInTime(new Date(avail.getStartTime() - 2)) == UNKNOWN;
assert getPointInTime(new Date(avail.getStartTime())) == UP;
assert getPointInTime(new Date(avail.getStartTime() + 2)) == UP;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (Status.STATUS_ACTIVE == getTransactionManager().getStatus()) {