@Test
public void setPayloadInformation() throws PersistenceException {

    // Because payload data is lazily loaded, we need to completely load the JPA object using the QueryManager
    QueryManager    queryManager = new QueryManager();
    // Add a message unit to the database so we can change it
    em.getTransaction().begin();
    UserMessage userMsgJPA = new UserMessage(TestData.userMsg5);
    em.persist(userMsgJPA);
    UserMessageEntity userMsg = new UserMessageEntity(userMsgJPA);
    em.getTransaction().commit();
    // Check it accidentially has not already the new value
    queryManager.ensureCompletelyLoaded(userMsg);
    assertTrue(Utils.isNullOrEmpty(userMsg.getPayloads()));
    // Perform the update
    Collection<IPayload>    newPayloads = new ArrayList<>();
    newPayloads.add(TestData.payload2);
    newPayloads.add(TestData.payload3);
    updManager.setPayloadInformation(userMsg, newPayloads);
    // Check update in entity object
    assertFalse(Utils.isNullOrEmpty(userMsg.getPayloads()));
    assertEquals(2, userMsg.getPayloads().size());
    // Check that database is updated
    em.refresh(userMsgJPA);
    assertFalse(Utils.isNullOrEmpty(userMsgJPA.getPayloads()));
    assertEquals(2, userMsgJPA.getPayloads().size());
    // Change one of the payloads in the updated set
    Iterator<IPayload> it = userMsg.getPayloads().iterator();
    Payload p1 = new Payload(it.next());
    p1.addProperty(new Property(T_NEW_PL_PROP_NAME, T_NEW_PL_PROP_VALUE));
    Payload p2 = new Payload(it.next());
    p2.setContentLocation(T_NEW_CONTENT_LOC);
    newPayloads = new ArrayList<>(2);
    newPayloads.add(p1); newPayloads.add(p2);
    updManager.setPayloadInformation(userMsg, newPayloads);
    // Check update in entity object
    assertFalse(Utils.isNullOrEmpty(userMsg.getPayloads()));
    assertEquals(2, userMsg.getPayloads().size());
    for (IPayload p : userMsg.getPayloads()) {
        if (p.getContainment() == p1.getContainment()) {
            assertFalse(Utils.isNullOrEmpty(p.getProperties()));
            assertEquals(1, p.getProperties().size());
            assertTrue(CompareUtils.areEqual(p.getProperties().iterator().next(),
                                             p1.getProperties().iterator().next()));
        } else if (p.getContainment() == p2.getContainment()) {
            assertTrue(Utils.isNullOrEmpty(p.getProperties()));
            assertEquals(p2.getContentLocation(), p.getContentLocation());
        } else
            fail("unknown payload");
    }
    // Check that database is updated
    em.refresh(userMsgJPA);
    assertFalse(Utils.isNullOrEmpty(userMsgJPA.getPayloads()));
    assertEquals(2, userMsgJPA.getPayloads().size());
    for (IPayload p : userMsgJPA.getPayloads()) {
        if (p.getContainment() == p1.getContainment()) {
            assertFalse(Utils.isNullOrEmpty(p.getProperties()));
            assertEquals(1, p.getProperties().size());
            assertTrue(CompareUtils.areEqual(p.getProperties().iterator().next(),
                                             p1.getProperties().iterator().next()));
        } else if (p.getContainment() == p2.getContainment()) {
            assertTrue(Utils.isNullOrEmpty(p.getProperties()));
            assertEquals(p2.getContentLocation(), p.getContentLocation());
        } else
            fail("unknown payload");
    }
}