package com.gentics.mesh.search;

import static com.gentics.mesh.test.ClientHelper.call;
import static com.gentics.mesh.test.TestDataProvider.PROJECT_NAME;
import static com.gentics.mesh.test.TestSize.FULL;
import static com.gentics.mesh.test.context.MeshTestHelper.getSimpleTermQuery;
import static org.junit.Assert.assertEquals;

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

import com.gentics.madl.tx.Tx;
import com.gentics.mesh.core.rest.tag.TagListResponse;
import com.gentics.mesh.test.context.AbstractMeshTest;
import com.gentics.mesh.test.context.MeshTestSetting;
import com.gentics.mesh.test.definition.BasicSearchCrudTestcases;

import static com.gentics.mesh.test.context.ElasticsearchTestMode.CONTAINER;
@MeshTestSetting(elasticsearch = CONTAINER, startServer = true, testSize = FULL)
public class TagSearchEndpointTest extends AbstractMeshTest implements BasicSearchCrudTestcases {

	@Test
	@Override
	public void testDocumentCreation() throws InterruptedException, JSONException {
		String tagName = "newtag";
		try (Tx tx = tx()) {
			createTag(PROJECT_NAME, tagFamily("colors").getUuid(), tagName);
		}

		waitForSearchIdleEvent();

		TagListResponse list = call(() -> client().searchTags(getSimpleTermQuery("name.raw", tagName)));
		assertEquals(1, list.getData().size());
	}

	@Test
	@Override
	public void testDocumentUpdate() throws InterruptedException, JSONException {
		String uuid = db().tx(() -> tag("red").getUuid());
		String parentTagFamilyUuid = db().tx(() -> tagFamily("colors").getUuid());

		String newName = "redish";
		updateTag(PROJECT_NAME, parentTagFamilyUuid, uuid, newName);
		updateTag(PROJECT_NAME, parentTagFamilyUuid, uuid, newName + "2");

		try (Tx tx = tx()) {
			assertEquals("The tag name was not updated as expected.", newName + "2", tag("red").getName());
		}

		waitForSearchIdleEvent();

		TagListResponse list = call(() -> client().searchTags(getSimpleTermQuery("name.raw", newName + "2")));
		assertEquals(1, list.getData().size());
	}

	@Test
	@Override
	public void testDocumentDeletion() throws Exception {
		try (Tx tx = tx()) {
			recreateIndices();
		}

		String name = db().tx(() -> tag("red").getName());
		String uuid = db().tx(() -> tag("red").getUuid());
		String parentTagFamilyUuid = db().tx(() -> tagFamily("colors").getUuid());

		waitForSearchIdleEvent();

		// 1. Verify that the tag is indexed
		TagListResponse list = call(() -> client().searchTags(getSimpleTermQuery("name.raw", name)));
		assertEquals("The tag with name {" + name + "} and uuid {" + uuid + "} could not be found in the search index.", 1, list.getData().size());

		// 2. Delete the tag
		call(() -> client().deleteTag(PROJECT_NAME, parentTagFamilyUuid, uuid));

		waitForSearchIdleEvent();

		// 3. Search again and verify that the document was removed from the index
		list = call(() -> client().searchTags(getSimpleTermQuery("fields.name", name)));
		assertEquals(0, list.getData().size());
	}

}

@@ -737,16 +737,17 @@
 TAINER;%0A
+%0A
 @MeshTes
@@ -1421,37 +1421,32 @@
 %09%09String uuid = 
-db().
 tx(() -%3E tag(%22re
@@ -1484,37 +1484,32 @@
 TagFamilyUuid = 
-db().
 tx(() -%3E tagFami
@@ -2142,21 +2142,16 @@
  name = 
-db().
 tx(() -%3E
@@ -2186,29 +2186,24 @@
 ring uuid = 
-db().
 tx(() -%3E tag
@@ -2257,13 +2257,8 @@
 d = 
-db().
 tx((

Remove bogus db calls