@Test(expected = RepositoryException.class)
  public void testGetWorkflowsNullRepository() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);
    List flows = repo.getWorkflows();

    assertNotNull(flows);
  }
  @Test(expected = RepositoryException.class)
  public void testGetTasksByWorkflowNameNoDataSource() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);
    List<WorkflowTask> tasks = repo.getTasksByWorkflowName(null);

    assertThat(tasks, allOf(notNullValue(), hasSize(2)));
  }
  @Test
  public void testGetWorkflows() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<Workflow> flows = repo.getWorkflows();

    assertThat(flows, allOf(notNullValue(), hasSize(1)));
  }
  @Test
  public void testDataSourceRepo() throws SQLException, RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    // test id 1
    WorkflowCondition wc = repo.getWorkflowConditionById("1");
    assertEquals(wc.getConditionName(), "CheckCond");
    WorkflowConditionInstance condInst =
        GenericWorkflowObjectFactory.getConditionObjectFromClassName(
            wc.getConditionInstanceClassName());
    Metadata m = new Metadata();
    m.addMetadata("Met1", "Val1");
    m.addMetadata("Met2", "Val2");
    m.addMetadata("Met3", "Val3");
    assertTrue(condInst.evaluate(m, wc.getTaskConfig()));

    // test id 2
    wc = repo.getWorkflowConditionById("2");
    assertEquals(wc.getConditionName(), "FalseCond");
    condInst =
        GenericWorkflowObjectFactory.getConditionObjectFromClassName(
            wc.getConditionInstanceClassName());
    assertFalse(condInst.evaluate(m, wc.getTaskConfig()));

    // test id 3
    wc = repo.getWorkflowConditionById("3");
    assertEquals(wc.getConditionName(), "TrueCond");
    condInst =
        GenericWorkflowObjectFactory.getConditionObjectFromClassName(
            wc.getConditionInstanceClassName());
    assertTrue(condInst.evaluate(m, wc.getTaskConfig()));
  }
  @Test
  public void testGetTasksByWorkflowNameNull() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<WorkflowTask> tasks = repo.getTasksByWorkflowName(null);

    assertThat(tasks, nullValue());
  }
  @Test(expected = RepositoryException.class)
  public void testGetConditionsByTaskIdNoDataSource() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);
    List<WorkflowCondition> workflow = repo.getConditionsByTaskName("1");

    assertThat(workflow, allOf(notNullValue(), hasSize(1)));
    assertThat(workflow.get(0).getConditionName(), equalTo("Test Condition"));
  }
  @Test
  public void testGetRegisteredEvents() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<String> workflow = repo.getRegisteredEvents();

    assertThat(workflow, allOf(notNullValue(), hasSize(1)));
    assertThat(workflow.get(0), equalTo("event"));
  }
  @Test
  public void testGetTaskById() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    WorkflowTask task = repo.getTaskById("1");

    assertThat(task, notNullValue());
    assertThat(task.getTaskName(), allOf(notNullValue(), equalTo("Test Task")));
  }
  @Test
  public void testGetWorkflowByNameIncorrect() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    Workflow w = repo.getWorkflowByName("Broken Workflow");

    assertNull(w);
  }
  @Test(expected = RepositoryException.class)
  public void testGetTaskByIdNoDataSource() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);
    WorkflowTask task = repo.getTaskById("1");

    assertThat(task, notNullValue());
    assertThat(task.getTaskName(), allOf(notNullValue(), equalTo("Test")));
  }
  @Test(expected = RepositoryException.class)
  public void testGetworkflowByIdNoDataSource() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);

    Workflow w = repo.getWorkflowById("1");

    assertNull(w);
  }
  @Test
  public void testGetworkflowByIncorredId() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    Workflow w = repo.getWorkflowById("100");

    assertNull(w);
  }
  @Test
  public void testGetWorkflowsForEvent() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<Workflow> workflow = repo.getWorkflowsForEvent("event");

    assertThat(workflow, allOf(notNullValue(), hasSize(1)));

    assertThat(workflow.get(0).getName(), equalTo("Test Workflow"));
  }
  @Test
  public void testGetConditions() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<WorkflowCondition> task = repo.getConditions();

    assertThat(task, allOf(notNullValue(), hasSize(4)));
    assertThat(task.get(0).getConditionName(), allOf(notNullValue(), equalTo("CheckCond")));
  }
  @Test(expected = RepositoryException.class)
  public void testGetConfigurationByTaskIdNoDataSource() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);
    WorkflowTaskConfiguration workflow = repo.getConfigurationByTaskId("1");

    assertThat(workflow, notNullValue());
    assertThat(workflow.getProperties(), notNullValue());
    assertThat(workflow.getProperties().getProperty("test"), notNullValue());
  }
  @Test
  public void testGetConfigurationByTaskId() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    WorkflowTaskConfiguration workflow = repo.getConfigurationByTaskId("1");

    assertThat(workflow, notNullValue());
    assertThat(workflow.getProperties(), notNullValue());
    assertThat(workflow.getProperties().getProperty("TestProp"), notNullValue());
  }
  @Test
  public void testGetConditionsByTaskId() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<WorkflowCondition> workflow = repo.getConditionsByTaskId("1");

    assertThat(workflow, allOf(notNullValue(), hasSize(1)));
    assertThat(workflow.get(0).getConditionName(), equalTo("TrueCond"));
  }
  @Test
  public void testGetworkflowByName() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    Workflow w = repo.getWorkflowByName("Test Workflow");

    assertNotNull(w);

    assertThat("Test Workflow", equalTo(w.getName()));
  }
  @Test(expected = RepositoryException.class)
  public void testGetWorkflowsForEventNoDataSource() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);
    List<Workflow> workflow = repo.getWorkflowsForEvent("event", false, false);

    assertThat(workflow, allOf(notNullValue(), hasSize(2)));

    assertThat(workflow.get(0).getPreConditions(), allOf(notNullValue(), hasSize(0)));
    assertThat(workflow.get(0).getPostConditions(), allOf(notNullValue(), hasSize(0)));
  }
  @Test
  public void testGetworkflowsForEventNoTasksOrConditions() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<Workflow> workflow = repo.getWorkflowsForEvent("event", false, false);

    assertThat(workflow, allOf(notNullValue(), hasSize(1)));

    assertThat(workflow.get(0).getPreConditions(), allOf(notNullValue(), hasSize(0)));
    assertThat(workflow.get(0).getPostConditions(), allOf(notNullValue(), hasSize(0)));
  }
  @Test
  public void testGetTaskByWorkflowName() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<WorkflowTask> tasks = repo.getTasksByWorkflowName("Test Workflow");

    assertThat(tasks, allOf(notNullValue(), hasSize(2)));

    assertThat(tasks.get(0).getTaskName(), equalTo("Test Task"));

    assertThat(tasks.get(1).getTaskName(), equalTo("Test Task2"));
  }
  @Test
  public void testGetWorkflowsNoConditionsOrTasks() throws RepositoryException {

    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    List<Workflow> flows = repo.getWorkflows(false, false);

    assertThat(flows, allOf(notNullValue(), hasSize(1)));

    assertThat(flows.get(0).getPreConditions(), hasSize(0));

    assertThat(flows.get(0).getPostConditions(), hasSize(0));

    assertThat(flows.get(0).getTasks(), hasSize(0));
  }
  /** @since OODT-205 */
  @Test
  public void testWorkflowConditions() {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);

    Workflow w = null;
    try {
      w = repo.getWorkflowById("1");
    } catch (Exception e) {
      fail(e.getMessage());
    }

    assertNotNull(w);
    assertNotNull(w.getConditions());
    assertTrue(w.getConditions().size() > 0);
    assertEquals(w.getConditions().size(), 1);
  }
  @Test
  public void testAddTask() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    WorkflowTask t = new WorkflowTask();
    t.setTaskName("Manual");
    t.setTaskId("50");
    t.setPreConditions(Collections.EMPTY_LIST);
    t.setPostConditions(Collections.EMPTY_LIST);
    String workflow = repo.addTask(t);

    assertThat(workflow, notNullValue());
    assertThat(workflow, equalTo("3"));

    // TODO GET TASK BACK

  }
  @Test
  public void testAddWorkflow() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(ds);
    Workflow w = new Workflow();
    w.setId("50");
    w.setName("Manual");
    WorkflowTask t = new WorkflowTask();
    t.setTaskId("1");
    List<WorkflowTask> l = new ArrayList<WorkflowTask>();
    l.add(t);
    w.setTasks(l);
    String workflow = repo.addWorkflow(w);

    assertThat(workflow, notNullValue());
    assertThat(workflow, equalTo("50"));

    // TODO GET WORKFLOW

  }
  @Test(expected = RepositoryException.class)
  public void testGetWorkflowByNameNoDataSource() throws RepositoryException {
    DataSourceWorkflowRepository repo = new DataSourceWorkflowRepository(null);

    repo.getWorkflowByName("Broken Workflow");
  }