Search in sources :

Example 16 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestODataQueryExecution method helpProcedureExecute.

private ProcedureExecution helpProcedureExecute(MetadataFactory mf, String query, final String resultJson, String expectedURL, int responseCode, boolean decode) throws Exception {
    ODataExecutionFactory translator = new ODataExecutionFactory();
    translator.start();
    TranslationUtility utility = new TranslationUtility(TestODataMetadataProcessor.getTransformationMetadata(mf, translator));
    Command cmd = utility.parseCommand(query);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    WSConnection connection = Mockito.mock(WSConnection.class);
    Map<String, Object> headers = new HashMap<String, Object>();
    headers.put(MessageContext.HTTP_REQUEST_HEADERS, new HashMap<String, List<String>>());
    headers.put(WSConnection.STATUS_CODE, new Integer(responseCode));
    Dispatch<DataSource> dispatch = Mockito.mock(Dispatch.class);
    Mockito.stub(dispatch.getRequestContext()).toReturn(headers);
    Mockito.stub(dispatch.getResponseContext()).toReturn(headers);
    Mockito.stub(connection.createDispatch(Mockito.eq(HTTPBinding.HTTP_BINDING), Mockito.anyString(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE))).toReturn(dispatch);
    DataSource ds = new DataSource() {

        @Override
        public OutputStream getOutputStream() throws IOException {
            return new ByteArrayOutputStream();
        }

        @Override
        public String getName() {
            return "result";
        }

        @Override
        public InputStream getInputStream() throws IOException {
            ByteArrayInputStream in = new ByteArrayInputStream(resultJson.getBytes());
            return in;
        }

        @Override
        public String getContentType() {
            return "application/xml";
        }
    };
    Mockito.stub(dispatch.invoke(Mockito.any(DataSource.class))).toReturn(ds);
    ProcedureExecution execution = translator.createProcedureExecution((Call) cmd, context, utility.createRuntimeMetadata(), connection);
    execution.execute();
    ArgumentCaptor<String> endpoint = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> binding = ArgumentCaptor.forClass(String.class);
    Mockito.verify(connection).createDispatch(binding.capture(), endpoint.capture(), Mockito.eq(DataSource.class), Mockito.eq(Mode.MESSAGE));
    assertEquals(expectedURL, decode ? URLDecoder.decode(endpoint.getValue(), "utf-8") : endpoint.getValue());
    return execution;
}
Also used : WSConnection(org.teiid.translator.WSConnection) HashMap(java.util.HashMap) TranslationUtility(org.teiid.cdk.api.TranslationUtility) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataSource(javax.activation.DataSource) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcedureExecution(org.teiid.translator.ProcedureExecution) List(java.util.List)

Example 17 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestSwaggerQueryExecution method testScalarResponse.

@Test
public void testScalarResponse() throws Exception {
    String query = "exec loginStatus('foo');";
    String expectedURL = "http://petstore.swagger.io/v2/user/loginStatus?username=foo";
    String response = "foo";
    ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "GET", null, getHeaders());
    assertEquals("foo", excution.getOutputParameterValues().get(0));
}
Also used : ProcedureExecution(org.teiid.translator.ProcedureExecution) Test(org.junit.Test)

Example 18 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestSwaggerQueryExecution method testPostBasedQuery.

@Test
public void testPostBasedQuery() throws Exception {
    String query = "exec addPet(id=>99, category_id=>0,category_name=>'canine',name=>'nikky'," + "photoUrls=>('photo1','photo2'),tags_tag_id=>0, tags_tag_name=>'doggie'," + "status=>'available');";
    String expectedURL = "http://petstore.swagger.io/v2/pet";
    String response = "{" + "\"id\":99," + "\"name\":\"nikky\"," + "\"photoUrls\":[\"photo1\",\"photo2\"]," + "\"status\":\"available\"," + "\"category\":{" + "\"id\":0," + "\"name\":\"canine\"" + "}," + "\"tags\":[" + "{\"id\":0," + "\"name\":\"doggie\"" + "}" + "]}";
    ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "POST", response, getHeaders());
}
Also used : ProcedureExecution(org.teiid.translator.ProcedureExecution) Test(org.junit.Test)

Example 19 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestSwaggerQueryExecution method testNullParameter.

@Test(expected = TeiidRuntimeException.class)
public void testNullParameter() throws Exception {
    String query = "exec loginStatus(null);";
    String expectedURL = null;
    String response = null;
    ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "GET", null, getHeaders());
}
Also used : ProcedureExecution(org.teiid.translator.ProcedureExecution) Test(org.junit.Test)

Example 20 with ProcedureExecution

use of org.teiid.translator.ProcedureExecution in project teiid by teiid.

the class TestSwaggerQueryExecution method testDefaultResponse.

@Test
public void testDefaultResponse() throws Exception {
    String query = "exec logoutUser();";
    String expectedURL = "http://petstore.swagger.io/v2/user/logout";
    String response = "";
    ProcedureExecution excution = helpProcedureExecute(query, response, expectedURL, 200, true, "GET", null, getHeaders());
    assertNull(excution.next());
}
Also used : ProcedureExecution(org.teiid.translator.ProcedureExecution) Test(org.junit.Test)

Aggregations

ProcedureExecution (org.teiid.translator.ProcedureExecution)24 Test (org.junit.Test)20 MetadataFactory (org.teiid.metadata.MetadataFactory)7 CsdlReturnType (org.apache.olingo.commons.api.edm.provider.CsdlReturnType)6 List (java.util.List)5 ExecutionContext (org.teiid.translator.ExecutionContext)4 CsdlComplexType (org.apache.olingo.commons.api.edm.provider.CsdlComplexType)3 Call (org.teiid.language.Call)3 Command (org.teiid.language.Command)3 TranslatorException (org.teiid.translator.TranslatorException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 Connection (java.sql.Connection)2 Timestamp (java.sql.Timestamp)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 DataSource (javax.activation.DataSource)2 TranslationUtility (org.teiid.cdk.api.TranslationUtility)2 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)2