@Test
  public void testSystemPropertiesInVDBXML() throws Exception {
    String vdbName = "test";
    String testVDB =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<vdb name=\"test\" version=\"1\">\n"
            + "    <property name=\"UseConnectorMetadata\" value=\"${teiid.vdb.UseConnectorMetadata:none}\" />\n"
            + "    <model name=\"loopy\">\n"
            + "        <source name=\"loop\" translator-name=\"loopy\" />\n"
            + "    </model>\n"
            + "</vdb>";

    Collection<?> vdbs = admin.getVDBs();
    assertTrue(vdbs.isEmpty());

    JavaArchive jar = getLoopyArchive();
    admin.deploy("loopy.jar", jar.as(ZipExporter.class).exportAsInputStream());

    // normal load
    admin.deploy("test-vdb.xml", new ByteArrayInputStream(testVDB.getBytes()));
    AdminUtil.waitForVDBLoad(admin, vdbName, 1, 3);

    VDB vdb = admin.getVDB(vdbName, 1);
    String value = vdb.getPropertyValue("UseConnectorMetadata");

    // see the arquillian.zml file in resources in the JVM proeprties section for the expected value
    assertEquals("custom", value);

    admin.undeploy("loopy.jar");
    admin.undeploy("test-vdb.xml");
  }
  @Test
  public void testReadOdataMetadata() throws Exception {
    String vdb =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<vdb name=\"Loopy\" version=\"1\">\n"
            + "    <model name=\"MarketData\">\n"
            + "        <source name=\"text-connector2\" translator-name=\"loopback\" />\n"
            + "         <metadata type=\"DDL\"><![CDATA[\n"
            + "                CREATE FOREIGN TABLE G1 (e1 string[], e2 integer PRIMARY KEY);\n"
            + "                CREATE FOREIGN TABLE G2 (e1 string, e2 integer PRIMARY KEY) OPTIONS (UPDATABLE 'true');\n"
            + "        ]]> </metadata>\n"
            + "    </model>\n"
            + "</vdb>";

    admin.deploy(
        "loopy-vdb.xml", new ReaderInputStream(new StringReader(vdb), Charset.forName("UTF-8")));

    assertTrue(AdminUtil.waitForVDBLoad(admin, "Loopy", 1, 3));

    String vdb2 =
        "<?xml version='1.0' encoding='UTF-8'?>\n"
            + "<vdb name=\"TestOData\" version=\"1\">\n"
            + "    <model name=\"TestOData\" type=\"PHYSICAL\" visible=\"true\">\n"
            + "     <property name=\"importer.entityContainer\" value=\"MarketData\"/>\n"
            + "     <property name=\"importer.schemaNamespace\" value=\"MarketData\"/>\n"
            + "     <source name=\"TestOData\" translator-name=\"odata4\" connection-jndi-name=\"java:/TestOData4\"/>\n"
            + "    </model>\n"
            + "</vdb>";

    Properties p = new Properties();
    p.setProperty("class-name", "org.teiid.resource.adapter.ws.WSManagedConnectionFactory");
    p.setProperty("EndPoint", "http://*****:*****@mm://localhost:31000;user=user;password=user", null);

    execute("SELECT Name FROM Sys.Tables Where name='G1'"); // $NON-NLS-1$
    assertResultsSetEquals("Name[string]\nG1");

    execute("SELECT * from G1"); // $NON-NLS-1$
    assertResultsSetEquals("e1[string[]]    e2[integer]\n[ABCDEFGHIJ]    0");

    admin.undeploy("loopy-vdb.xml");
    admin.undeploy("test-vdb.xml");
  }
  // TEIID-3914 - test the olingo-patch work
  public void testCompositeKeyTimestamp() throws Exception {

    String vdb =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<vdb name=\"northwind\" version=\"1\">\n"
            + "    <model name=\"m\">\n"
            + "        <source name=\"x1\" translator-name=\"loopback\" />\n"
            + "         <metadata type=\"DDL\"><![CDATA[\n"
            + "                CREATE FOREIGN TABLE x (a string, b timestamp, c integer, primary key (a, b)) options (updatable true);\n"
            + "        ]]> </metadata>\n"
            + "    </model>\n"
            + "</vdb>";

    admin.deploy(
        "loopy-vdb.xml", new ReaderInputStream(new StringReader(vdb), Charset.forName("UTF-8")));
    assertTrue(AdminUtil.waitForVDBLoad(admin, "Loopy", 1, 3));

    WebClient client =
        WebClient.create("http://localhost:8080/odata4/m/x(a='a',b=2011-09-11T00:00:00Z)");
    client.header(
        "Authorization",
        "Basic " + Base64.encodeBytes(("user:user").getBytes())); // $NON-NLS-1$ //$NON-NLS-2$
    Response response = client.invoke("GET", null);
    assertEquals(200, response.getStatus());

    client = WebClient.create("http://localhost:8080/odata4/m/x");
    client.header(
        "Authorization",
        "Basic " + Base64.encodeBytes(("user:user").getBytes())); // $NON-NLS-1$ //$NON-NLS-2$
    response = client.post("{\"a\":\"b\", \"b\":\"2000-02-02T22:22:22Z\"}");
    assertEquals(204, response.getStatus());

    admin.undeploy("loopy-vdb.xml");
  }
  @Test
  public void testDDLExport() throws Exception {
    String vdbName = "test";
    String testVDB =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<vdb name=\"test\" version=\"1\">\n"
            + "    <property name=\"UseConnectorMetadata\" value=\"cached\" />\n"
            + "    <model name=\"loopy\">\n"
            + "        <source name=\"loop\" translator-name=\"loopy\" />\n"
            + "    </model>\n"
            + "</vdb>";

    Collection<?> vdbs = admin.getVDBs();
    assertTrue(vdbs.isEmpty());

    JavaArchive jar = getLoopyArchive();
    admin.deploy("loopy.jar", jar.as(ZipExporter.class).exportAsInputStream());

    // normal load
    admin.deploy("test-vdb.xml", new ByteArrayInputStream(testVDB.getBytes()));
    AdminUtil.waitForVDBLoad(admin, vdbName, 1, 3);

    String ddl = admin.getSchema(vdbName, 1, "loopy", null, null);

    String expected = "CREATE FOREIGN TABLE Matadata (\n" + "	execCount integer\n" + ");";
    assertEquals(expected, ddl);

    admin.undeploy("loopy.jar");
  }
  @Test
  public void testVDBRestart() throws Exception {
    String vdbName = "test";
    String testVDB =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<vdb name=\"test\" version=\"1\">\n"
            + "    <property name=\"UseConnectorMetadata\" value=\"cached\" />\n"
            + "    <model name=\"loopy\">\n"
            + "        <source name=\"loop\" translator-name=\"loopy\" />\n"
            + "    </model>\n"
            + "</vdb>";

    Collection<?> vdbs = admin.getVDBs();
    assertTrue(vdbs.isEmpty());

    JavaArchive jar = getLoopyArchive();
    admin.deploy("loopy.jar", jar.as(ZipExporter.class).exportAsInputStream());

    // normal load
    admin.deploy("test-vdb.xml", new ByteArrayInputStream(testVDB.getBytes()));
    AdminUtil.waitForVDBLoad(admin, vdbName, 1, 3);
    int count = assertMetadataLoadCount(false, 1);

    // 1st restart
    admin.restartVDB(vdbName, 1);
    AdminUtil.waitForVDBLoad(admin, vdbName, 1, 3);
    count = assertMetadataLoadCount(true, count + 1);

    // 2nd restart
    admin.restartVDB(vdbName, 1);
    AdminUtil.waitForVDBLoad(admin, vdbName, 1, 3);
    count = assertMetadataLoadCount(true, count + 1);

    admin.undeploy("loopy.jar");
  }
  @Test
  public void testOdata() throws Exception {
    String vdb =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
            + "<vdb name=\"Loopy\" version=\"1\">\n"
            + "    <model name=\"MarketData\">\n"
            + "        <source name=\"text-connector2\" translator-name=\"loopback\" />\n"
            + "         <metadata type=\"DDL\"><![CDATA[\n"
            + "                CREATE FOREIGN TABLE G1 (e1 string, e2 integer PRIMARY KEY);\n"
            + "                CREATE FOREIGN TABLE G2 (e1 string, e2 integer PRIMARY KEY) OPTIONS (UPDATABLE 'true');\n"
            + "        ]]> </metadata>\n"
            + "    </model>\n"
            + "</vdb>";

    admin.deploy(
        "loopy-vdb.xml", new ReaderInputStream(new StringReader(vdb), Charset.forName("UTF-8")));

    assertTrue(AdminUtil.waitForVDBLoad(admin, "Loopy", 1, 3));

    WebClient client =
        WebClient.create("http://*****:*****@mm://localhost:31000;user=user;password=user", null);

    PreparedStatement ps =
        conn.prepareCall(
            "select t.* from xmltable('/*:Edmx/*:DataServices/*:Schema[@Alias=\"MarketData\"]' passing xmlparse(document cast(? as clob))) as t");
    ps.setAsciiStream(1, (InputStream) response.getEntity());

    ResultSet rs = ps.executeQuery();
    rs.next();

    assertEquals(
        ObjectConverterUtil.convertFileToString(
            UnitTestUtil.getTestDataFile("loopy-metadata4-results.txt")),
        rs.getString(1));

    conn.close();

    // try an invalid url
    client = WebClient.create("http://localhost:8080/odata/x/y$metadata");
    client.header(
        "Authorization",
        "Basic " + Base64.encodeBytes(("user:user").getBytes())); // $NON-NLS-1$ //$NON-NLS-2$
    response = client.invoke("GET", null);
    assertEquals(500, response.getStatus());

    admin.undeploy("loopy-vdb.xml");
  }
  @Test
  public void testGetRequests() throws Exception {
    JavaArchive jar = getLoopyArchive();

    try {
      admin.deploy("loopy.jar", jar.as(ZipExporter.class).exportAsInputStream());
      deployVdb();
      Translator t = admin.getTranslator("loopy");
      assertNotNull(t);

      admin.updateSource("bqt", 1, "Source", "loopy", "java:jboss/datasources/ExampleDS");
      Connection conn =
          TeiidDriver.getInstance()
              .connect("jdbc:teiid:bqt@mm://localhost:31000;user=user;password=user", null);
      Collection<? extends Session> sessions = admin.getSessions();
      assertEquals(1, sessions.size());
      Session s = sessions.iterator().next();

      Statement stmt = conn.createStatement();

      Collection<? extends Request> requests = admin.getRequests();

      assertEquals(0, requests.size());

      stmt.execute("select * from source.smalla");

      requests = admin.getRequests();
      assertEquals(1, requests.size());

      Request r = requests.iterator().next();
      assertEquals("select * from source.smalla", r.getCommand());
      assertNotNull(r.getExecutionId());
      assertNotNull(r.getSessionId());

      stmt.execute("select * from source.smalla");
      Collection<? extends Request> requests2 = admin.getRequestsForSession(s.getSessionId());
      assertEquals(1, requests2.size());

      Request r2 = requests.iterator().next();
      assertEquals("select * from source.smalla", r2.getCommand());
      assertEquals(s.getSessionId(), r2.getSessionId());

      stmt.close();
      conn.close();

      requests = admin.getRequests();
      assertEquals(0, requests.size());

    } finally {
      admin.undeploy("loopy.jar");
    }
  }
  @Test
  public void testChainedDelegates() throws Exception {
    Properties props = new Properties();
    props.setProperty("connection-url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1");
    props.setProperty("user-name", "sa");
    props.setProperty("password", "sa");

    AdminUtil.createDataSource(admin, "Oracle11_PushDS", "h2", props);
    admin.deploy("fake.jar", new FileInputStream(UnitTestUtil.getTestDataFile("fake.jar")));
    try {
      admin.deploy(
          "chained-vdb.xml", new FileInputStream(UnitTestUtil.getTestDataFile("chained-vdb.xml")));
    } finally {
      admin.undeploy("fake.jar");
    }
  }
  @Test
  public void testTraslators() throws Exception {
    Collection<? extends Translator> translators = admin.getTranslators();
    assertEquals(38, translators.size());

    JavaArchive jar = getLoopyArchive();

    try {
      admin.deploy("loopy.jar", jar.as(ZipExporter.class).exportAsInputStream());

      VDBTranslatorMetaData t = (VDBTranslatorMetaData) admin.getTranslator("loopy");
      assertNotNull(t);
      assertEquals("ANY", t.getPropertyValue("SupportedJoinCriteria"));
      assertEquals("true", t.getPropertyValue("supportsSelectDistinct"));
    } finally {
      admin.undeploy("loopy.jar");
    }

    VDBTranslatorMetaData t = (VDBTranslatorMetaData) admin.getTranslator("orcl");
    assertNull(t);
  }