package at.ac.univie.isc.asio.metadata.sql;

import at.ac.univie.isc.asio.*;
import at.ac.univie.isc.asio.engine.sql.XmlSchemaType;
import at.ac.univie.isc.asio.io.Classpath;
import at.ac.univie.isc.asio.sql.Database;
import com.google.common.collect.Iterables;
import org.jooq.impl.SQLDataType;
import org.junit.Test;

import java.util.List;

import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.text.IsEqualIgnoringCase.equalToIgnoringCase;
import static org.junit.Assert.assertThat;

public class H2SchemaServiceTest {
  private final Database db = Database
      .create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1").build()
      .execute(Classpath.read("database/H2SchemaServiceTest-schema.sql"));

  private final H2SchemaService subject = new H2SchemaService(db.datasource());

  @Test
  public void fetches_public_table_metadata() throws Exception {
    final SqlSchema schema = subject.explore(Schema.valueOf("test"));
    final List<Table> tables = schema.getTable();
    assertThat(tables, hasSize(1));
  }

  @Test
  public void validate_extracted_metadata() throws Exception {
    final SqlSchema schema = subject.explore(Schema.valueOf("test"));
    final Table table = Iterables.getOnlyElement(schema.getTable());
    assertThat(table.getName(), is(equalToIgnoringCase("SAMPLE")));
    assertThat(table.getSchema(), is(equalToIgnoringCase("TEST")));
    assertThat(table.getCatalog(), is(equalToIgnoringCase("TEST")));
  }

  @Test
  public void validate_extracted_columns() throws Exception {
    final ObjectFactory jaxb = new ObjectFactory();
    final SqlSchema schema = subject.explore(Schema.valueOf("test"));
    final Table table = Iterables.getOnlyElement(schema.getTable());
    final Column idColumn = jaxb.createColumn()
        .withName("ID")
        .withType(XmlSchemaType.LONG.qname())
        .withSqlType(SQLDataType.INTEGER.getTypeName())
        .withLength(10);
    final Column dataColumn = jaxb.createColumn()
        .withName("DATA")
        .withType(XmlSchemaType.STRING.qname())
        .withSqlType(SQLDataType.VARCHAR.getTypeName())
        .withLength(255);
    assertThat(table.getColumn(), containsInAnyOrder(idColumn, dataColumn));
  }
}

@@ -70,16 +70,52 @@
 asio.*;%0A
+import at.ac.univie.isc.asio.Table;%0A
 import a
@@ -157,24 +157,24 @@
 SchemaType;%0A
-
 import at.ac
@@ -280,25 +280,17 @@
 collect.
-Iterables
+*
 ;%0Aimport
@@ -361,24 +361,69 @@
 util.List;%0A%0A
+import static org.hamcrest.Matchers.hasItem;%0A
 import stati
@@ -933,16 +933,16 @@
 ql%22));%0A%0A
-
   privat
@@ -1014,16 +1014,332 @@
 ce());%0A%0A
+  @Test%0A  public void explore_h2_transient_in_memory_db() throws Exception %7B%0A    final Database db = Database.create(%22jdbc:h2:mem:%22).build();%0A    final com.google.common.collect.Table%3CInteger, String, String%3E result =%0A        db.reference(%22SELECT SCHEMA()%22);%0A    assertThat(result.values(), hasItem(%22public%22));%0A  %7D%0A%0A
   @Test%0A

Add explorative test for H2 behavior