package com.gentics.mesh.search;

import static com.gentics.mesh.util.MeshAssert.assertSuccess;
import static com.gentics.mesh.util.MeshAssert.latchFor;
import static org.junit.Assert.assertEquals;

import org.codehaus.jettison.json.JSONException;
import org.junit.Test;

import com.gentics.mesh.core.rest.group.GroupListResponse;
import com.gentics.mesh.core.rest.group.GroupResponse;
import com.gentics.mesh.rest.client.MeshResponse;

public class GroupSearchEndpointTest extends AbstractSearchEndpointTest implements BasicSearchCrudTestcases {

	@Test
	@Override
	public void testDocumentCreation() throws InterruptedException, JSONException {
		String groupName = "testgroup42a";
		createGroup(groupName);

		MeshResponse<GroupListResponse> searchFuture = getClient().searchGroups(getSimpleTermQuery("name", groupName)).invoke();
		latchFor(searchFuture);
		assertSuccess(searchFuture);
		assertEquals(1, searchFuture.result().getData().size());
	}

	@Test
	public void testSearchByUuid() throws InterruptedException, JSONException {
		String groupName = "testgroup42a";
		String uuid = createGroup(groupName).getUuid();

		MeshResponse<GroupListResponse> searchFuture = getClient().searchGroups(getSimpleTermQuery("uuid", uuid)).invoke();
		latchFor(searchFuture);
		assertSuccess(searchFuture);
		assertEquals(1, searchFuture.result().getData().size());
		assertEquals(uuid, searchFuture.result().getData().get(0).getUuid());
	}

	@Test
	@Override
	public void testDocumentDeletion() throws InterruptedException, JSONException {
		String groupName = "testgroup42a";

		GroupResponse group = createGroup(groupName);
		deleteGroup(group.getUuid());

		MeshResponse<GroupListResponse> searchFuture = getClient().searchGroups(getSimpleTermQuery("name", groupName)).invoke();
		latchFor(searchFuture);
		assertSuccess(searchFuture);
		assertEquals(0, searchFuture.result().getData().size());
	}

	@Test
	@Override
	public void testDocumentUpdate() throws InterruptedException, JSONException {
		String groupName = "testgroup42a";
		GroupResponse group = createGroup(groupName);

		String newGroupName = "testgrouprenamed";
		updateGroup(group.getUuid(), newGroupName);

		MeshResponse<GroupListResponse> searchFuture = getClient().searchGroups(getSimpleTermQuery("name", groupName)).invoke();
		latchFor(searchFuture);
		assertSuccess(searchFuture);
		assertEquals(0, searchFuture.result().getData().size());

		searchFuture = getClient().searchGroups(getSimpleTermQuery("name", newGroupName)).invoke();
		latchFor(searchFuture);
		assertSuccess(searchFuture);
		assertEquals(1, searchFuture.result().getData().size());
	}

}

@@ -27,16 +27,82 @@
 earch;%0A%0A
+import static com.gentics.mesh.assertj.MeshAssertions.assertThat;%0A
 import s
@@ -1183,37 +1183,24 @@
 tUuid();%0A%0A%09%09
-MeshResponse%3C
 GroupListRes
@@ -1204,32 +1204,36 @@
 Response
-%3E searchFuture =
+ result = call(() -%3E
  getClie
@@ -1287,115 +1287,417 @@
 id))
-.invoke();%0A%09%09latchFor(searchFuture);%0A%09%09assertSuccess(searchFuture
+);%0A%09%09assertThat(result.getData()).hasSize(1);%0A%09%09assertEquals(uuid, result.getData().get(0).getUuid());%0A%09%7D%0A%0A%09@Test%0A%09public void testSearchByName() throws InterruptedException, JSONException %7B%0A%09%09String groupName = %22test-grou  %25!p42a%22;%0A%09%09String uuid = createGroup(groupName).getUuid(
 );%0A
+%0A
 %09%09
-assertEquals(1, searchFuture.
+GroupListResponse result = call(() -%3E getClient().searchGroups(getSimpleTermQuery(%22name%22, groupName)));%0A%09%09assertThat(
 result
-()
 .get
@@ -1698,31 +1698,35 @@
 lt.getData()
-.s
+).hasS
 ize(
-)
+1
 );%0A%09%09assertE
@@ -1737,37 +1737,22 @@
 s(uuid, 
-searchFuture.
 result
-()
 .getData
@@ -1993,37 +1993,24 @@
 Uuid());%0A%0A%09%09
-MeshResponse%3C
 GroupListRes
@@ -2014,32 +2014,36 @@
 Response
-%3E searchFuture =
+ result = call(() -%3E
  getClie
@@ -2102,115 +2102,30 @@
 me))
-.invoke();%0A%09%09latchFor(searchFuture);%0A%09%09assertSuccess(searchFuture);%0A%09%09assertEquals(0, searchFuture.
+);%0A%09%09assertThat(
 result
-()
 .get
@@ -2122,39 +2122,43 @@
 result.getData()
-.s
+).hasS
 ize(
-)
+0
 );%0A%09%7D%0A%0A%09@Test%0A%09@
@@ -2420,37 +2420,24 @@
 upName);%0A%0A%09%09
-MeshResponse%3C
 GroupListRes
@@ -2441,32 +2441,36 @@
 Response
-%3E searchFuture =
+ result = call(() -%3E
  getClie
@@ -2529,115 +2529,30 @@
 me))
-.invoke();%0A%09%09latchFor(searchFuture);%0A%09%09assertSuccess(searchFuture);%0A%09%09assertEquals(0, searchFuture.
+);%0A%09%09assertThat(
 result
-()
 .get
@@ -2561,35 +2561,44 @@
 ta()
-.s
+).hasS
 ize(
-)
+0
 );%0A%0A%09%09
-searchFuture =
+result = call(() -%3E
  get
@@ -2664,115 +2664,30 @@
 me))
-.invoke();%0A%09%09latchFor(searchFuture);%0A%09%09assertSuccess(searchFuture);%0A%09%09assertEquals(1, searchFuture.
+);%0A%09%09assertThat(
 result
-()
 .get
@@ -2688,28 +2688,32 @@
 lt.getData()
-.s
+).hasS
 ize(
-)
+1
 );%0A%09%7D%0A%0A%7D%0A

Add test