package org.buddycloud.channelserver.channel;

import java.util.concurrent.LinkedBlockingQueue;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Packet;
import org.buddycloud.channelserver.packetHandler.iq.TestHandler;
import org.buddycloud.channelserver.packetprocessor.iq.namespace.pubsub.get.RepliesGet;
import org.dom4j.Element;

import junit.framework.Assert;
import junit.framework.TestCase;

public class ValidateEntryTest extends TestHandler {

	ValidateEntry validateEntry;
	IQ request;
	Element entry;

	@Before
	public void setUp() throws Exception {
		request = readStanzaAsIq("/iq/pubsub/publish/request.stanza");
		entry = request.getChildElement().element("publish").element("item")
				.element("entry");
	}

	@Test
	public void notProvidingAnEntryReturnsError() throws Exception {

		validateEntry = new ValidateEntry(null);
		Assert.assertFalse(validateEntry.isValid());
	}

	@Test
	public void missingIdAttributeGetsAdded() throws Exception {

		Assert.assertEquals("96da02ee1baef61e767742844207bec4",
				entry.elementText("id"));

		Element entry = (Element) this.entry.clone();
		entry.element("id").detach();
		validateEntry = new ValidateEntry(entry);
		Assert.assertTrue(validateEntry.isValid());
		Assert.assertEquals("1", entry.elementText("id"));
	}

	@Test
	public void emptyIdElementHasValueAdded() throws Exception {

		Assert.assertEquals("96da02ee1baef61e767742844207bec4",
				entry.elementText("id"));

		Element entry = (Element) this.entry.clone();
		entry.element("id").detach();
		entry.addElement("id");

		validateEntry = new ValidateEntry(entry);
		Assert.assertTrue(validateEntry.isValid());
		Assert.assertEquals("1", entry.elementText("id"));
	}

	@Test
	public void missingTitleElementIsAdded() throws Exception {

		Assert.assertEquals("Post title", entry.elementText("title"));

		Element entry = (Element) this.entry.clone();
		entry.element("title").detach();
		validateEntry = new ValidateEntry(entry);
		Assert.assertTrue(validateEntry.isValid());
		Assert.assertEquals("Post", entry.elementText("title"));
	}

	@Test
	public void missingContentElementReturnsInvalid() throws Exception {

		Assert.assertNotNull(entry.element("content"));

		Element entry = (Element) this.entry.clone();
		entry.element("content").detach();
		validateEntry = new ValidateEntry(entry);
		Assert.assertFalse(validateEntry.isValid());
		Assert.assertEquals(ValidateEntry.MISSING_CONTENT_ELEMENT,
				validateEntry.getErrorMessage());
	}
	
	@Test
	public void missingUpdatedElementHasValueAdded() throws Exception {

		Assert.assertEquals("2014-01-01T00:00:00.000Z",
				entry.elementText("updated"));

		Element entry = (Element) this.entry.clone();
		entry.element("updated").detach();
		validateEntry = new ValidateEntry(entry);
		Assert.assertTrue(validateEntry.isValid());
		Assert.assertTrue(entry.elementText("updated").matches("[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z"));
	}
}

@@ -470,16 +470,245 @@
 tCase;%0A%0A
+/**%0A * TODO Additional work required:%0A * %0A * - Check for (and add) activity stream verb%0A * - Check for (and add) activity stream object type%0A * - Check for (and add) content type (accept 'text' and 'html', default 'text')%0A *%0A */%0A
 public c
@@ -787,17 +787,65 @@
 ry;%0A%09IQ 
-r
+publishRequest;%0A%09Element publishEntry;%0A%09IQ replyR
 equest;%0A
@@ -849,25 +849,30 @@
 t;%0A%09Element 
-e
+replyE
 ntry;%0A%0A%09@Bef
@@ -917,17 +917,24 @@
 ion %7B%0A%09%09
-r
+publishR
 equest =
@@ -985,25 +985,206 @@
 stanza%22);%0A%09%09
-e
+publishEntry = publishRequest.getChildElement().element(%22publish%22).element(%22item%22)%0A%09%09%09%09.element(%22entry%22);%0A%09%09replyRequest = readStanzaAsIq(%22/iq/pubsub/publish/reply.stanza%22);%0A%09%09replyE
 ntry = reque
@@ -1180,16 +1180,21 @@
 try = re
+plyRe
 quest.ge
@@ -1563,33 +1563,40 @@
 44207bec4%22,%0A%09%09%09%09
-e
+publishE
 ntry.elementText
@@ -1630,33 +1630,40 @@
  (Element) this.
-e
+publishE
 ntry.clone();%0A%09%09
@@ -1967,25 +1967,32 @@
 7bec4%22,%0A%09%09%09%09
-e
+publishE
 ntry.element
@@ -2030,33 +2030,40 @@
  (Element) this.
-e
+publishE
 ntry.clone();%0A%09%09
@@ -2371,17 +2371,24 @@
 title%22, 
-e
+publishE
 ntry.ele
@@ -2433,33 +2433,40 @@
  (Element) this.
-e
+publishE
 ntry.clone();%0A%09%09
@@ -2752,17 +2752,24 @@
 NotNull(
-e
+publishE
 ntry.ele
@@ -2812,33 +2812,40 @@
  (Element) this.
-e
+publishE
 ntry.clone();%0A%09%09

Support publish and reply requests