Search in sources :

Example 26 with HardCodedExecutionFactory

use of org.teiid.runtime.HardCodedExecutionFactory in project teiid by teiid.

the class TestODataIntegration method testReverseNavigationWithUniqueKey.

@Test
public void testReverseNavigationWithUniqueKey() throws Exception {
    HardCodedExecutionFactory hc = new HardCodedExecutionFactory();
    hc.addData("SELECT Customers.id, Customers.name FROM Customers", Arrays.asList(Arrays.asList(1, "customer1"), Arrays.asList(2, "customer2"), Arrays.asList(3, "customer3"), Arrays.asList(4, "customer4")));
    hc.addData("SELECT Customers.id FROM Customers", Arrays.asList(Arrays.asList(1), Arrays.asList(2), Arrays.asList(3), Arrays.asList(4)));
    hc.addData("SELECT Orders.id, Orders.customerid FROM Orders", Arrays.asList(Arrays.asList(1, 1), Arrays.asList(2, 1), Arrays.asList(3, 1), Arrays.asList(4, 1), Arrays.asList(5, 2), Arrays.asList(6, 2), Arrays.asList(7, 3), Arrays.asList(8, 3)));
    hc.addData("SELECT Orders.id, Orders.customerid, Orders.place FROM Orders", Arrays.asList(Arrays.asList(1, 1, "town"), Arrays.asList(2, 1, "state"), Arrays.asList(3, 1, "country"), Arrays.asList(4, 1, "abroad"), Arrays.asList(5, 2, "state"), Arrays.asList(6, 2, "country"), Arrays.asList(7, 3, "town"), Arrays.asList(8, 3, "town")));
    hc.addData("SELECT Orders.customerid, Orders.id, Orders.place FROM Orders", Arrays.asList(Arrays.asList(1, 1, "town"), Arrays.asList(1, 2, "state"), Arrays.asList(1, 3, "country"), Arrays.asList(1, 4, "abroad"), Arrays.asList(2, 5, "state"), Arrays.asList(2, 6, "country"), Arrays.asList(3, 7, "town"), Arrays.asList(3, 8, "town")));
    teiid.addTranslator("x12", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "CREATE FOREIGN TABLE Customers (\n" + "  id integer UNIQUE OPTIONS (NAMEINSOURCE 'id'),\n" + "  name varchar(10));\n" + "CREATE FOREIGN TABLE Orders (\n" + "  id integer PRIMARY KEY OPTIONS (NAMEINSOURCE 'id'),\n" + "  customerid integer,\n" + "  place varchar(10),\n" + "  CONSTRAINT Customer FOREIGN KEY (customerid) REFERENCES Customers(id));");
        mmd.addSourceMapping("x12", "x12", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = null;
        response = http.newRequest(baseURL + "/northwind/m/Orders(1)/Customer").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers/$entity\"," + "\"id\":1,\"name\":\"customer1\"}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Orders(1)?$expand=Customer").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Orders/$entity\"," + "\"id\":1,\"customerid\":1,\"place\":\"town\"," + "\"Customer\":{\"id\":1,\"name\":\"customer1\"}}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/Orders(1)/Customer/Orders_Customer").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#Customers\"," + "\"value\":[{\"id\":1,\"customerid\":1,\"place\":\"town\"}," + "{\"id\":2,\"customerid\":1,\"place\":\"state\"}," + "{\"id\":3,\"customerid\":1,\"place\":\"country\"}," + "{\"id\":4,\"customerid\":1,\"place\":\"abroad\"}]}", response.getContentAsString());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 27 with HardCodedExecutionFactory

use of org.teiid.runtime.HardCodedExecutionFactory in project teiid by teiid.

the class TestODataIntegration method testExpandComplex.

