// ////////////////////////////////////////////////
 // ////////////////////////////////////////////////
 // Test Methods
 // ////////////////////////////////////////////////
 // ////////////////////////////////////////////////
 @Test
 public void QueryDatabaseConfigurationFileConfiguredCorrectly() throws IOException {
   try {
     DataSource queryDataSource = Config.userDatabaseDataSource();
     assertNotNull(queryDataSource);
   } catch (MalformedConfigException e) {
     fail("Configuration file missing fields.");
   }
 }
 // ////////////////////////////////////////////////
 // ////////////////////////////////////////////////
 // Test Methods
 // ////////////////////////////////////////////////
 // ////////////////////////////////////////////////
 @Test
 public void LemnaTecDatabaseConfigurationFileConfiguredCorrectly() throws IOException {
   try {
     DataSource lemnaTecDatabase = Config.experimentDataSource("LTSystem");
     assertNotNull(lemnaTecDatabase);
   } catch (MalformedConfigException e) {
     fail("Configuration file missing fields.");
   }
 }
  // ////////////////////////////////////////////////
  // ////////////////////////////////////////////////
  // Database Access Helper Methods
  // ////////////////////////////////////////////////
  // ////////////////////////////////////////////////
  private List<String> GetDatabaseNames() throws MalformedConfigException, IOException {
    DataSource lemnaTecDatabase = Config.experimentDataSource("LTSystem");

    String getDatabases = "SELECT name FROM ltdbs WHERE removed = FALSE";

    JdbcTemplate jdbcTemplate = new JdbcTemplate(lemnaTecDatabase);
    List<String> databaseNames = jdbcTemplate.query(getDatabases, new ParseLemnaTecDatabaseNames());

    return databaseNames;
  }
  private List<String> GetTableNames(String databaseName)
      throws MalformedConfigException, IOException {
    DataSource experimentDatabase = Config.experimentDataSource(databaseName);

    String getTables = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES";

    JdbcTemplate jdbcTemplate = new JdbcTemplate(experimentDatabase);
    List<String> tableNames = jdbcTemplate.query(getTables, new ParsePostgreSQLTableNames());

    return tableNames;
  }
  private List<String> GetColumnNames(String table, String databaseName)
      throws MalformedConfigException, IOException {
    DataSource experimentDatabase = Config.experimentDataSource(databaseName);

    String getColumns =
        "SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '" + table + "'";

    JdbcTemplate jdbcTemplate = new JdbcTemplate(experimentDatabase);
    List<String> columnNames = jdbcTemplate.query(getColumns, new ParsePostgreSQLColumnNames());

    return columnNames;
  }
  @Test
  public void QueryServerHasCorrectTables() throws IOException {
    try {
      DataSource queryDataSource = Config.userDatabaseDataSource();

      String getTables = "SHOW TABLES";

      JdbcTemplate queryDatabase = new JdbcTemplate(queryDataSource);
      List<String> tableNames = queryDatabase.query(getTables, new ParseMySQLTableNames());

      ListAssert.assertContains(tableNames, QueryDaoImpl.QUERY_TABLE);
    } catch (MalformedConfigException e) {
      fail("Configuration file missing fields.");
    }
  }
  @Test
  public void QueryTableHasCorrectColumns() throws IOException {
    try {
      DataSource queryDataSource = Config.userDatabaseDataSource();

      String getColumns = "DESCRIBE " + QueryDaoImpl.QUERY_TABLE;

      JdbcTemplate queryDatabase = new JdbcTemplate(queryDataSource);
      List<MySQLColumn> columns = queryDatabase.query(getColumns, new ParseMySQLColumns());

      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.QUERY_ID,
              MySQLColumn.INT_UNSIGNED(10),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.PRIMARY_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.METADATA_ID,
              MySQLColumn.INT_UNSIGNED(10),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.EXPERIMENT,
              MySQLColumn.VARCHAR(255),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.BARCODE,
              MySQLColumn.VARCHAR(255),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.MEASUREMENT,
              MySQLColumn.VARCHAR(255),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.START_TIME,
              MySQLColumn.DATETIME,
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.END_TIME,
              MySQLColumn.DATETIME,
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.WATERING,
              MySQLColumn.TINYINT(1),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.VISIBLE,
              MySQLColumn.TINYINT(1),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.FLUORESCENT,
              MySQLColumn.TINYINT(1),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.INFRARED,
              MySQLColumn.TINYINT(1),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.NOT_KEY));
    } catch (MalformedConfigException e) {
      fail("Configuration file missing fields.");
    }
  }
  @Test
  public void QueryMetadataTableHasCorrectColumns() throws IOException {
    try {
      DataSource queryDataSource = Config.userDatabaseDataSource();

      String getColumns = "DESCRIBE " + QueryDaoImpl.METADATA_TABLE;

      JdbcTemplate queryDatabase = new JdbcTemplate(queryDataSource);
      List<MySQLColumn> columns = queryDatabase.query(getColumns, new ParseMySQLColumns());

      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.METADATA_ID,
              MySQLColumn.INT_UNSIGNED(10),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.PRIMARY_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.QUERY_ID,
              MySQLColumn.INT_UNSIGNED(10),
              MySQLColumn.NEVER_NULL,
              MySQLColumn.MULTIPLE_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.USER_ID,
              MySQLColumn.INT_UNSIGNED(10),
              MySQLColumn.CAN_NULL,
              MySQLColumn.MULTIPLE_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.COMMENT,
              MySQLColumn.TEXT,
              MySQLColumn.CAN_NULL,
              MySQLColumn.MULTIPLE_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.DATE_MADE,
              MySQLColumn.DATETIME,
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.DOWNLOAD_BEGIN,
              MySQLColumn.DATETIME,
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.DOWNLOAD_END,
              MySQLColumn.DATETIME,
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.INTERRUPTED,
              MySQLColumn.TINYINT(1),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.MISSED_SNAPSHOTS,
              MySQLColumn.TEXT,
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.SIZE,
              MySQLColumn.BIGINT(20),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.NUM_SNAPSHOTS,
              MySQLColumn.INT(11),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
      ListAssert.assertContains(
          columns,
          new MySQLColumn(
              QueryDaoImpl.NUM_TILES,
              MySQLColumn.INT(11),
              MySQLColumn.CAN_NULL,
              MySQLColumn.NOT_KEY));
    } catch (MalformedConfigException e) {
      fail("Configuration file missing fields.");
    }
  }