package beaform.dao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;

import beaform.debug.DebugUtils;
import beaform.entities.FormulaTag;

/**
 * Test for the tag DAO.
 *
 * @author Steven Post
 *
 */
@SuppressWarnings("static-method")
public class FormulaTagDAOTest {

	@Before
	public void setUp() {
		GraphDbHandler.initInstance("neo4j_test/db");
		DebugUtils.clearDb();
	}

	@Test
	public void testFindOrCreateWithoutData() {
		String name;

		final GraphDatabaseService graphDb = GraphDbHandler.getInstance().getService();
		try (Transaction tx = graphDb.beginTx()) {
			Node tag = FormulaTagDAO.findOrCreate(new FormulaTag("First"));
			name = (String) tag.getProperty("name");
			tx.success();
		}

		assertNotNull("The tag wasn't found", name);
		assertEquals("This isn't the expected formula", "First", name);
	}

	@Test
	public void testFindOrCreateWithData() {
		String name;

		DebugUtils.fillDb();
		final GraphDatabaseService graphDb = GraphDbHandler.getInstance().getService();
		try (Transaction tx = graphDb.beginTx()) {
			Node tag = FormulaTagDAO.findOrCreate(new FormulaTag("First"));
			name = (String) tag.getProperty("name");
			tx.success();
		}

		assertNotNull("The tag wasn't found", name);
		assertEquals("This isn't the expected formula", "First", name);
	}

	@Test
	public void testFindByName() {
		String name;

		DebugUtils.fillDb();
		final GraphDatabaseService graphDb = GraphDbHandler.getInstance().getService();
		try (Transaction tx = graphDb.beginTx()) {
			Node tag = FormulaTagDAO.findByName("First");
			name = (String) tag.getProperty("name");
			tx.success();
		}

		assertNotNull("The tag wasn't found", name);
		assertEquals("This isn't the expected formula", "First", name);
	}

	@After
	public void tearDown() {
		DebugUtils.clearDb();
	}

}

@@ -1952,16 +1952,328 @@
 e);%0A%09%7D%0A%0A
+%09@Test(expected=InvalidFormulaException.class)%0A%09public void testNodeToTagWithInvalid() %7B%0A%09%09final GraphDatabaseService graphDb = GraphDbHandler.getInstance().getService();%0A%09%09try (Transaction tx = graphDb.beginTx()) %7B%0A%09%09%09Node node = graphDb.createNode();%0A%09%09%09FormulaTagDAO.nodeToTag(node);%0A%09%09%09tx.success();%0A%09%09%7D%0A%09%7D%0A%0A
 %09@After%0A

Add test for trying to convert an invalid formula