package com.gentics.mesh.core.verticle.node;

import static com.gentics.mesh.demo.DemoDataProvider.PROJECT_NAME;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import io.vertx.core.Future;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.gentics.mesh.api.common.PagingInfo;
import com.gentics.mesh.core.AbstractWebVerticle;
import com.gentics.mesh.core.data.node.Node;
import com.gentics.mesh.core.rest.node.NodeListResponse;
import com.gentics.mesh.core.rest.node.NodeRequestParameters;
import com.gentics.mesh.core.rest.node.NodeResponse;
import com.gentics.mesh.core.verticle.project.ProjectNodeVerticle;
import com.gentics.mesh.test.AbstractRestVerticleTest;
//import com.gentics.mesh.util.DataHelper;

public class ProjectNodeChildrenVerticleTest extends AbstractRestVerticleTest {

	@Autowired
	private ProjectNodeVerticle verticle;

//	@Autowired
//	private DataHelper helper;

	@Override
	public AbstractWebVerticle getVerticle() {
		return verticle;
	}

	@Test
	public void testReadNodeByUUIDAndCheckChildren() throws Exception {
		Node node = data().getFolder("2015");
		assertNotNull(node);
		assertNotNull(node.getUuid());

		Future<NodeResponse> future = getClient().findNodeByUuid(PROJECT_NAME, node.getUuid());
		latchFor(future);
		assertSuccess(future);
		NodeResponse restNode = future.result();
		test.assertMeshNode(node, restNode);
		assertTrue(restNode.isContainer());
		assertTrue(restNode.getChildren().size() > 5);
	}

	@Test
	public void testReadNodeByUUIDAndCheckChildren2() throws Exception {
		Node node = data().getContent("boeing 737");
		assertNotNull(node);
		assertNotNull(node.getUuid());

		Future<NodeResponse> future = getClient().findNodeByUuid(PROJECT_NAME, node.getUuid());
		latchFor(future);
		assertSuccess(future);
		NodeResponse restNode = future.result();
		test.assertMeshNode(node, restNode);
		assertFalse(restNode.isContainer());
		assertNull(restNode.getChildren());
	}

	@Test
	public void testReadNodeChildren() throws Exception {
		Node node = data().getFolder("news");
		assertNotNull(node);
		assertNotNull(node.getUuid());

		int expectedItemsInPage = node.getChildren().size() > 25 ? 25 : node.getChildren().size();

		Future<NodeListResponse> future = getClient().findNodeChildren(PROJECT_NAME, node.getUuid(), new PagingInfo(), new NodeRequestParameters());
		latchFor(future);
		assertSuccess(future);

		NodeListResponse nodeList = future.result();
		assertEquals(node.getChildren().size(), nodeList.getMetainfo().getTotalCount());
		assertEquals(expectedItemsInPage, nodeList.getData().size());
	}

}

@@ -533,32 +533,75 @@
 actWebVerticle;%0A
+import com.gentics.mesh.core.data.Project;%0A
 import com.genti
@@ -629,16 +629,16 @@
 e.Node;%0A
-
 import c
@@ -923,16 +923,17 @@
 leTest;%0A
+%0A
 //import
@@ -1105,53 +1105,8 @@
 e;%0A%0A
-//%09@Autowired%0A//%09private DataHelper helper;%0A%0A
 %09@Ov
@@ -1156,16 +1156,16 @@
 cle() %7B%0A
-
 %09%09return
@@ -1175,24 +1175,278 @@
 rticle;%0A%09%7D%0A%0A
+%09@Test%0A%09public void testReadChildrenOfBaseNode() %7B%0A%09%09Project project = data().getProject();%0A%09%09Future%3CNodeListResponse%3E future = getClient().findNodeChildren(PROJECT_NAME, project.getBaseNode().getUuid());%0A%09%09latchFor(future);%0A%09%09assertSuccess(future);%0A%09%7D%0A%0A
 %09@Test%0A%09publ

Add extra testcase