@Test
public void testExpandComplex() throws Exception {
    HardCodedExecutionFactory hc = new HardCodedExecutionFactory();
    hc.addData("SELECT x.a, x.b FROM x", Arrays.asList(Arrays.asList("a", "b")));
    hc.addData("SELECT y.b, y.a FROM y", Arrays.asList(Arrays.asList("a", "y"), Arrays.asList("a", "y1")));
    hc.addData("SELECT y.a, y.b FROM y", Arrays.asList(Arrays.asList("y", "a"), Arrays.asList("y1", "a")));
    hc.addData("SELECT z.a, z.b FROM z", Arrays.asList(Arrays.asList("a", "y")));
    hc.addData("SELECT z.b, z.a FROM z", Arrays.asList(Arrays.asList("y", "a")));
    teiid.addTranslator("x7", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "create foreign table x (" + " a string, " + " b string, " + " primary key (a)" + ") options (updatable true);" + "create foreign table y (" + " a string, " + " b string, " + " primary key (a)," + " CONSTRAINT FKX FOREIGN KEY (b) REFERENCES x(a)" + ") options (updatable true);" + "create foreign table z (" + " a string, " + " b string, " + " primary key (a)," + " CONSTRAINT FKX FOREIGN KEY (a) REFERENCES x(a)," + " CONSTRAINT FKY FOREIGN KEY (b) REFERENCES y(a)" + ") options (updatable true);");
        mmd.addSourceMapping("x7", "x7", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = null;
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=y_FKX($expand=z_FKY)").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#x\",\"value\":[" + "{\"a\":\"a\",\"b\":\"b\",\"y_FKX\":" + "[{\"a\":\"y\",\"b\":\"a\",\"z_FKY\":[{\"a\":\"a\",\"b\":\"y\"}]},{\"a\":\"y1\",\"b\":\"a\",\"z_FKY\":[]}]}" + "]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=y_FKX,z_FKX").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#x\",\"value\":[" + "{\"a\":\"a\",\"b\":\"b\",\"y_FKX\":" + "[{\"a\":\"y\",\"b\":\"a\"},{\"a\":\"y1\",\"b\":\"a\"}]," + "\"z_FKX\":{\"a\":\"a\",\"b\":\"y\"}}" + "]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=*").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#x\",\"value\":[" + "{\"a\":\"a\",\"b\":\"b\",\"y_FKX\":" + "[{\"a\":\"y\",\"b\":\"a\"},{\"a\":\"y1\",\"b\":\"a\"}]," + "\"z_FKX\":{\"a\":\"a\",\"b\":\"y\"}}" + "]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=y_FKX($filter=a%20eq%20'y1'),*").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#x\",\"value\":[" + "{\"a\":\"a\",\"b\":\"b\",\"y_FKX\":" + "[{\"a\":\"y1\",\"b\":\"a\"}]," + "\"z_FKX\":{\"a\":\"a\",\"b\":\"y\"}}" + "]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=y_FKX,y_FKX").method("GET").send();
        assertEquals(400, response.getStatus());
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=*($levels=3)").method("GET").send();
        assertEquals(200, response.getStatus());
        String expected = "{" + "\"@odata.context\":\"$metadata#x\"," + "\"value\":[" + "{" + "\"a\":\"a\"," + "\"b\":\"b\"," + "\"y_FKX\":[" + "{" + "\"a\":\"y\"," + "\"b\":\"a\"," + "\"FKX\":" + "{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/x('a')\"" + "}" + "," + "\"z_FKY\":[" + "{" + "\"a\":\"a\"," + "\"b\":\"y\"," + "\"FKX\":{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/x('a')\"" + "}," + "\"FKY\":" + "{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/y('y')\"" + "}" + "" + "}" + "]" + "}," + "{" + "\"a\":\"y1\"," + "\"b\":\"a\"," + "\"FKX\":" + "{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/x('a')\"" + "}" + "," + "\"z_FKY\":[" + "]" + "}" + "]," + "\"z_FKX\":{" + "\"a\":\"a\"," + "\"b\":\"y\"," + "\"FKX\":{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/x('a')\"" + "}," + "\"FKY\":" + "{" + "\"a\":\"y\"," + "\"b\":\"a\"," + "\"FKX\":" + "{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/x('a')\"" + "}" + "," + "\"z_FKY\":[" + "{" + "\"@odata.id\":\"" + baseURL + "/northwind/m/z('a')\"" + "}" + "]" + "}" + "" + "}" + "}" + "]" + "}";
        assertEquals(expected, response.getContentAsString());
        // invalid it's not a self relationship
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=y_FKX($levels=1)").method("GET").send();
        assertEquals(400, response.getStatus());
        response = http.newRequest(baseURL + "/northwind/m/x?$expand=y_FKX($filter=$it/b%20eq%20a)").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#x\",\"value\":[" + "{\"a\":\"a\",\"b\":\"b\",\"y_FKX\":" + "[]}" + "]}", response.getContentAsString());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 28 with HardCodedExecutionFactory

use of org.teiid.runtime.HardCodedExecutionFactory in project teiid by teiid.

the class TestODataIntegration method testRelatedEntities.

@Test
public void testRelatedEntities() throws Exception {
    HardCodedExecutionFactory hc = buildHardCodedExecutionFactory();
    hc.addData("SELECT x.a, x.b FROM x WHERE x.a = 'xa1'", Arrays.asList(Arrays.asList("xa1", "xb")));
    hc.addData("SELECT y.a, y.b FROM y WHERE y.b = 'xa1'", Arrays.asList(Arrays.asList("ya1", "xa1"), Arrays.asList("ya2", "xa1")));
    // 1-many
    hc.addData("SELECT x.a FROM x WHERE x.a = 'xa2'", Arrays.asList(Arrays.asList("xa2")));
    hc.addData("SELECT y.a, y.b FROM y WHERE y.b = 'xa2'", new ArrayList<List<?>>());
    // 1-1
    hc.addData("SELECT z.a FROM z WHERE z.a = 'xa3'", Arrays.asList(Arrays.asList("xa3")));
    hc.addData("SELECT x.a, x.b FROM x WHERE x.a = 'xa3'", new ArrayList<List<?>>());
    teiid.addTranslator("x7", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "create foreign table x (" + " a string, " + " b string, " + " primary key (a)" + ") options (updatable true);" + "create foreign table y (" + " a string, " + " b string, " + " primary key (a)," + " CONSTRAINT FKX FOREIGN KEY (b) REFERENCES x(a)" + ") options (updatable true);" + "create foreign table z (" + " a string, " + " b string, " + " primary key (a)," + " CONSTRAINT FKX FOREIGN KEY (a) REFERENCES x(a)" + ") options (updatable true);");
        mmd.addSourceMapping("x7", "x7", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = null;
        // single 1-many relation
        response = http.newRequest(baseURL + "/northwind/m/x('xa2')/y_FKX").method("GET").send();
        assertEquals(200, response.getStatus());
        assertEquals("{\"@odata.context\":\"$metadata#y\",\"value\":[]}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/z('xa3')/FKX").method("GET").send();
        assertEquals(204, response.getStatus());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) List(java.util.List) ArrayList(java.util.ArrayList) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Example 29 with HardCodedExecutionFactory

use of org.teiid.runtime.HardCodedExecutionFactory in project teiid by teiid.

the class TestODataIntegration method testErrorCodes.

@Test
public void testErrorCodes() throws Exception {
    HardCodedExecutionFactory hc = new HardCodedExecutionFactory() {

        @Override
        public ResultSetExecution createResultSetExecution(final QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            List<? extends List<?>> list = getData(command);
            if (list == null) {
                throw new RuntimeException(command.toString());
            }
            final Iterator<? extends List<?>> result = list.iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                    throw new TranslatorException(ODataPlugin.Event.TEIID16001, "execution failed");
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (result.hasNext()) {
                        return result.next();
                    }
                    return null;
                }
            };
        }
    };
    hc.addData("SELECT x.a, x.b FROM x", Arrays.asList(Arrays.asList("a", 1)));
    teiid.addTranslator("x1", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "create foreign table x (a string, b integer, primary key (a));");
        mmd.addSourceMapping("x1", "x1", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = http.newRequest(baseURL + "/northwind/m/x").method("GET").send();
        assertEquals(400, response.getStatus());
        assertEquals("{\"error\":{\"code\":\"TEIID30504\"," + "\"message\":\"TEIID30504 x1: TEIID16001 execution failed\"}}", response.getContentAsString());
        response = http.newRequest(baseURL + "/northwind/m/x?$format=xml").method("GET").send();
        assertEquals(400, response.getStatus());
        assertEquals("<?xml version='1.0' encoding='UTF-8'?>" + "<error xmlns=\"http://docs.oasis-open.org/odata/ns/metadata\">" + "<code>TEIID30504</code>" + "<message>TEIID30504 x1: TEIID16001 execution failed</message>" + "</error>", response.getContentAsString());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) Test(org.junit.Test)

Example 30 with HardCodedExecutionFactory

use of org.teiid.runtime.HardCodedExecutionFactory in project teiid by teiid.

the class TestODataIntegration method testNonExistentEntity.

@Test
public void testNonExistentEntity() throws Exception {
    HardCodedExecutionFactory hc = new HardCodedExecutionFactory();
    hc.addData("SELECT x.a, x.b FROM x", Arrays.asList(Arrays.asList("a", 1)));
    teiid.addTranslator("x1", hc);
    try {
        ModelMetaData mmd = new ModelMetaData();
        mmd.setName("m");
        mmd.addSourceMetadata("ddl", "create foreign table x (a string, b integer, primary key (a));");
        mmd.addSourceMapping("x1", "x1", null);
        teiid.deployVDB("northwind", mmd);
        localClient = getClient(teiid.getDriver(), "northwind", new Properties());
        ContentResponse response = http.newRequest(baseURL + "/northwind/m/x('b')").method("GET").send();
        assertEquals(404, response.getStatus());
        response = http.newRequest(baseURL + "/northwind/m/x('b')/b").method("GET").send();
        assertEquals(404, response.getStatus());
    } finally {
        localClient = null;
        teiid.undeployVDB("northwind");
    }
}
Also used : ContentResponse(org.eclipse.jetty.client.api.ContentResponse) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) Properties(java.util.Properties) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) Test(org.junit.Test)

Aggregations

HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)46 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)43 Test (org.junit.Test)40 Properties (java.util.Properties)27 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)27 StringContentProvider (org.eclipse.jetty.client.util.StringContentProvider)10 Connection (java.sql.Connection)8 ResultSet (java.sql.ResultSet)8 Statement (java.sql.Statement)7 CallableStatement (java.sql.CallableStatement)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 FakeServer (org.teiid.jdbc.FakeServer)4 QueryExpression (org.teiid.language.QueryExpression)3 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)3 Table (org.teiid.metadata.Table)3 EmbeddedConfiguration (org.teiid.runtime.EmbeddedConfiguration)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BeforeClass (org.junit.BeforeClass)2 AbstractQueryTest (org.teiid.jdbc.AbstractQueryTest)2