@SuppressWarnings({ "unchecked", "rawtypes" })

@Test
public void testCreateNode() throws Exception
{
    when(permissionService.hasPermission(any(NodeRef.class), anyString())).thenReturn(AccessStatus.ALLOWED);
    // Mock serializer since this behaviour is already tested in PublishingPackageSerializerTest.
    ContentWriter writer = mock(ContentWriter.class);
    when(contentService.getWriter(any(NodeRef.class), eq(PROP_PUBLISHING_EVENT_PAYLOAD), eq(true)))
        .thenReturn(writer);
    OutputStream outputStream = mock(OutputStream.class);
    when(writer.getContentOutputStream()).thenReturn(outputStream);
    NodeSnapshotSerializer serializer = mock(NodeSnapshotSerializer.class);
    helper.setSerializer(serializer);
    
    NodeRef queue = new NodeRef("foo://bar/queue");
    NodeRef event = new NodeRef("foo://bar/event");
    
    ChildAssociationRef childAssoc = new ChildAssociationRef(ASSOC_PUBLISHING_EVENT, queue, null, event);
    when(nodeService.createNode(any(NodeRef.class), any(QName.class), any(QName.class), any(QName.class), anyMap()))
        .thenReturn(childAssoc);
    
    String channelId = "test://channel/id";
    Calendar schedule = Calendar.getInstance();
    String comment = "The comment";
    
    PublishingDetails details = new PublishingDetailsImpl()
        .setSchedule(schedule)
        .setPublishChannelId(channelId)
        .setComment(comment);
    
    NodeRef result = helper.createNode(queue, details);
    assertEquals(event, result);
    
    ArgumentCaptor<Map> argument = ArgumentCaptor.forClass(Map.class);
    verify(nodeService)
        .createNode(eq(queue), eq(ASSOC_PUBLISHING_EVENT),
                any(QName.class), eq(TYPE_PUBLISHING_EVENT),
                argument.capture());
    Map<QName, Serializable> props = argument.getValue();
    
    assertNotNull(props.get(ContentModel.PROP_NAME));
    assertEquals(channelId, props.get(PROP_PUBLISHING_EVENT_CHANNEL));
    assertEquals(comment, props.get(PROP_PUBLISHING_EVENT_COMMENT));
    assertEquals(schedule.getTime(), props.get(PROP_PUBLISHING_EVENT_TIME));
    assertEquals(schedule.getTimeZone().getID(), props.get(PROP_PUBLISHING_EVENT_TIME_ZONE));
}