@Test
  public void chekGetAllRowsWithEmptyParam() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    assertEquals(
        new ArrayList<String>(), sqlSourceHelper.getAllRows(new ArrayList<List<Object>>()));
  }
  @Test
  public void checkStatusFileCorrectlyCreated() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    // sqlSourceHelper.setCurrentIndex(10);

    sqlSourceHelper.updateStatusFile();

    File file = new File("/tmp/flume/statusFileName.txt");
    assertEquals(true, file.exists());
    if (file.exists()) {
      file.delete();
      file.getParentFile().delete();
    }
  }
  @Test
  public void checkStatusFileCorrectlyUpdated() throws Exception {

    File file = File.createTempFile("statusFileName", ".txt");

    when(context.getString("status.file.path", "/var/lib/flume")).thenReturn(file.getParent());
    when(context.getString("status.file.name")).thenReturn(file.getName());

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    sqlSourceHelper.setCurrentIndex(10);

    sqlSourceHelper.updateStatusFile();

    SQLSourceHelper sqlSourceHelper2 = new SQLSourceHelper(context, "Source Name");
    assertEquals(10L, sqlSourceHelper2.getCurrentIndex());
  }
  /** Connect to database using hibernate */
  public void establishSession() {

    LOG.info("Opening hibernate session");

    serviceRegistry =
        new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
    factory = config.buildSessionFactory(serviceRegistry);
    session = factory.openSession();
    session.setCacheMode(CacheMode.IGNORE);

    session.setDefaultReadOnly(sqlSourceHelper.isReadOnlySession());
  }
  @SuppressWarnings("deprecation")
  @Test
  public void chekGetAllRows() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    List<List<Object>> queryResult = new ArrayList<List<Object>>(2);
    List<String[]> expectedResult = new ArrayList<String[]>(2);
    String string1 = "string1";
    String string2 = "string2";
    int int1 = 1;
    int int2 = 2;
    Date date1 = new Date(115, 0, 1);
    Date date2 = new Date(115, 1, 2);

    List<Object> row1 = new ArrayList<Object>(3);
    String[] expectedRow1 = new String[3];
    row1.add(string1);
    expectedRow1[0] = string1;
    row1.add(int1);
    expectedRow1[1] = Integer.toString(int1);
    row1.add(date1);
    expectedRow1[2] = date1.toString();
    queryResult.add(row1);
    expectedResult.add(expectedRow1);

    List<Object> row2 = new ArrayList<Object>(3);
    String[] expectedRow2 = new String[3];
    row2.add(string2);
    expectedRow2[0] = string2;
    row2.add(int2);
    expectedRow2[1] = Integer.toString(int2);
    row2.add(date2);
    expectedRow2[2] = date2.toString();
    queryResult.add(row2);
    expectedResult.add(expectedRow2);

    assertArrayEquals(expectedResult.get(0), sqlSourceHelper.getAllRows(queryResult).get(0));
    assertArrayEquals(expectedResult.get(1), sqlSourceHelper.getAllRows(queryResult).get(1));
  }
  /**
   * Constructor to initialize hibernate configuration parameters
   *
   * @param sqlSourceHelper Contains the configuration parameters from flume config file
   */
  public HibernateHelper(SQLSourceHelper sqlSourceHelper) {

    this.sqlSourceHelper = sqlSourceHelper;
    Context context = sqlSourceHelper.getContext();

    Map<String, String> hibernateProperties = context.getSubProperties("hibernate.");
    Iterator<Map.Entry<String, String>> it = hibernateProperties.entrySet().iterator();

    config = new Configuration();
    Map.Entry<String, String> e;

    while (it.hasNext()) {
      e = it.next();
      config.setProperty("hibernate." + e.getKey(), e.getValue());
    }
  }
 @Test
 public void getCustomQuery() {
   when(context.getString("custom.query")).thenReturn("SELECT column FROM table");
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("SELECT column FROM table", sqlSourceHelper.getQuery());
 }
 @Test
 public void getQuery() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("SELECT * FROM table", sqlSourceHelper.getQuery());
 }
 @Test
 public void getBatchSize() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals(100, sqlSourceHelper.getBatchSize());
 }
 @Test
 public void getRunQueryDelay() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals(10000, sqlSourceHelper.getRunQueryDelay());
 }
 @Test
 public void getPassword() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("password", sqlSourceHelper.getPassword());
 }
 @Test
 public void getUser() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   assertEquals("user", sqlSourceHelper.getUser());
 }
 @Test
 public void setCurrentIndex() {
   SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
   sqlSourceHelper.setCurrentIndex(10);
   assertEquals(10, sqlSourceHelper.getCurrentIndex());
 }
  @Test
  public void getConnectionURL() {

    SQLSourceHelper sqlSourceHelper = new SQLSourceHelper(context, "Source Name");
    assertEquals("jdbc:mysql://host:3306/database", sqlSourceHelper.getConnectionURL());
  }
  /**
   * Execute the selection query in the database
   *
   * @return The query result. Each Object is a cell content.
   *     <p>The cell contents use database types (date,int,string...), keep in mind in case of
   *     future conversions/castings.
   * @throws InterruptedException
   */
  @SuppressWarnings("unchecked")
  public List<List<Object>> executeQuery() throws InterruptedException {

    List<List<Object>> rowsList = new ArrayList<List<Object>>();
    Query query;

    if (!session.isConnected()) {
      resetConnection();
    }

    if (sqlSourceHelper.isCustomQuerySet()) {

      query = session.createSQLQuery(sqlSourceHelper.buildQuery());

      if (sqlSourceHelper.getMaxRows() != 0) {
        query = query.setMaxResults(sqlSourceHelper.getMaxRows());
      }
    } else {
      query =
          session
              .createSQLQuery(sqlSourceHelper.getQuery())
              .setFirstResult(Integer.parseInt(sqlSourceHelper.getCurrentIndex()));

      if (sqlSourceHelper.getMaxRows() != 0) {
        query = query.setMaxResults(sqlSourceHelper.getMaxRows());
      }
    }

    try {
      rowsList =
          query
              .setFetchSize(sqlSourceHelper.getMaxRows())
              .setResultTransformer(Transformers.TO_LIST)
              .list();
    } catch (Exception e) {
      resetConnection();
    }

    if (!rowsList.isEmpty()) {
      if (sqlSourceHelper.isCustomQuerySet()) {
        sqlSourceHelper.setCurrentIndex(rowsList.get(rowsList.size() - 1).get(0).toString());
      } else {
        sqlSourceHelper.setCurrentIndex(
            Integer.toString(
                (Integer.parseInt(sqlSourceHelper.getCurrentIndex()) + rowsList.size())));
      }
    }

    return rowsList;
  }