/*
 * Copyright 2014-2015 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.io;

import static br.com.objectos.way.assertion.Assertion.assertThat;

import br.com.objectos.way.io.SqlReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
 * @author marcio.endo@objectos.com.br (Marcio Endo)
 */
public class SqlReaderTest extends AbstractIoSqlTest {

  @Test
  public void issue42_string_after_string_before_delimiter() {
    try (SqlReader reader = mysqlReader("/sql/sql-reader-mysql6.sql")) {
      List<String> res = reader.stream().collect(Collectors.toList());
      assertThat(res).hasSize(2).equalTo(
          "select 'a' as 'b'",
          "select 1");
    } catch (IOException e) {
      Assert.fail();
    }
  }

  @DataProvider
  public Object[][] iteratorProvider() {
    return new Object[][] {
        { "/sql/sql-reader-mysql0.sql" },
        { "/sql/sql-reader-mysql1.sql" },
        { "/sql/sql-reader-mysql2.sql" },
        { "/sql/sql-reader-mysql3.sql" },
        { "/sql/sql-reader-mysql4.sql" }
    };
  }

  @Test(dataProvider = "iteratorProvider")
  public void iterator_mysql(String resourceName) {
    try (SqlReader reader = mysqlReader(resourceName)) {
      Iterator<String> iter = reader.iterator();
      assertThat(iter.hasNext()).is(true);
      assertThat(iter.next()).equalTo("select 1");
      assertThat(iter.hasNext()).is(true);
      assertThat(iter.next()).equalTo("select 2");
      assertThat(iter.hasNext()).is(false);
    } catch (IOException e) {
      Assert.fail();
    }
  }

  @Test
  public void stream_mysql() {
    try (SqlReader reader = mysqlReader("/sql/sql-reader-mysql5.sql")) {
      List<String> res = reader.stream().collect(Collectors.toList());
      assertThat(res).hasSize(4).equalTo(
          "select 'abc'",
          "select \"abc\"",
          "select \"a'b'c\"",
          "select \"a\"\"b\"\"c\"");
    } catch (IOException e) {
      Assert.fail();
    }
  }

  @Test
  public void stream_oracle0() {
    try (SqlReader reader = oracleReader("/sql/sql-reader-oracle0.sql")) {
      List<String> res = reader.stream().collect(Collectors.toList());
      assertThat(res).hasSize(1).equalTo(
          "CREATE PROCEDURE remove_emp (employee_id NUMBER) AS tot_emps NUMBER; BEGIN DELETE FROM employees WHERE employees.employee_id = remove_emp.employee_id; tot_emps := tot_emps - 1; END;");
    } catch (IOException e) {
      Assert.fail();
    }
  }

  @Test
  public void stringAction() {
    try (SqlReader reader = mysqlReader("/sql/sql-reader-mysql7.sql")) {
      List<String> res = reader.stream().collect(Collectors.toList());
      assertThat(res).hasSize(2).equalTo(
          "select _utf8''",
          "select 2");
    } catch (IOException e) {
      Assert.fail();
    }
  }

}