@Test
public void ensureCompletelyLoaded() throws PersistenceException {

    // First get a stored user message from the database
    Collection<IMessageUnitEntity>   result =  queryManager.getMessageUnitsWithId(TestData.userMsg1.getMessageId());
    assertFalse(Utils.isNullOrEmpty(result));
    UserMessageEntity userMessage = (UserMessageEntity) result.iterator().next();
    // The User Message should not be completely loaded just after the query
    assertFalse(userMessage.isLoadedCompletely());
    queryManager.ensureCompletelyLoaded(userMessage);
    // Now it should be
    assertTrue(userMessage.isLoadedCompletely());
    // Also check that all info is indeed available
    assertNotNull(userMessage.getSender());
    assertTrue(CompareUtils.areEqual(TestData.userMsg1.getSender(), userMessage.getSender()));
    assertNotNull(userMessage.getReceiver());
    assertTrue(CompareUtils.areEqual(TestData.userMsg1.getReceiver(), userMessage.getReceiver()));
    assertFalse(Utils.isNullOrEmpty(userMessage.getPayloads()));
    IPayload retrievedPayload = userMessage.getPayloads().iterator().next();
    IPayload originalPayload = TestData.userMsg1.getPayloads().iterator().next();
    assertEquals(originalPayload.getContainment(), retrievedPayload.getContainment());
    assertFalse(Utils.isNullOrEmpty(retrievedPayload.getProperties()));
    assertTrue(CompareUtils.areEqual(retrievedPayload.getProperties().iterator().next(),
                                     originalPayload.getProperties().iterator().next()));
    assertEquals(originalPayload.getDescription().getText(), retrievedPayload.getDescription().getText());
    assertEquals(originalPayload.getDescription().getLanguage(), retrievedPayload.getDescription().getLanguage());
    assertFalse(Utils.isNullOrEmpty(userMessage.getMessageProperties()));
    assertTrue(CompareUtils.areEqual(TestData.userMsg1.getMessageProperties().iterator().next(),
                                     userMessage.getMessageProperties().iterator().next()));
    // Test also the Receipt (should be already loaded on query)
    result =  queryManager.getMessageUnitsWithId(TestData.receipt1.getMessageId());
    assertFalse(Utils.isNullOrEmpty(result));
    ReceiptEntity receipt = (ReceiptEntity) result.iterator().next();
    assertTrue(receipt.isLoadedCompletely());
    assertNull(receipt.getContent());
    // Test the Error (needs loading)
    result =  queryManager.getMessageUnitsWithId(TestData.error3.getMessageId());
    assertFalse(Utils.isNullOrEmpty(result));
    ErrorMessageEntity error = (ErrorMessageEntity) result.iterator().next();
    assertFalse(error.isLoadedCompletely());
    queryManager.ensureCompletelyLoaded(error);
    assertTrue(error.isLoadedCompletely());
    // Also check that all info is indeed available
    assertFalse(Utils.isNullOrEmpty(error.getErrors()));
    IEbmsError originalError = TestData.error3.getErrors().iterator().next();
    IEbmsError retrievedError = error.getErrors().iterator().next();
    assertEquals(originalError.getErrorCode(), retrievedError.getErrorCode());
    assertEquals(originalError.getErrorDetail(), retrievedError.getErrorDetail());
}