@Test
public void testDiscoverySequence() throws InterruptedException {

    storage.receivedEvent(InventoryEvent.discovery(
            endpointService,
            resourceManager,
            resourceTypeManager,
            resourceManager.getResourcesBreadthFirst(),
            Collections.emptyList()));
    Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size());
    expectCalls(collectedPostCalls,
            "http://ignore/ignore/import");
    // Make sure persisted time is correct
    final long initialTime = R_1.getPersistedTime();
    Assert.assertTrue(initialTime > 0);
    Assert.assertEquals(initialTime, R_2.getPersistedTime());
    Assert.assertEquals(initialTime, RT_1.getPersistedTime());
    Thread.sleep(10);
    // Next run with no discovered resource
    storage.receivedEvent(InventoryEvent.discovery(
            endpointService,
            resourceManager,
            resourceTypeManager,
            Collections.emptyList(),
            Collections.emptyList()));
    Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size());
    Assert.assertEquals(0, collectedPostCalls.size());
    // Persistence time hasn't changed
    Assert.assertEquals(initialTime, RT_1.getPersistedTime());
    Assert.assertEquals(initialTime, R_1.getPersistedTime());
    Thread.sleep(10);
    // Next run with 1 discovered resource under R_2
    Resource<AnyLocation> r3 = Resource.<AnyLocation>builder()
            .id(new ID("r3"))
            .name(new Name("Resource 3"))
            .location(new AnyLocation("/2/2/3"))
            .type(RT_2)
            .parent(R_2)
            .build();
    resourceManager.addResource(r3);
    storage.receivedEvent(InventoryEvent.discovery(
            endpointService,
            resourceManager,
            resourceTypeManager,
            Collections.singletonList(r3),
            Collections.emptyList()));
    Assert.assertEquals("Unexpected DELETE calls: " + collectedDeleteCalls, 0, collectedDeleteCalls.size());
    expectCalls(collectedPostCalls,
            "http://ignore/ignore/import");
    // Persistence time changed only for R_2
    Assert.assertEquals(initialTime, RT_1.getPersistedTime());
    Assert.assertEquals(initialTime, R_1.getPersistedTime());
    final long intermediateR2Time = R_2.getPersistedTime();
    Thread.sleep(10);
    // Next run with removed R_1
    resourceManager.removeResource(R_1);
    storage.receivedEvent(InventoryEvent.discovery(
            endpointService,
            resourceManager,
            resourceTypeManager,
            Collections.emptyList(),
            Collections.singletonList(R_1)));
    expectCalls(collectedDeleteCalls,
            "http://ignore/ignore/resources?ids=r1");
    Assert.assertEquals(0, collectedPostCalls.size());
    // Persistence time hasn't changed
    Assert.assertEquals(initialTime, RT_1.getPersistedTime());
    Assert.assertEquals(intermediateR2Time, R_2.getPersistedTime());
    Thread.sleep(10);
    // Next run with removed r3
    resourceManager.removeResource(r3);
    storage.receivedEvent(InventoryEvent.discovery(
            endpointService,
            resourceManager,
            resourceTypeManager,
            Collections.emptyList(),
            Collections.singletonList(r3)));
    // r3 being a child resource, it triggers an update on its parent
    Assert.assertEquals(0, collectedPostCalls.size());
    expectCalls(collectedDeleteCalls,
            "http://ignore/ignore/resources?ids=r3");
    // Persistence time changed for R2
    Assert.assertEquals(initialTime, RT_1.getPersistedTime());
    Assert.assertEquals(intermediateR2Time, R_2.getPersistedTime());
}